ByteBuffer getLong() method in Java with Examples
Last Updated :
17 Jun, 2019
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: This method returns the long 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 getLong() method:
Examples 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 16 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.asLongBuffer()
.put( 1233003 )
.put( 2292292 );
bb.rewind();
System.out.println( "Original ByteBuffer: " );
for ( int i = 1 ; i <= capacity / 8 ; i++)
System.out.print(bb.getLong() + " " );
bb.rewind();
long value = bb.getLong();
System.out.println( "\n\nByte Value: " + value);
long value1 = bb.getLong();
System.out.print( "\nNext Byte Value: " + value1);
}
catch (BufferUnderflowException e) {
System.out.println( "\nException Thrown : " + e);
}
}
}
|
Output:
Original ByteBuffer:
1233003 2292292
Byte Value: 1233003
Next Byte Value: 2292292
Examples 2:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 16 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.asLongBuffer()
.put( 1233003 )
.put( 2292292 );
bb.rewind();
System.out.println( "Original ByteBuffer: " );
for ( int i = 1 ; i <= capacity / 8 ; i++)
System.out.print(bb.getLong() + " " );
bb.rewind();
long value = bb.getLong();
System.out.println( "\n\nByte Value: " + value);
long value1 = bb.getLong();
System.out.print( "\nNext Byte Value: " + value1);
long value2 = bb.getLong();
}
catch (BufferUnderflowException e) {
System.out.println( "\nthere are fewer than "
+ "eight bytes remaining in this buffer" );
System.out.println( "Exception Thrown : " + e);
}
}
}
|
Output:
Original ByteBuffer:
1233003 2292292
Byte Value: 1233003
Next Byte Value: 2292292
there are fewer than eight bytes remaining in this buffer
Exception Thrown : java.nio.BufferUnderflowException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getLong–
getLong(int index)
The getLong(int index) method of ByteBuffer is used to read four bytes at the given index, composing them into a float value according to the current byte order.
Syntax:
public abstract long getLong(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 long 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 getLong(int index) method:
Examples 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 16 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.asLongBuffer()
.put( 1233003 )
.put( 2292292 );
bb.rewind();
System.out.println( "Original ByteBuffer: " );
for ( int i = 1 ; i <= capacity / 8 ; i++)
System.out.print(bb.getLong() + " " );
bb.rewind();
long value = bb.getLong( 0 );
System.out.println( "\n\nByte Value: " + value);
long value1 = bb.getLong( 8 );
System.out.print( "\nNext 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:
1233003 2292292
Byte Value: 1233003
Next Byte Value: 2292292
Examples 2:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 16 ;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.asLongBuffer()
.put( 1233003 )
.put( 2292292 );
bb.rewind();
System.out.println( "Original ByteBuffer: " );
for ( int i = 1 ; i <= capacity / 8 ; i++)
System.out.print(bb.getLong() + " " );
bb.rewind();
long value = bb.getLong( 0 );
System.out.println( "\n\nByte Value: " + value);
long value1 = bb.getLong( 11 );
System.out.print( "\nNext 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:
1233003 2292292
Byte Value: 1233003
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#getLong-int-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...