IntBuffer holds a sequence of integer values to be used in an I/O operation. The IntBuffer class provides the following four categories of operations upon long buffers:
- Absolute and relative get method that read single ints.
- Absolute and relative put methods that write single ints.
- Relative bulk put and get methods that transfer contiguous sequences of ints from an int array or some other int buffer into this buffer and from this buffer into an array.
Int buffers can be created by:
- allocate(): This allocates space for the buffer’s content.
- wrap(): This wraps an existing long array into a buffer.
Most of the methods of IntBuffer class are directly analogous to the methods defined by ByteBuffer.
Class Declaration:
public abstract class IntBuffer
extends Buffer
implements Comparable<IntBuffer>
Methods of IntBuffer class:
Method |
Description |
---|---|
This method allocates a new int buffer. |
|
This method returns the int array that backs this buffer. |
|
This method returns the offset within this buffer’s backing array of the first element of the buffer. |
|
This method creates a new, read-only int buffer that shares this buffer’s content. |
|
This method compacts this buffer. |
|
This method compares this buffer to another. |
|
This method creates a new int buffer that shares this buffer’s content. |
|
This method tells whether this buffer is equal to another object. |
|
This method is a relative get method and returns the int at the buffer’s current position. |
|
This method is an absolute get method and returns the int at the given index. |
|
This method is a relative bulk get method and returns this buffer. |
|
This method relative is a bulk get method and returns this buffer. |
|
This method tells whether this buffer is backed by an accessible int array. |
|
hashCode() |
This method returns the current hash code of this buffer. |
isDirect() |
This method tells whether this int buffer is direct. |
order() |
This method retrieves this buffer’s byte order. |
This method is a relative put method and returns this buffer. |
|
This method is a relative bulk put method and returns this buffer. |
|
This method is a relative bulk put method and returns this buffer. |
|
This method is a relative bulk put method and returns this buffer. |
|
This method is an absolute bulk put method and returns this buffer. |
|
This method creates a new int buffer whose content is a shared subsequence of this buffer’s content. |
|
toString() |
This method returns a string summarizing the state of this buffer. |
This method wraps an int array into a buffer. |
|
This method wraps an int array into a buffer. |
Following are some programs to demonstrate IntBuffer class and its methods:
Example 1:
Java
// Java program to demonstrate // IntBuffer class import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the IntBuffer int Capacity = 10 ; // Creating the IntBuffer // creating object of intbuffer // and allocating size capacity IntBuffer ib = IntBuffer.allocate(Capacity); // putting the value in intbuffer ib.put( 100 ); ib.put( 2 , 9 ); System.out.println( "IntBuffer: " + Arrays.toString(ib.array())); } } |
IntBuffer: [100, 0, 9, 0, 0, 0, 0, 0, 0, 0]
Example 2:
Java
// Java program to demonstrate // IntBuffer class import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the IntBuffer int capacity = 10 ; // Creating the IntBuffer try { // creating object of Intbuffer // and allocating size capacity IntBuffer ib = IntBuffer.allocate(capacity); // putting the value in Intbuffer ib.put( 4 ); ib.put( 2 , 9 ); ib.rewind(); // checking IntBuffer ib is backed by array or // not boolean isArray = ib.hasArray(); // checking if else condition if (isArray) System.out.println( "IntBuffer ib is" + " backed by array" ); else System.out.println( "IntBuffer ib is" + " not backed by any array" ); } catch (IllegalArgumentException e) { System.out.println( "IllegalArgumentException catched" ); } catch (ReadOnlyBufferException e) { System.out.println( "ReadOnlyBufferException catched" ); } } } |
IntBuffer ib is backed by array