CharBuffer wrap() method in Java
Last Updated :
06 Nov, 2019
wrap(char[ ] array)
The wrap() method of java.nio.CharBuffer Class is used to wrap a character array into a buffer. The new buffer will be backed by the given char array. As a consequence, any modifications to the buffer will cause the array to be modified and vice versa. The new buffer’s capacity is determined by array.length, its position will be zero, and its mark will be undefined. Its backing array will be the given array, and its array offset will be zero.
Syntax:
public static CharBuffer wrap(char[] array)
Parameters: This method accepts an array as a parameter which will back this buffer.
Return Value: This method returns the new char buffer.
Below are the examples to illustrate the wrap() method:
Example-1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
char [] cbb = { 'a' , 'b' , 'c' };
System.out.println( "Array length : " + cbb.length);
System.out.println( "\nArray element : "
+ Arrays.toString(cbb));
CharBuffer charBuffer = CharBuffer.wrap(cbb);
charBuffer.rewind();
System.out.println( "\ncharBuffer : "
+ Arrays.toString(charBuffer.array()));
System.out.println( "\ncharbuffer capacity : "
+ charBuffer.capacity());
System.out.println( "\ncharbuffer position: "
+ charBuffer.position());
}
}
|
Output:
Array length : 3
Array element : [a, b, c]
charBuffer : [a, b, c]
charbuffer capacity : 3
charbuffer position: 0
wrap(char[ ] array, int offset, int length)
The new buffer will be backed by the given char array. As a consequence, any modifications to the buffer will cause the array to be modified and vice versa. The new buffer’s capacity is determined by array.length, its position will be offset, its limit will be offset + length, and its mark will be undefined. Its backing array will be the given array, and its array offset will be zero.
Syntax:
public static CharBuffer wrap(char[ ] array, int offset, int length)
Parameters: This method takes following parameters:
- array: The array that will back the new buffer.
- offset: The offset of the subarray to be used; must be non-negative and no larger than array.length. The new buffer’s position will be set to this value.
- length: The length of the subarray to be used; must be non-negative and no larger than array.length – offset. The new buffer’s limit will be set to offset + length.
Return Value: This method returns the new char buffer.
Throws: This method throws the IndexOutOfBoundsException(If the preconditions on the offset and length parameters do not hold) .
Below are the examples to illustrate the wrap() method:
Example-1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
char [] cbb = { 'a' , 'b' , 'c' };
System.out.println( "Array length : " + cbb.length);
System.out.println( "\nArray element : "
+ Arrays.toString(cbb));
CharBuffer charBuffer = CharBuffer.wrap(cbb, 0 , cbb.length);
charBuffer.rewind();
System.out.println( "\ncharBuffer : "
+ Arrays.toString(charBuffer.array()));
System.out.println( "\ncharbuffer capacity : "
+ charBuffer.capacity());
System.out.println( "\ncharbuffer position: "
+ charBuffer.position());
}
}
|
Output:
Array length : 3
Array element : [a, b, c]
charBuffer : [a, b, c]
charbuffer capacity : 3
charbuffer position: 0
Examples 2: To demonstrate NullPointerException
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
char [] cbb = { 'a' , 'b' , 'c' };
System.out.println( "Array length : " + cbb.length);
System.out.println( "\nArray element : " + Arrays.toString(cbb));
try {
System.out.println( "\nHere "
+ "offset and length does not hold"
+ " the required condition " );
CharBuffer charBuffer = CharBuffer.wrap(cbb, 1 , cbb.length);
charBuffer.rewind();
System.out.println( "\ncharBuffer : "
+ Arrays.toString(charBuffer.array()));
System.out.println( "\ncharbuffer capacity : "
+ charBuffer.capacity());
System.out.println( "\ncharbuffer position: "
+ charBuffer.position());
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception throws: " + e);
}
}
}
|
Output:
Array length : 3
Array element : [a, b, c]
Here offset and length does not hold the required condition
Exception throws: java.lang.IndexOutOfBoundsException
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...