IntBuffer wrap() method in Java
Last Updated :
06 Nov, 2019
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int [] ibb = { 1 , 2 , 3 };
System.out.println( "Array length : "
+ ibb.length);
System.out.println( "\nArray element : "
+ Arrays.toString(ibb));
IntBuffer intBuffer = IntBuffer.wrap(ibb);
intBuffer.rewind();
System.out.println( "\nintBuffer : "
+ Arrays.toString(intBuffer.array()));
System.out.println( "\nintbuffer capacity : "
+ intBuffer.capacity());
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int [] ibb = { 1 , 2 , 3 };
System.out.println( "Array length : " + ibb.length);
System.out.println( "\nArray element : "
+ Arrays.toString(ibb));
IntBuffer intBuffer = IntBuffer.wrap(ibb, 0 ,
ibb.length);
intBuffer.rewind();
System.out.println( "\nintBuffer : "
+ Arrays.toString(intBuffer.array()));
System.out.println( "\nintbuffer capacity : "
+ intBuffer.capacity());
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int [] ibb = { 1 , 2 , 3 };
System.out.println( "Array length : "
+ ibb.length);
System.out.println( "\nArray element : "
+ Arrays.toString(ibb));
try {
System.out.println( "\nHere "
+ "offset and length does not hold"
+ " the required condition " );
IntBuffer intBuffer = IntBuffer.wrap(ibb,
1 ,
ibb.length);
intBuffer.rewind();
System.out.println( "\nintBuffer : "
+ Arrays.toString(intBuffer.array()));
System.out.println( "\nintbuffer capacity : "
+ intBuffer.capacity());
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
Similar Reads
IntBuffer slice() method in Java
The slice() method of java.nio.IntBuffer Class is used to creates a new int buffer whose content is a shared subsequence of the given buffer's content. The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and
3 min read
IntBuffer put() methods in Java | Set 1
put(int i) The put(int i) method of java.nio.IntBuffer Class is used to write the given int into the newly created int buffer at the current position, and then increments the position. Syntax : public abstract IntBuffer put(int i) Parameters: This method takes the int value i as a parameter which is
6 min read
IntBuffer array() method in Java
The array() method of java.nio.IntBuffer Class is used to Return the int array that backs this buffer. Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke() the hasArray() method are used before invoking this method in order to ensure
3 min read
IntBuffer equals() method in Java
The equals() method of java.nio.IntBuffer Class is used to check whether or not the given buffer is equal to another object. Two int 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, consi
4 min read
IntBuffer compact() method in Java
The compact() method of java.nio.IntBuffer 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
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 i
4 min read
IntBuffer get() methods in Java | Set 1
get() The get() method of java.nio.IntBuffer Class is used to reads the int at the given buffer's current position, and then increments the position. Syntax : public abstract int get() Return Value: This method returns the int value at the buffer's current position. Throws: This method throws Buffer
5 min read
IntBuffer allocate() method in Java
The allocate() method of java.nio.IntBuffer Class is used to allocate a new int buffer next to the existing buffer. The new buffer's position will be zero. Its limit will be its capacity. Its mark will be undefined. And each of its elements will be initialized to zero. It will have a backing array,
2 min read
IntBuffer hasArray() method in Java
The hasArray() method of java.nio.IntBuffer class is used to ensure whether or not the given buffer is backed by an accessible int 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() met
2 min read
IntBuffer compareTo() method in Java
The compareTo() method of java.nio.IntBuffer class is used to compare one buffer to another. Two int buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of int eleme
4 min read