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 BufferUnderflowException – If the buffer’s current position is not smaller than its limit, then this exception is thrown.
Below are the examples to illustrate the get() method:
Examples 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 5 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.put(( byte ) 20 );
bb.put(( byte ) 30 );
bb.put(( byte ) 40 );
bb.rewind();
System.out.println( "Original ByteBuffer: "
+ Arrays.toString(bb.array()));
byte value = bb.get();
System.out.println( "\nByte Value: " + value);
byte value1 = bb.get();
System.out.print( "\nNext Byte Value: " + value1);
}
catch (IllegalArgumentException e) {
System.out.println( "\nException Thrown : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println( "\nException Thrown : " + e);
}
catch (BufferUnderflowException e) {
System.out.println( "\nException Thrown : " + e);
}
}
}
|
Output:
Original ByteBuffer: [20, 30, 40, 0, 0]
Byte Value: 20
Next Byte Value: 30
Examples 2:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.put(( byte ) 20 );
bb.put(( byte ) 30 );
System.out.println( "Original ByteBuffer: "
+ Arrays.toString(bb.array()));
byte value = bb.get();
System.out.println( "\nByte Value: " + value);
System.out.print( "\nsince the buffer current position is incremented" );
System.out.print( " to greater than its limit " );
byte value1 = bb.get();
System.out.print( "\nNext Byte Value: " + value1);
}
catch (IllegalArgumentException e) {
System.out.println( "\nIllegalArgumentException catched" );
}
catch (ReadOnlyBufferException e) {
System.out.println( "\nReadOnlyBufferException catched" );
}
catch (BufferUnderflowException e) {
System.out.println( "\nException throws : " + e);
}
}
}
|
Output:
Original ByteBuffer: [20, 30, 0]
Byte Value: 0
since the buffer current position is incremented to greater than its limit
Exception throws : java.nio.BufferUnderflowException
get(int index)
The get(int index) method of ByteBuffer is used to read the article at a specified index.
Syntax :
public abstract byte get(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 Byte 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 get(int index) method:
Examples 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.put(( byte ) 20 );
bb.put(( byte ) 30 );
bb.put(( byte ) 40 );
System.out.println( "Original ByteBuffer: "
+ Arrays.toString(bb.array()));
byte value0 = bb.get( 0 );
System.out.println( "\nByte Value at index 0: " + value0);
byte value1 = bb.get( 1 );
System.out.println( "\nByte Value at index 1: " + value1);
byte value2 = bb.get( 2 );
System.out.println( "\nByte Value at index 2: " + value2);
}
catch (IllegalArgumentException e) {
System.out.println( "\nIllegalArgumentException catched" );
}
catch (IndexOutOfBoundsException e) {
System.out.println( "\nReadOnlyBufferException catched" );
}
catch (BufferUnderflowException e) {
System.out.println( "\nException throws : " + e);
}
}
}
|
Output:
Original ByteBuffer: [20, 30, 40]
Byte Value at index 0: 20
Byte Value at index 1: 30
Byte Value at index 2: 40
Examples 2:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 3 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.put(( byte ) 20 );
bb.put(( byte ) 30 );
bb.put(( byte ) 40 );
System.out.println( "Original ByteBuffer: "
+ Arrays.toString(bb.array()));
byte value0 = bb.get( 0 );
System.out.println( "\nByte Value at index 0: " + value0);
byte value1 = bb.get( 1 );
System.out.println( "\nByte Value at index 1: " + value1);
System.out.println( "\nTrying to get the byte"
+ " of index greater than its limit " );
byte value2 = bb.get( 4 );
System.out.println( "\nByte Value at index 4: " + value2);
}
catch (IndexOutOfBoundsException e) {
System.out.println( "\nException throws : " + e);
}
catch (BufferUnderflowException e) {
System.out.println( "\nException throws : " + e);
}
}
}
|
Output:
Original ByteBuffer: [20, 30, 40]
Byte Value at index 0: 20
Byte Value at index 1: 30
Trying to get the byte of index greater than its limit
Exception throws : java.lang.IndexOutOfBoundsException