ByteBuffer getInt() method in Java with Examples
Last Updated :
17 Jun, 2019
getInt()
The
getInt() method of
java.nio.ByteBuffer class is used to read the next four bytes at this buffer's current position, composing them into an int value according to the current byte order, and then increments the position by four.
Syntax:
public abstract int getInt()
Return Value: This method returns the int value at the buffer's current position
Throws: This method throws
BufferUnderflowException – If there are fewer than four bytes remaining in this buffer.
Below are the examples to illustrate the getInt() method:
Examples 1:
Java
// Java program to demonstrate
// getInt() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer
int capacity = 12;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity);
// putting the int value in the bytebuffer
bb.asIntBuffer()
.put(10)
.put(20)
.put(30);
// rewind the Bytebuffer
bb.rewind();
// print the ByteBuffer
System.out.println("Original ByteBuffer: ");
for (int i = 1; i <= capacity / 4; i++)
System.out.print(bb.getInt() + " ");
// rewind the Bytebuffer
bb.rewind();
// Reads the Int at this buffer's current position
// using getInt() method
int value = bb.getInt();
// print the int value
System.out.println("\n\nByte Value: " + value);
// Reads the int at this buffer's next position
// using getInt() method
int value1 = bb.getInt();
// print the int value
System.out.println("Next Byte Value: " + value1);
}
catch (BufferUnderflowException e) {
System.out.println("\nException Thrown : " + e);
}
}
}
Output:
Original ByteBuffer:
10 20 30
Byte Value: 10
Next Byte Value: 20
Examples 2:
Java
// Java program to demonstrate
// getInt() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer
int capacity = 8;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity);
// putting the int value in the bytebuffer
bb.asIntBuffer()
.put(10)
.put(20);
// rewind the Bytebuffer
bb.rewind();
// print the ByteBuffer
System.out.println("Original ByteBuffer: ");
for (int i = 1; i <= capacity / 4; i++)
System.out.print(bb.getInt() + " ");
// rewind the Bytebuffer
bb.rewind();
// Reads the Int at this buffer's current position
// using getInt() method
int value = bb.getInt();
// print the int value
System.out.println("\n\nByte Value: " + value);
// Reads the int at this buffer's next position
// using getInt() method
int value1 = bb.getInt();
// print the int value
System.out.println("Next Byte Value: " + value1);
// Reads the int at this buffer's next position
// using getInt() method
int value2 = bb.getInt();
}
catch (BufferUnderflowException e) {
System.out.println("\nthere are fewer than "
+ "four bytes remaining in this buffer");
System.out.println("Exception Thrown : " + e);
}
}
}
Output:
Original ByteBuffer:
10 20
Byte Value: 10
Next Byte Value: 20
there are fewer than four bytes remaining in this buffer
Exception Thrown : java.nio.BufferUnderflowException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getInt--
getInt(int index)
The
getInt(int index) method of
ByteBuffer is used to read four bytes at the given index, composing them into a int value according to the current byte order.
Syntax :
public abstract int getInt(int index)
Parameters: This method takes index (The index from which the Byte will be read) as a parameter.
Return Value: This method returns The int value at the given index.
Exception: This method throws
IndexOutOfBoundsException. If index is negative or not smaller than the buffer’s limit this exception is thrown.
Below are the examples to illustrate the
getInt(int index) method:
Examples 1:
Java
// Java program to demonstrate
// getInt() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer
int capacity = 8;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity);
// putting the int value in the bytebuffer
bb.asIntBuffer()
.put(10)
.put(20);
// rewind the Bytebuffer
bb.rewind();
// print the ByteBuffer
System.out.println("Original ByteBuffer: ");
for (int i = 1; i <= capacity / 4; i++)
System.out.print(bb.getInt() + " ");
// rewind the Bytebuffer
bb.rewind();
// Reads the Int at this buffer's current position
// using getInt() method
int value = bb.getInt(0);
// print the int value
System.out.println("\n\nByte Value: " + value);
// Reads the int at this buffer's next position
// using getInt() method
int value1 = bb.getInt(4);
// print the int value
System.out.println("Next Byte Value: " + value1);
}
catch (IndexOutOfBoundsException e) {
System.out.println("\nindex is negative or smaller "
+ "than the buffer's limit, minus seven");
System.out.println("Exception Thrown : " + e);
}
}
}
Output:
Original ByteBuffer:
10 20
Byte Value: 10
Next Byte Value: 20
Examples 2:
Java
// Java program to demonstrate
// getInt() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer
int capacity = 8;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity);
// putting the int value in the bytebuffer
bb.asIntBuffer()
.put(10)
.put(20);
// rewind the Bytebuffer
bb.rewind();
// print the ByteBuffer
System.out.println("Original ByteBuffer: ");
for (int i = 1; i <= capacity / 4; i++)
System.out.print(bb.getInt() + " ");
// rewind the Bytebuffer
bb.rewind();
// Reads the Int at this buffer's current position
// using getInt() method
int value = bb.getInt(0);
// print the int value
System.out.println("\n\nByte Value: " + value);
// Reads the int at this buffer's next position
// using getInt() method
int value1 = bb.getInt(7);
// print the int value
System.out.println("Next Byte Value: " + value1);
}
catch (IndexOutOfBoundsException e) {
System.out.println("\nindex is negative or smaller"
+ " than the buffer's limit, minus seven");
System.out.println("Exception Thrown : " + e);
}
}
}
Output:
Original ByteBuffer:
10 20
Byte Value: 10
index is negative or smaller than the buffer's limit, minus seven
Exception Thrown : java.lang.IndexOutOfBoundsException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getInt-int-
Similar Reads
ByteBuffer get() method in Java with Examples get() The get() method of java.nio.ByteBuffer class is used to read the byte at the buffer's current position, and then increments the position. Syntax : public abstract byte get() Return Value: This method returns the byte at the buffer's current position. Throws: This method throws BufferUnderflow
6 min read
ByteBuffer getLong() method in Java with Examples getLong() The getLong() method of java.nio.ByteBuffer class is used to read the next eight bytes at this buffer's current position, composing them into a long value according to the current byte order, and then increments the position by eight. Syntax: public abstract long getLong() Return Value: Th
5 min read
ByteBuffer getFloat() method in Java with Examples getFloat() The getFloat() method of java.nio.ByteBuffer class is used to read the next four bytes at this buffer's current position, composing them into a float value according to the current byte order, and then increments the position by four. Syntax: public abstract float getFloat() Return Value:
5 min read
ByteBuffer getShort() method in Java with Examples getShort() The getShort() method of java.nio.ByteBuffer class is used to read the next two bytes at this buffer's current position, composing them into a short value according to the current byte order, and then increments the position by two. Syntax: public abstract short getShort() Return Value: T
5 min read
ByteBuffer getChar() method in Java with Examples getChar() The getChar() method of java.nio.ByteBuffer class is used to get method for reading a char value Reads the next two bytes at this buffer's current position, composing them into a char value according to the current byte order, and then increments the position by two. Syntax: public abstrac
6 min read
ByteBuffer getDouble() method in Java with Examples getDouble() The getDouble() method of java.nio.ByteBuffer class is used to read the next eight bytes at this buffer's current position, composing them into a double value according to the current byte order, and then increments the position by eight. Syntax: public abstract double getDouble() Return
6 min read