java.nio.LongBuffer Class in Java
LongBuffer holds a sequence of long values to be used in an I/O operation. The LongBuffer class provides the following four categories of operations upon long buffers:
- Absolute and relative get and put methods that read and write single longs.
- Relative bulk get methods that transfer contiguous sequences of longs from this buffer into an array.
- Relative bulk put methods that transfer contiguous sequences of longs from a long array or some other long buffer into this buffer.
- A method for compacting a long buffer.
Long buffers can be created by:
- allocate() method, which allocates space for the buffer’s content,
- wrap() method, which wraps an existing long array into a buffer, or
- by creating a view of an existing byte buffer.
Most of the methods of LongBuffer class are directly analogous to the methods defined by ByteBuffer.
Syntax: Class Declaration
public abstract class LongBuffer
extends Buffer
implements Comparable<LongBuffer>
All of the methods of LongBuffer class are listed below as follows:
Method |
Description |
allocate​(int capacity) |
This method allocates a new long buffer. |
array​() |
This method returns the long 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 long buffer that shares this buffer’s content. |
clear​() |
This method clears this buffer. |
compact​() |
This method compacts this buffer. |
compareTo​(LongBuffer that) |
This method compares this buffer to another. |
duplicate​() |
This method creates a new long buffer that shares this buffer’s content. |
equals​(Object ob) |
This method tells whether or not this buffer is equal to another object. |
flip​() |
This method flips this buffer. |
get​() |
This method is a relative get method and returns the long at the buffer’s current position. |
get​(int index) |
This method is an absolute get method and returns the long at the given index. |
get​(long[] dst) |
This method is a relative bulk get method and returns this buffer. |
get​(long[] dst, int offset, int length) |
This method is a relative bulk get method and returns this buffer. |
hasArray​() |
This method tells whether this buffer is backed by an accessible long array. |
hashCode​() |
This method returns the current hash code of this buffer. |
isDirect​() |
This method tells whether this long buffer is direct. |
limit​(int newLimit) |
This method sets this buffer’s limit. |
mark​() |
This method sets this buffer’s mark at its position. |
order​() |
This method retrieves this buffer’s byte order. |
position​(int newPosition) |
This method sets this buffer’s position. |
put​(int index, long l) |
This method is an absolute put method and returns this buffer. |
put​(long l) |
This method is a relative put method and returns this buffer. |
put​(long[] src) |
This method is a relative bulk put method and returns this buffer. |
put​(long[] src, int offset, int length) |
This method is a relative bulk put method and returns this buffer. |
put​(LongBuffer src) |
This method is a relative bulk put method and returns this buffer. |
reset​() |
This method resets this buffer’s position to the previously-marked position. |
rewind​() |
This method rewinds this buffer. |
slice​() |
This method creates a new long 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​(long[] array) |
This method wraps a long array into a buffer. |
wrap​(long[] array, int offset, int length) |
This method wraps a long array into a buffer. |
Following are some programs to demonstrate LongBuffer class and its methods:
Example 1:
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 5 ;
try {
LongBuffer ib = LongBuffer.allocate(capacity);
ib.put( 9 );
ib.put( 8 );
ib.put( 5 );
ib.rewind();
System.out.println(
"Original LongBuffer: "
+ Arrays.toString(ib.array()));
Long value = ib.get();
System.out.println( "Long Value: " + value);
Long value1 = ib.get();
System.out.print( "Next Long Value: " + value1);
}
catch (IllegalArgumentException e) {
System.out.println(
"IllegalArgumentException catched" );
}
catch (ReadOnlyBufferException e) {
System.out.println(
"ReadOnlyBufferException catched" );
}
catch (BufferUnderflowException e) {
System.out.println( "Exception throws: " + e);
}
}
}
|
Output
Original LongBuffer: [9, 8, 5, 0, 0]
Long Value: 9
Next Long Value: 8
Example 2:
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int Capacity = 10 ;
LongBuffer ib = LongBuffer.allocate(Capacity);
ib.put( 11 );
ib.put( 5 , 22 );
System.out.println( "LongBuffer: "
+ Arrays.toString(ib.array()));
}
}
|
Output
LongBuffer: [11, 0, 0, 0, 0, 22, 0, 0, 0, 0]
Last Updated :
29 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...