Saturday, December 28, 2024
Google search engine
HomeLanguagesJavajava.nio.IntBuffer Class in Java

java.nio.IntBuffer Class in Java

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

allocate(int capacity)

This method allocates a new int buffer.

array()

This method returns the int array that backs this buffer. 

arrayOffset()

This method returns the offset within this buffer’s backing array of the first element of the buffer.

asReadOnlyBuffer()

This method creates a new, read-only int buffer that shares this buffer’s content.

compact()

This method compacts this buffer.

compareTo(IntBuffer that)

This method compares this buffer to another.

duplicate()

This method creates a new int buffer that shares this buffer’s content.

equals(Object ob)

This method tells whether this buffer is equal to another object.

get()

This method is a relative get method and returns the int at the buffer’s current position.

get(int index)

This method is an absolute get method and returns the int at the given index.

get(int[] dst)

This method is a relative bulk get method and returns this buffer.

get(int[] dst, int offset, int length)

This method relative is a bulk get method and returns this buffer.

hasArray()

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.

put(int i)

This method is a relative put method and returns this buffer.

put(int[] src)

This method is a relative bulk put method and returns this buffer.

put(int[] src, int offset, int length)

This method is a relative bulk put method and returns this buffer.

put(IntBuffer src)

This method is a relative bulk put method and returns this buffer.

put(int index, int i)

This method is an absolute bulk put method and returns this buffer.

slice()

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.

wrap(int[] array)

This method wraps an int array into a buffer.

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

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()));
    }
}


Output

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");
        }
    }
}


Output

IntBuffer ib is backed by array
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments