Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaCharBuffer wrap() method in Java

CharBuffer wrap() method in Java

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 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 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 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
RELATED ARTICLES

Most Popular

Recent Comments