CharBuffer put() methods in Java
put(char i)
The put(char i) method of java.nio.CharBuffer Class is used to write the given character into the newly created char buffer at the current position and then increments the position.
Syntax :
public abstract CharBuffer put(char i)
Parameters: This method takes the char value i as a parameter which is to be written in char buffer.
Return Value: This method returns this buffer, in which the char value is inserted.
Exception: This method throws the following exceptions:
- BufferOverflowException- If this buffer’s current position is not smaller than its limit
- ReadOnlyBufferException- If this buffer is read-only
Below are the examples to illustrate the put(char i) method:
Example 1:
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity);
cb.put( 'a' );
cb.put( 'b' );
cb.put( 'c' );
cb.rewind();
System.out.println( "Original CharBuffer: "
+ Arrays.toString(cb.array()));
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Original CharBuffer: [a, b, c]
Example 2: To demonstrate BufferOverflowException.
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity);
cb.put( 'a' );
cb.put( 'b' );
cb.put( 'c' );
System.out.println( "Trying to put the Int at the "
+ "position more than its limit" );
cb.put( 'd' );
cb.rewind();
System.out.println( "Original CharBuffer: "
+ Arrays.toString(cb.array()));
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the Int at the position more than its limit
Exception throws : java.nio.BufferOverflowException
Examples 3: To demonstrate ReadOnlyBufferException.
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity);
CharBuffer cb1 = cb.asReadOnlyBuffer();
System.out.println( "Trying to put the Int value"
+ " in read only buffer" );
cb1.put( 'a' );
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the Int value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException
put(int index, char i)
The put(int index, char i) method of java.nio.CharBuffer Class is used to write the given int into the buffer at the given index.
Syntax:
public abstract CharBuffer put(int index, char i)
Parameters: This method takes the following arguments as a parameter:
- index: The index at which the char will be written
- i: The char value to be written
Return Value: This method returns this buffer.
Exception: This method throws the following exception:
- IndexOutOfBoundsException- If index is negative or not smaller than the buffer’s limit
- ReadOnlyBufferException- If this buffer is read-only
Below are the examples to illustrate the put(int index, char i) method:
Example 1:
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity);
cb.put( 0 , 'a' );
cb.put( 2 , 'c' );
cb.put( 1 , 'b' );
cb.rewind();
System.out.println( "Original CharBuffer: "
+ Arrays.toString(cb.array()));
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Original CharBuffer: [a, b, c]
Example 2: To demonstrate IndexOutOfBoundsException.
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity);
cb.put( 0 , 'c' );
cb.put( 2 , 'b' );
System.out.println( "Trying to put the value"
+ " at the negative index" );
cb.put(- 1 , 'a' );
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the value at the negative index
Exception throws : java.lang.IndexOutOfBoundsException
Example 3: To demonstrate ReadOnlyBufferException.
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
CharBuffer cb = CharBuffer.allocate(capacity);
CharBuffer cb1 = cb.asReadOnlyBuffer();
System.out.println( "Trying to put the Int value"
+ " in read only buffer" );
cb1.put( 0 , 'a' );
}
catch (BufferOverflowException e) {
System.out.println( "Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws : " + e);
}
}
}
|
Output:
Trying to put the Int value in read only buffer
Exception throws : java.nio.ReadOnlyBufferException
Last Updated :
20 Nov, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...