Open In App

IntBuffer wrap() method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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.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 IntBuffer wrap(int[] array)

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

Return Value: This method returns the new int buffer created.

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 int array
        int[] ibb = { 1, 2, 3 };
  
        // print the int array length
        System.out.println("Array length : "
                           + ibb.length);
  
        // print the int array element
        System.out.println("\nArray element : "
                           + Arrays.toString(ibb));
  
        // wrap the int array into intBuffer
        // using wrap() method
        IntBuffer intBuffer = IntBuffer.wrap(ibb);
  
        // Rewind the intbuffer
        intBuffer.rewind();
  
        // print the int buffer
        System.out.println("\nintBuffer : "
                           + Arrays.toString(intBuffer.array()));
  
        // print the IntBuffer capacity
        System.out.println("\nintbuffer capacity : "
                           + intBuffer.capacity());
  
        // print the IntBuffer position
        System.out.println("\nintbuffer position:  "
                           + intBuffer.position());
    }
}


Output:

Array length : 3

Array element : [1, 2, 3]

intBuffer : [1, 2, 3]

intbuffer capacity : 3

intbuffer position:  0

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

The wrap() method wraps an 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 will be 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 IntBuffer 
    wrap (int[] 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 float 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 int array
        int[] ibb = { 1, 2, 3 };
  
        // print the int array length
        System.out.println("Array length : " + ibb.length);
  
        // print the int array element
        System.out.println("\nArray element : "
                           + Arrays.toString(ibb));
  
        // wrap the int array into intBuffer
        // using wrap() method
        IntBuffer intBuffer = IntBuffer.wrap(ibb, 0,
                                             ibb.length);
  
        // Rewind the intbuffer
        intBuffer.rewind();
  
        // print the int buffer
        System.out.println("\nintBuffer : "
                           + Arrays.toString(intBuffer.array()));
  
        // print the IntBuffer capacity
        System.out.println("\nintbuffer capacity : "
                           + intBuffer.capacity());
  
        // print the IntBuffer position
        System.out.println("\nintbuffer position: "
                           + intBuffer.position());
    }
}


Output:

Array length : 3

Array element : [1, 2, 3]

intBuffer : [1, 2, 3]

intbuffer capacity : 3

intbuffer 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
        int[] ibb = { 1, 2, 3 };
  
        // print the int array length
        System.out.println("Array length : "
                           + ibb.length);
  
        // print the int array element
        System.out.println("\nArray element : "
                           + Arrays.toString(ibb));
  
        try {
            // wrap the int array into intBuffer
            // using wrap() method
            System.out.println("\nHere "
                               + "offset and length does not hold"
                               + " the required condition ");
  
            IntBuffer intBuffer = IntBuffer.wrap(ibb,
                                                 1,
                                                 ibb.length);
  
            // Rewind the intbuffer
            intBuffer.rewind();
  
            // print the int buffer
            System.out.println("\nintBuffer : "
                               + Arrays.toString(intBuffer.array()));
  
            // print the IntBuffer capacity
            System.out.println("\nintbuffer capacity : "
                               + intBuffer.capacity());
  
            // print the IntBuffer position
            System.out.println("\nintbuffer position:  "
                               + intBuffer.position());
        }
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception throws:  " + e);
        }
    }
}


Output:

Array length : 3

Array element : [1, 2, 3]

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


Last Updated : 06 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads