LongBuffer is a class from the java.nio package used for handling sequences of 64-bit long integers efficiently. It is part of Java’s New I/O (NIO) system, designed for fast memory-based I/O operations without direct use of arrays.LongBuffer is abstract; use allocate() and wrap() factory methods to instantiate.
Commonly Used Mrthods

Simple Program: Storing Long Timestamps
import java.nio.LongBuffer;
public class LongBufferSimple {
public static void main(String[] args) {
// Allocate LongBuffer with space for 3 longs
LongBuffer buffer = LongBuffer.allocate(3);
// Put long values (timestamps)
buffer.put(1623782934L);
buffer.put(1623782988L);
buffer.put(1623783031L);
// Prepare for reading
buffer.flip();
System.out.println("Timestamps stored in LongBuffer:");
while (buffer.hasRemaining()) {
System.out.println(buffer.get());
}
}
}Problem Statement
LotusJavaPrince is managing a blockchain system in which each block is uniquely identified by a 64-bit blockId.
Mahesh has challenged him:
- Accept an array of 10 random
blockIdsaslong[]. - Store them in a
LongBuffer. - Filter out only the even block IDs.
- Display the valid even block IDs and their positions.
Can you help LotusJavaPrince solve this efficiently using LongBuffer?
import java.nio.LongBuffer;
public class BlockchainBuffer {
public static void main(String[] args) {
// Simulated blockchain block IDs (64-bit)
long[] blockIds = {
112233445566L, 987654321123L, 102030405060L,
556677889900L, 998877665544L, 123456789012L,
111111111111L, 246802468024L, 135791357913L, 121212121212L
};
// Wrap the blockIds into a LongBuffer
LongBuffer inputBuffer = LongBuffer.wrap(blockIds);
// Allocate a buffer to hold even block IDs
LongBuffer evenBlockBuffer = LongBuffer.allocate(blockIds.length);
// Filter and store even block IDs
while (inputBuffer.hasRemaining()) {
long blockId = inputBuffer.get();
if (blockId % 2 == 0) {
evenBlockBuffer.put(blockId);
}
}
// Prepare buffer for reading
evenBlockBuffer.flip();
// Display the even block IDs
System.out.println("Even Block IDs (filtered by LotusJavaPrince):");
int index = 0;
while (evenBlockBuffer.hasRemaining()) {
System.out.println("ValidBlock[" + index + "]: " + evenBlockBuffer.get());
index++;
}
}
}Output
Even Block IDs (filtered by LotusJavaPrince):
ValidBlock[0]: 112233445566
ValidBlock[1]: 102030405060
ValidBlock[2]: 556677889900
ValidBlock[3]: 998877665544
ValidBlock[4]: 123456789012
ValidBlock[5]: 246802468024
ValidBlock[6]: 121212121212Code language: CSS (css)
The LongBuffer class in Java NIO provides a powerful mechanism to store and manipulate sequences of 64-bit long values efficiently. As a subclass of the abstract Buffer class, it inherits essential buffer management capabilities and including position, limit, capacity, and mark — tailored specifically for long data types.
LongBuffer is especially useful in applications dealing with large numerical datasets, such as high-precision timestamps, big data analytics, file processing, and memory-mapped I/O where long integers are frequently used. By supporting both heap-based and direct buffers, it ensures flexible memory management and high throughput.
Key advantages of LongBuffer include:
- Fast access to long values using relative and absolute
get()andput()methods, - Support for bulk operations to read/write multiple longs at once,
- Ability to slice, duplicate, and compact buffers for advanced data manipulation.
