ByteBuffer wrap() methods in Java with Examples

wrap(byte[] array)

The wrap() method of java.nio.ByteBuffer Class is used to wraps a byte array into a buffer. The new buffer will be backed by the given byte array, i.e., modifications to the buffer will cause the array to be modified and vice versa. The new buffer’s capacity and limit will be array.length, its position will be zero, its mark will be undefined, and its byte order will be BIG_ENDIAN. It’s backing array will be the given array, and its array offset will be zero.

Syntax:

public static ByteBuffer wrap(byte[] array)

Parameters: This method takes array which is the array that will back this buffer as a parameter.

Return Value: This method returns the the new byte buffer.

Below are the examples to illustrate the wrap() method:

Examples 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 byte array
        byte[] bb = { 10, 20, 30 };
  
        // print the byte array length
        System.out.println("Array length: "
                           + bb.length);
  
        // print the byte array element
        System.out.println("\nArray element: "
                           + Arrays.toString(bb));
  
        // wrap the byte array into floatBuffer
        // using wrap() method
        ByteBuffer byteBuffer = ByteBuffer.wrap(bb);
  
        // Rewind the bytebuffer
        byteBuffer.rewind();
  
        // print the byte buffer
        System.out.println("\nbyteBuffer: "
                           + Arrays.toString(
                                 byteBuffer.array()));
  
        // print the byteBuffer capacity
        System.out.println("\nbytebuffer capacity: "
                           + byteBuffer.capacity());
  
        // print the byteBuffer position
        System.out.println("\nbytebuffer position:  "
                           + byteBuffer.position());
    }
}

chevron_right


Output:

Array length: 3

Array element: [10, 20, 30]

byteBuffer: [10, 20, 30]

bytebuffer capacity: 3

bytebuffer position:  0

wrap(byte[] array, int offset, int length)

The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer’s capacity will be array.length, its position will be offset, its limit will be offset + length, its mark will be undefined, and its byte order will be BIG_ENDIAN. Its backing array will be the given array, and its array offset will be zero.

Syntax:

public static ByteBuffer 
    wrap(byte[] 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 byte buffer.

Exceptions: 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:

Examples 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// wrap() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Declare and initialize the byte array
            byte[] bb = { 10, 20, 30 };
  
            // print the byte array length
            System.out.println("Array length: "
                               + bb.length);
  
            // print the byte array element
            System.out.println("\nArray element: "
                               + Arrays.toString(bb));
  
            // wrap the byte array into floatBuffer
            // using wrap() method
            ByteBuffer byteBuffer
                = ByteBuffer.wrap(bb, 0,
                                  bb.length);
  
            // Rewind the bytebuffer
            byteBuffer.rewind();
  
            // print the byte buffer
            System.out.println("\nbyteBuffer: "
                               + Arrays.toString(
                                     byteBuffer.array()));
  
            // print the byteBuffer capacity
            System.out.println("\nbytebuffer capacity: "
                               + byteBuffer.capacity());
  
            // print the byteBuffer position
            System.out.println("\nbytebuffer position:  "
                               + byteBuffer.position());
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("\npreconditions on the"
                               + " offset and length parameters"
                               + " do not hold");
            System.out.println("Exception throws:  " + e);
        }
    }
}

chevron_right


Output:

Array length: 3

Array element: [10, 20, 30]

byteBuffer: [10, 20, 30]

bytebuffer capacity: 3

bytebuffer position:  0

Examples 2: To demonstrate IndexOutOfBoundsException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// wrap() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Declare and initialize the byte array
            byte[] bb = { 10, 20, 30 };
  
            // print the byte array length
            System.out.println("Array length: "
                               + bb.length);
  
            // print the byte array element
            System.out.println("\nArray element: "
                               + Arrays.toString(bb));
  
            // wrap the byte array into floatBuffer
            // using wrap() method
            ByteBuffer byteBuffer
                = ByteBuffer.wrap(bb, 1,
                                  bb.length);
  
            // Rewind the bytebuffer
            byteBuffer.rewind();
  
            // print the byte buffer
            System.out.println("\nbyteBuffer: "
                               + Arrays.toString(
                                     byteBuffer.array()));
  
            // print the byteBuffer capacity
            System.out.println("\nbytebuffer capacity: "
                               + byteBuffer.capacity());
  
            // print the byteBuffer position
            System.out.println("\nbytebuffer position:  "
                               + byteBuffer.position());
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("\npreconditions on the"
                               + " offset and length parameters"
                               + " do not hold");
            System.out.println("Exception throws:  " + e);
        }
    }
}

chevron_right


Output:

Array length: 3

Array element: [10, 20, 30]

preconditions on the offset and length parameters do not hold
Exception throws:  java.lang.IndexOutOfBoundsException


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.