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);        }    }} |
Â
Â
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()));    }} |
Â
Â
LongBuffer: [11, 0, 0, 0, 0, 22, 0, 0, 0, 0]
Â

… [Trackback]
[…] Find More Information here on that Topic: geeksforgeeks.org/java-nio-longbuffer-class-in-java/ […]
… [Trackback]
[…] Information to that Topic: geeksforgeeks.org/java-nio-longbuffer-class-in-java/ […]