LongBuffer class in java
java.nio.LongBuffer is a class in the Java NIO (New Input/Output) framework that provides a container for a sequence of long values. It is used for efficient manipulation of sequences of long values, such as in file I/O, network I/O, and data processing.
The LongBuffer class is part of the NIO Buffer family, which includes other buffer classes such as ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, and ShortBuffer. Like other buffer classes, LongBuffer provides methods for accessing, manipulating, and transferring data in a buffer.
Some of the important methods of LongBuffer include:
- allocate(int capacity): Creates a new LongBuffer instance with the specified capacity.
- put(long value): Appends a long value to the buffer.
- get(): Reads the next long value from the buffer.
- flip(): Sets the limit of the buffer to the current position and resets the position to 0.
- asReadOnlyBuffer(): Creates a read-only view of the buffer.
- slice(): Creates a new LongBuffer instance that shares the same underlying data as the original buffer, but with a new position and limit.
LongBuffer is often used in conjunction with other NIO classes such as ByteBuffer, Channel, and Selector to implement high-performance I/O operations in Java.
Declaration of this class as follows.
public abstract class java.nio.LongBuffer
extends java.nio.Buffer
implements java.lang.Comparable <java.nio.LongBuffer >
LongBuffer public method
There are following useful methods which is define the inside of java.nio.LongBuffer class.
Method | Description |
---|---|
static LongBuffer allocate(int capacity) | It allocates a new long buffer. |
long[] array() | It returns the long array that backs this buffer (optional operation). |
int arrayOffset() | It returns the offset within this buffer's backing array of the first element of the buffer (optional operation). |
abstract LongBuffer asReadOnlyBuffer() | It creates a new, read-only long buffer that shares this buffer's content. |
abstract LongBuffer compact() | It compacts this buffer (optional operation). |
int compareTo(LongBuffer that) | It compares this buffer to another. |
abstract LongBuffer duplicate() | It creates a new long buffer that shares this buffer's content. |
boolean equals(Object ob) | It tells whether or not this buffer is equal to another object. |
abstract long get() | It relative get method. |
abstract long get(int index) | It absolute get method. |
LongBuffer get(long[] dst) | It relative bulk get method. |
LongBuffer get(long[] dst, int offset, int length) | It relative bulk get method. |
boolean hasArray() | It tells whether or not this buffer is backed by an accessible long array. |
int hashCode() | It returns the current hash code of this buffer. |
abstract boolean isDirect() | It tells whether or not this long buffer is direct. |
abstract ByteOrder order() | It retrieves this buffer's byte order. |
abstract LongBuffer put(int index, long l) | It absolute put method (optional operation). |
abstract LongBuffer put(long l) | It relative put method (optional operation). |
LongBuffer put(long[] src) | It relative bulk put method (optional operation). |
LongBuffer put(long[] src, int offset, int length) | It relative bulk put method (optional operation). |
LongBuffer put(LongBuffer src) | It relative bulk put method (optional operation). |
abstract LongBuffer slice() | It creates a new long buffer whose content is a shared subsequence of this buffer's content. |
String toString() | It returns a string summarizing the state of this buffer. |
static LongBuffer wrap(long[] array) | It wraps a long array into a buffer. |
static LongBuffer wrap(long[] array, int offset, int length) | It wraps a long array into a buffer. |
This reference is belong to javadoc
java.nio.LongBuffer allocate(int) method example
// allocate(int) method in java.nio.LongBuffer class
import java.nio.LongBuffer;
public class Example
{
public static void main(String[] args)
{
try
{
LongBuffer buffer = LongBuffer.allocate(5);
// Put long value
buffer.put(12L);
buffer.put(20L);
buffer.put(30L);
buffer.put(40L);
buffer.put(30L);
buffer.rewind();
System.out.println(buffer);
}
catch (Exception e)
{
System.out.println(e);
}
}
}

java.nio.HeapLongBuffer[pos=0 lim=5 cap=5]
java.nio.LongBuffer array() method example
This method are returns the reference of array.
// array() method in java.nio.LongBuffer class
// Useful packages
import java.nio.LongBuffer;
public class Example
{
public static void main(String[] args)
{
try
{
LongBuffer buffer = LongBuffer.allocate(5);
// Put long value
buffer.put(10L);
buffer.put(20L);
buffer.put(30L);
buffer.put(40L);
buffer.put(30L);
buffer.rewind();
// Get short array
long[] result = buffer.array();
for (long value: result)
{
System.out.println(value);
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}

10
20
30
40
30
Other methods are coming soon.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment