IntBuffer class Methods in Java Part -I

java.nio.IntBuffer

An int buffer.

1. Absolute and relative get and put methods that read and write single ints.

2. Absolute and relative bulk get methods that transfer contiguous sequences of ints from this buffer into an array.

3. Absolute and Relative bulk put methods that transfer contiguous sequences of ints from an int array or some other int buffer into this buffer.

4.  A method for compacting an int buffer.


Some of the Methods of  IntBuffer class.


IntBuffer.allocate(int)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 content.


capacity()This method returns this buffer's capacity.


clear()This method clears this buffer.


compact()This method compacts this buffer.


compareTo(IntBuffer) This method compares this buffer to another.


duplicate()This method creates a new int buffer that shares this buffer's content.


equals(Object)This method tells whether or not this buffer is equal to another object.


flip()This method flips this buffer.


get()This method reads the int at this buffer'scurrent position and then increments the position.


get(int)This method reads the int at the given index.


get(int[])This method transfers ints from this buffer into the given destination array.


get(int, int[])This method transfers ints from this buffer into the given destination array.


get(int[], int, int)This method transfers ints from this buffer into the given destination array.


get(int, int[], int, int)This method transfers length ints from this buffer into the given array, starting at the given index in this buffer and at the given offset in the array.


getClass()This method returns the runtime class of this Object.


hasArray()This method tells whether or not this buffer is backed by an accessible int array.


hashCode()This method returns the current hash code of this buffer.


hasRemaining()This method tells whether there are any elements between the current position and the limit.


isDirect()This method tells whether or not this int buffer is direct.


Some more Methods of IntBuffer


IntBuffer isDirect() in Java

isDirect(): This method is available in java.nio.IntBuffer class of Java.

Syntax:

boolean java.nio.IntBuffer.isDirect()

This method tells whether or not this int buffer is direct.

Parameters: NA

Returns: true if, and only if, this buffer is direct.

Exceptions: NA

Approach

Java

import java.nio.IntBuffer;

public class IntBufferisDirect {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };
        IntBuffer lb = IntBuffer.wrap(array);

        System.out.println(lb.isDirect());
    }
}

Output:

false


IntBuffer hasRemaining() in Java

hasRemaining(): This method is available in java.nio.Buffer class of Java.

Syntax:

boolean java.nio.Buffer.hasRemaining()

This method tells whether there are any elements between the current position and the limit.

Parameters: NA

Returns: true if, and only if, there is at least one element remaining in this buffer.

Exceptions: NA

Approach

Java

import java.nio.IntBuffer;

public class IntBufferhasRemaining {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };

        IntBuffer lb = IntBuffer.wrap(array);

        System.out.println(lb.hasRemaining());
    }
}

Output:

true


IntBuffer hashCode() in Java

hashCode(): This method is available in java.nio.IntBuffer class of Java.

Syntax:

int java.nio.IntBuffer.hashCode()

This method returns the current hash code of this buffer.

Note: The hash code of an int buffer depends only upon its remaining elements; that is, upon the elements from position() up to, and including, the element at the limit() - 1.

Parameters: NA

Returns: The current hash code of this buffer.

Exceptions: NA

Approach

Java

import java.nio.IntBuffer;

public class IntBufferhashCode {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };

        IntBuffer lb = IntBuffer.wrap(array);

        System.out.println(lb.hashCode());
    }
}

Output:

1045631


IntBuffer hasArray() in Java

hasArray(): This method is available in java.nio.IntBuffer class of Java.

Syntax:

boolean java.nio.IntBuffer.hasArray()

This method tells whether or not this buffer is backed by an accessible int array.

Parameters: NA

Returns: true if, and only if, this buffer is backed by an array and is not read-only.

Exceptions: NA

Approach

Java

import java.nio.IntBuffer;

public class IntBufferhasArray {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };

        IntBuffer lb = IntBuffer.wrap(array);

        System.out.println(lb.hasArray());
    }
}

Output:

true


IntBuffer getClass() in Java

getClass(): This method is available in java.lang.Object class of Java.

Syntax:

Class<? extends IntBuffer> java.lang.Object.getClass()

This method returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.

Parameters: NA

Returns: The Class object that represents the runtime class of this object.

Exceptions: NA

Approach

Java

import java.nio.IntBuffer;

public class IntBuffergetClass {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };
        IntBuffer lb = IntBuffer.wrap(array);

        System.out.println(lb.getClass());
    }
}

Output:

class java.nio.HeapIntBuffer


IntBuffer get(int, int[], int, int) in Java

get(int, int[], int, int): This method is available in java.nio.IntBuffer class of Java.

Syntax:

IntBuffer java.nio.IntBuffer.get(int index, int[] dst, int offset, int length)

This method takes four parameters one of type int array and the other three are of type int as its parameters. This method transfers length ints from this buffer into the given array, starting at the given index in this buffer and at the given offset in the array.

Note: The position of this buffer is unchanged.

Parameters: Four parameters are required for this method.

index: The index in this buffer from which the first int will be read; must be non-negative and less than the limit().

dst: The destination array.

offset: The offset within the array of the first int to be written; must be non-negative and less than dst.length.

length: The number of ints to be written to the given array; must be non-negative and no larger than the smaller of limit() - index and dst.length - offset.

Returns: This buffer.

Throws:

IndexOutOfBoundsException - If the preconditions on the index, offset, and length parameters do not hold

Approach 1: When no exceptions

Java

import java.nio.IntBuffer;

public class IntBufferget6 {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };
        IntBuffer lb = IntBuffer.wrap(array);

        int dst[] = { 1, 2, 3, 4 };
        int offset = 0;
        int length = 2;
        int index = 0;
        System.out.println(lb.get(index, dst, offset, length));
    }
}

Output:

java.nio.HeapIntBuffer[pos=0 lim=4 cap=4]


Approach 2: IndexOutOfBoundsException 

Java

import java.nio.IntBuffer;

public class IntBufferget6 {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };
        IntBuffer lb = IntBuffer.wrap(array);

        int dst[] = { 1, 2, 3, 4 };
        int offset = 0;
        int length = 2;
        int index = -1;
        System.out.println(lb.get(index, dst, offset, length));
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [-1, -1 + 2) out of bounds for length 4 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:82) at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:343) at java.base/java.util.Objects.checkFromIndexSize(Objects.java:411) at java.base/java.nio.HeapIntBuffer.get(HeapIntBuffer.java:193)


Some more get methods.


get()


get(int)


get(int[])


get(int, int[])


get(int[], int, int)


IntBuffer get(int[], int, int) in Java

get(int[], int, int): This method is available in java.nio.IntBuffer class of Java.

Syntax:

IntBuffer java.nio.IntBuffer.get(int[] dst, int offset, int length)

This method takes three arguments one of type int array and the other two are of type int as its parameter. This method transfers ints from this buffer into the given destination array.

Parameters: Three parameters are required for this method.

dst: The array into which ints are to be written.

offset: The offset within the array of the first int to be written; must be non-negative and no larger than dst.length

length: The maximum number of ints to be written to the given array; must be non-negative and no larger than dst.length - offset.

Returns: This buffer.

Throws:

1. BufferUnderflowException - If there are fewer than length ints remaining in this buffer.

2. IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold

Approach 1: When no exceptions

Java

import java.nio.IntBuffer;

public class IntBufferget5 {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };
        IntBuffer lb = IntBuffer.wrap(array);

        int dst[] = { 1, 2, 3, 5, 6 };
        int offset = 0;
        int length = 4;
        System.out.println(lb.get(dst, offset, length));
    }
}

Output:

java.nio.HeapIntBuffer[pos=4 lim=4 cap=4]


Approach 2: BufferUnderflowException 

Java

import java.nio.IntBuffer;

public class IntBufferget5 {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3 };
        IntBuffer lb = IntBuffer.wrap(array);

        int dst[] = { 1, 2, 3, 5, 6 };
        int offset = 0;
        int length = 4;
        System.out.println(lb.get(dst, offset, length));
    }
}

Output:

Exception in thread "main" java.nio.BufferUnderflowException at java.base/java.nio.HeapIntBuffer.get(HeapIntBuffer.java:185)


Approach 3: IndexOutOfBoundsException

Java

import java.nio.IntBuffer;

public class IntBufferget5 {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };
        IntBuffer lb = IntBuffer.wrap(array);

        int dst[] = { 1, 2, 3 };
        int offset = 0;
        int length = 4;
        System.out.println(lb.get(dst, offset, length));
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [0, 0 + 4) out of bounds for length 3 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:82) at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:343) at java.base/java.util.Objects.checkFromIndexSize(Objects.java:411) at java.base/java.nio.HeapIntBuffer.get(HeapIntBuffer.java:182)


Some more get methods.


get()


get(int)


get(int[])


get(int, int[])


get(int, int[], int, int)


IntBuffer get(int, int[]) in Java

get(int, int[]): This method is available in java.nio.IntBuffer class of Java.

Syntax:

IntBuffer java.nio.IntBuffer.get(int index, int[] dst)

This method takes two arguments one of type int and another of type int array as its parameter. This method transfers ints from this buffer into the given destination array.

Note: The position of this buffer is unchanged.

Parameters: Two parameters are required for this method.

index: The index in this buffer from which the first int will be read; must be non-negative and less than the limit().

dst: The destination array.

Returns: This buffer.

Throws:

IndexOutOfBoundsException - If an index is negative, not smaller than limit(),or limit() - index < dst.length

Approach 1: When no exceptions

Java

import java.nio.IntBuffer;

public class IntBufferget4 {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };
        IntBuffer lb = IntBuffer.wrap(array);

        int dst[] = { 1, 2, 3 };
        int index = 0;
        System.out.println(lb.get(index, dst));
    }
}

Output:

java.nio.HeapIntBuffer[pos=0 lim=4 cap=4]


Approach 2: IndexOutOfBoundsException

Java

import java.nio.IntBuffer;

public class IntBufferget4 {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };
        IntBuffer lb = IntBuffer.wrap(array);

        int dst[] = { 1, 2, 3 };
        int index = -1;
        System.out.println(lb.get(index, dst));
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [-1, -1 + 3) out of bounds for length 4 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:82) at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:343) at java.base/java.util.Objects.checkFromIndexSize(Objects.java:411) at java.base/java.nio.HeapIntBuffer.get(HeapIntBuffer.java:193) at java.base/java.nio.IntBuffer.get(IntBuffer.java:898)


Some more get methods.


get()


get(int)


get(int[])


get(int[], int, int)


get(int, int[], int, int)


IntBuffer get(int[]) in Java

get(int[]): This method is available in java.nio.IntBuffer class of Java.

Syntax:

IntBuffer java.nio.IntBuffer.get(int[] dst)

This method takes one argument of type int array as its parameter. This method transfers ints from this buffer into the given destination array.

Parameters: One parameter is required for this method.

dst: The destination array.

Returns: This buffer.

Throws:

BufferUnderflowException - If there are fewer than length ints remaining in this buffer.

Approach 1: When no exceptions.

Java

import java.nio.IntBuffer;

public class IntBufferget3 {
    public static void main(String[] args) {

        int array[] = { 1, 2, 78, 1991, 18 };
        IntBuffer lb = IntBuffer.wrap(array);

        int dst[] = { 1, 2, 3 };
        System.out.println(lb.get(dst));
    }
}

Output:

java.nio.HeapIntBuffer[pos=3 lim=5 cap=5]


Approach 2: BufferUnderflowException 

Java

import java.nio.IntBuffer;

public class IntBufferget3 {
    public static void main(String[] args) {

        int array[] = { 1, 2 };
        IntBuffer lb = IntBuffer.wrap(array);

        int dst[] = { 1, 2, 3 };
        System.out.println(lb.get(dst));
    }
}

Output:

Exception in thread "main" java.nio.BufferUnderflowException at java.base/java.nio.HeapIntBuffer.get(HeapIntBuffer.java:185) at java.base/java.nio.IntBuffer.get(IntBuffer.java:814)


Some more get methods.


get()


get(int)


get(int, int[])


get(int[], int, int)


get(int, int[], int, int)


IntBuffer get(int) in Java

get(int): This method is available in java.nio.IntBuffer class of Java.

Syntax:

int java.nio.IntBuffer.get(int index)

This method takes one argument of type int as its parameter. This method reads the int at the given index.

Parameters: One parameter is required for this method.

index: The index from which the int will be read.

Returns: The int at the given index.

Throws:

IndexOutOfBoundsException - If the index is negative or not smaller than the buffer's limit.

Approach 1: When no exceptions

Java

import java.nio.IntBuffer;

public class IntBufferget2 {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };
        IntBuffer lb = IntBuffer.wrap(array);

        int index = 2;
        System.out.println(lb.get(index));
    }

}

Output:

3


Approach 2: IndexOutOfBoundsException 

Java

import java.nio.IntBuffer;

public class IntBufferget2 {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };
        IntBuffer lb = IntBuffer.wrap(array);

        int index = 5;
        System.out.println(lb.get(index));
    }

}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.nio.Buffer.checkIndex(Buffer.java:738) at java.base/java.nio.HeapIntBuffer.get(HeapIntBuffer.java:171)


Some more get methods.


get()


get(int[])


get(int, int[])


get(int[], int, int)


get(int, int[], int, int)


IntBuffer get() in Java

get(): This method is available in java.nio.IntBuffer class of Java.

Syntax:

int java.nio.IntBuffer.get()

This method reads the int at this buffer'scurrent position and then increments the position.

Parameters: NA

Returns: The int at the buffer's current position.

Throws:

BufferUnderflowException - If the buffer's current position is not smaller than its limit.

Approach 1: When no exceptions

Java

import java.nio.IntBuffer;

public class IntBufferget {
    public static void main(String[] args) {

        int array[] = { 12, 34, 56, 78 };
        IntBuffer lb = IntBuffer.wrap(array);

        System.out.println(lb.get());
    }
}

Output:

12


Approach 2: BufferUnderflowException

Java

import java.nio.IntBuffer;

public class IntBufferget {
    public static void main(String[] args) {

        int array[] = {};
        IntBuffer lb = IntBuffer.wrap(array);

        System.out.println(lb.get());
    }
}

Output:

Exception in thread "main" java.nio.BufferUnderflowException at java.base/java.nio.Buffer.nextGetIndex(Buffer.java:694) at java.base/java.nio.HeapIntBuffer.get(HeapIntBuffer.java:166)


Some more get methods.


get(int)


get(int[])


get(int, int[])


get(int[], int, int)


get(int, int[], int, int)