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:
Java
// Java program to demonstrate
// wrap() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declare and initialize the char array
char[] cbb = { 'a', 'b', 'c' };
// print the char array length
System.out.println("Array length : " + cbb.length);
// print the char array element
System.out.println("\nArray element : "
+ Arrays.toString(cbb));
// wrap the char array into charBuffer
// using wrap() method
CharBuffer charBuffer = CharBuffer.wrap(cbb);
// Rewind the intbuffer
charBuffer.rewind();
// print the char buffer
System.out.println("\ncharBuffer : "
+ Arrays.toString(charBuffer.array()));
// print the CharBuffer capacity
System.out.println("\ncharbuffer capacity : "
+ charBuffer.capacity());
// print the CharBuffer position
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:
Java
// Java program to demonstrate
// wrap() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declare and initialize the char array
char[] cbb = { 'a', 'b', 'c' };
// print the char array length
System.out.println("Array length : " + cbb.length);
// print the char array element
System.out.println("\nArray element : "
+ Arrays.toString(cbb));
// wrap the char array into charBuffer
// using wrap() method
CharBuffer charBuffer = CharBuffer.wrap(cbb, 0, cbb.length);
// Rewind the intbuffer
charBuffer.rewind();
// print the char buffer
System.out.println("\ncharBuffer : "
+ Arrays.toString(charBuffer.array()));
// print the CharBuffer capacity
System.out.println("\ncharbuffer capacity : "
+ charBuffer.capacity());
// print the CharBuffer position
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
Java
// Java program to demonstrate
// wrap() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declare and initialize the float array
char[] cbb = { 'a', 'b', 'c' };
// print the char array length
System.out.println("Array length : " + cbb.length);
// print the char array element
System.out.println("\nArray element : " + Arrays.toString(cbb));
try {
// wrap the char array into charBuffer
// using wrap() method
System.out.println("\nHere "
+ "offset and length does not hold"
+ " the required condition ");
CharBuffer charBuffer = CharBuffer.wrap(cbb, 1, cbb.length);
// Rewind the intbuffer
charBuffer.rewind();
// print the char buffer
System.out.println("\ncharBuffer : "
+ Arrays.toString(charBuffer.array()));
// print the CharBuffer capacity
System.out.println("\ncharbuffer capacity : "
+ charBuffer.capacity());
// print the CharBuffer position
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
Similar Reads
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 parame
6 min read
CharBuffer slice() method in Java
slice() method of java.nio.charBuffer Class is used to create a new char buffer whose content is a shared subsequence of the given buffer's content. The content of the new buffer will start from this buffer's current position. The new buffer will show the changes made in the buffer's content, and vi
3 min read
CharBuffer get() methods in Java
The get() method of java.nio.CharBuffer Class is used to reads the char at the given buffer's current position, and then increments the position. Syntax: public abstract char get() Return Value: This method returns the char value at the buffer's current position. Exceptions: This method throws Buffe
5 min read
CharBuffer array() method in Java
The array() method of java.nio.CharBuffer Class is used to return the char array that backs this buffer. Any modifications done to this buffer's content will cause the returned array's content also to be modified, and vice versa. hasArray() method is invoked before invoking this method in order to e
3 min read
CharBuffer equals() method in Java
The equals() method of java.nio.CharBuffer Class is used to check whether or not the given buffer is equal to another object. Two char buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, con
4 min read
CharBuffer compact() method in Java
The compact() method of java.nio.CharBuffer Class is used to compact the given buffer. The values between the buffer's current position and its limit are copied to the beginning of the buffer. The buffer's position is then set to n+1 and its limit is set to its capacity. The buffer's position is set
3 min read
IntBuffer wrap() method in Java
wrap(int[] array) The wrap() method of java.nio.IntBuffer Class is used to wrap a int array into a buffer. The new buffer will be backed by the given int array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will be array.
4 min read
CharBuffer hasArray() method in Java
The hasArray() method of java.nio.CharBuffer class is used to ensure whether or not the given buffer is backed by an accessible char array. It returns true if there is an accessible backing array to this buffer, else it returns false. If this method returns true, then the array() and arrayOffset() m
2 min read
CharBuffer duplicate() method in Java
The duplicate() method of java.nio.CharBuffer Class is used to Create a new char buffer that shares the given buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, li
3 min read
CharBuffer compareTo() method in Java
compareTo() method of java.nio.charBuffer class is used to compare one buffer to another. Two char buffers are compared by comparing their sequences of remaining elements lexicographically, without considering the starting position of each sequence within its corresponding buffer. Pairs of char elem
4 min read