Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaByteBuffer wrap() method in Java with Examples

ByteBuffer wrap() method 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; 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.length, its position will be zero, and its mark will be undefined. It’s backing array will be the given array, and its array offset will be zero.

Syntax:

public static ByteBuffer wrap(float[] array)

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

Return Value: This method returns the new byte buffer.

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

Examples 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 byte array
        byte[] bbb = { 10, 20, 30 };
  
        // print the byte array length
        System.out.println("Array length : "
                           + bbb.length);
  
        // print the byte array element
        System.out.println("\nArray element : "
                           + Arrays.toString(bbb));
  
        // wrap the byte array into byteBuffer
        // using wrap() method
        ByteBuffer byteBuffer = ByteBuffer.wrap(bbb);
  
        // 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());
    }
}


Output:

Array length : 3

Array element : [10, 20, 30]

byteBuffer : [10, 20, 30]

byteBuffer capacity : 3

byteBuffer position:  0

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#wrap-byte:A-

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, and its mark will be undefined. It’s 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.

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:

Examples 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 byte array
        byte[] bbb = { 10, 20, 30 };
  
        // print the byte array length
        System.out.println("Array length : "
                           + bbb.length);
  
        // print the byte array element
        System.out.println("\nArray element : "
                           + Arrays.toString(bbb));
  
        // wrap the byte array into ByteBuffer
        // using wrap() method
        ByteBuffer byteBuffer = ByteBuffer.wrap(bbb, 0,
                                                bbb.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());
    }
}


Output:

Array length : 3

Array element : [10, 20, 30]

byteBuffer : [10, 20, 30]

byteBuffer capacity : 3

byteBuffer position:  0

Examples 2: To demonstrate NullPointerException




// Java program to demonstrate
// asReadOnlyBuffer() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declare and initialize the byte array
        byte[] bbb = { 10, 20, 30 };
  
        // print the byte array length
        System.out.println("Array length : " + bbb.length);
  
        // print the byte array element
        System.out.println("\nArray element : " + Arrays.toString(bbb));
  
        try {
            // wrap the byte array into byteBuffer
            // using wrap() method
            System.out.println("\nHere "
                               + "offset and length does not hold"
                               + " the required condition ");
            ByteBuffer byteBuffer = ByteBuffer.wrap(bbb,
                                                    1,
                                                    bbb.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("Exception throws:  " + e);
        }
    }
}


Output:

Array length : 3

Array element : [10, 20, 30]

Here offset and length does not hold the required condition 
Exception throws:  java.lang.IndexOutOfBoundsException

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#wrap-byte:A-int-int-

RELATED ARTICLES

Most Popular

Recent Comments