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
// Java program to demonstrate LongBuffer class// Importing input output classesimport java.nio.*;// Importing all utility classesimport java.util.*;// Main Classpublic class GFG { // Main driver method public static void main(String[] args) { // Declaring and initializing variable to // the capacity of the LongBuffer int capacity = 5; // try block to check for exceptions try { // creating object of Longbuffer // and allocating size capacity LongBuffer ib = LongBuffer.allocate(capacity); // Adding elements to the objects of Longbuffer // class // using the pur() method ib.put(9); ib.put(8); ib.put(5); ib.rewind(); // print the original LongBuffer // using standard toString() method System.out.println( "Original LongBuffer: " + Arrays.toString(ib.array())); // Reads the Long at this buffer's current // position using get() method Long value = ib.get(); // Print the Long value System.out.println("Long Value: " + value); // Reads the Long at this buffer's next position // using get() method Long value1 = ib.get(); // Agan, now print the Long value System.out.print("Next Long Value: " + value1); } // Catch blocks to handle the exceptions // Catch block 1 catch (IllegalArgumentException e) { // Print the message when there is illegal // arguments System.out.println( "IllegalArgumentException catched"); } // Catch block 2 catch (ReadOnlyBufferException e) { // Print statement when an exception is encountered System.out.println( "ReadOnlyBufferException catched"); } // Catch block 3 catch (BufferUnderflowException e) { // Print statement when an exception is encountered System.out.println("Exception throws: " + e); } }} |
Output
Original LongBuffer: [9, 8, 5, 0, 0] Long Value: 9 Next Long Value: 8
Example 2:
Java
// Java program to demonstrate LongBuffer class// Importing required librariesimport java.nio.*;import java.util.*;// Main Classpublic class GFG { public static void main(String[] args) { // Declaring and initializing variable to // capacity of the LongBuffer int Capacity = 10; // Creating the LongBuffer // creating object of Longbuffer // and allocating size capacity LongBuffer ib = LongBuffer.allocate(Capacity); // Inserting the value in Longbuffer // Custom entries ib.put(11); ib.put(5, 22); // Print all the elements inside Longbuffer by // use of Arrays.toString() method System.out.println("LongBuffer: " + Arrays.toString(ib.array())); }} |
Output
LongBuffer: [11, 0, 0, 0, 0, 22, 0, 0, 0, 0]



