The CharBuffer class in Java is used to create and manipulate character buffers.
A buffer is a temporary storage of data. It is mostly used to read input data or export data to another process/program.
There are three methods of creating a character buffer:
The duplicate
method is used for the third approach. duplicate
is used to create a copy of an existing character buffer. The newly created copy will have the same independent position, limit, and capacity, but the content will be shared. Changes in the content of one buffer will be reflected in the other.
Each buffer has a capacity, position, and limit. Initially, the position points to index 0 and the limit points to the last index. Each time a character is entered, it is placed at the position and the position is incremented by one. The capacity is the maximum number of characters that can be accommodated in the buffer.
The newly created buffer will be direct or read-only, depending on the original buffer. Both of the buffers will share the same properties.
The duplicate
method does not accept any parameters, as shown in the following snippet of code:
[CharBuffer_obj].duplicate();
In the above snippet, CharBuffer_obj
will be replaced by the name of an object of the CharBuffer
class.
The duplicate
method returns the newly created character buffer. It is also an object of the CharBuffer
class.
In the following snippet of code, we use the wrap
method to create the first character buffer charBuff_1
and then use the put
method to populate data.
Then, we use the duplicate
method to create the second character buffer charBuff_2
.
When we enter the ‘!’ character in charBuff_2
, it can also be seen in charBuff_1
. However, using the flip
method on charBuff_1
sets its position to 0, but does not affect the position of charBuff_2
. This can be seen when the position of both the buffers are obtained using the position
method and printed.
The
flip
method is used to set the position to 0 and the limit to the last value of the position. Theposition
method is used to get the current position.
This example shows that when we create a copy of a character buffer using the duplicate
method, the newly created copy will share the content of the original buffer. Any changes in one buffer will also be reflected in the other. However, the position and limit for both buffers are independent.
import java.nio.*;import java.util.*;public class main {public static void main(String[] args){// Declare the char arraychar[] arr = new char[10];// creating the CharBuffer object and// wrapping the char array into itCharBuffer charBuff_1 = CharBuffer.wrap(arr);// populating data into the CharBuffer objectcharBuff_1.put("Educative");// using the duplicate method to create// a copy of an existing bufferCharBuffer charBuff_2 = charBuff_1.duplicate();charBuff_2.put("!");// usig the flip function to set// position to 0th index and// limit to the last value of positioncharBuff_1.flip();int position_1 = charBuff_1.position();int position_2 = charBuff_2.position();// print the byte bufferSystem.out.println("CharBuffer-1 is : "+ Arrays.toString(charBuff_1.array())+ "\nPosition of Buffer-1: "+position_1);System.out.println("\nCharBuffer-2 is : "+ Arrays.toString(charBuff_2.array()) + "\nPosition of Buffer-2: "+position_2);}}
Free Resources