Examples#
Reading Data#
Read from file#
Also see: Write to file
Read from channel#
Also see: Write to channel
Read from stream#
Also see: Write to stream
Read from buffer#
Also see: Write to buffer
Read array#
Also see: Write array
Read map#
Also see: Write map
Map<String, Integer> read(MessageReader reader) throws IOException {
var size = reader.readMapHeader();
// JDK 19+: var map = HashMap.<String, Integer>newHashMap(size);
var map = new HashMap<String, Integer>();
for (int i = 0; i < size; i++) {
var key = reader.readString();
var value = reader.readInt();
map.put(key, value);
}
return map;
}
Set reader options#
Also see: Set writer options
void read(ReadableByteChannel channel) throws IOException {
try (var reader = MessageReader.of(channel, options -> options
.allocator(BufferAllocator.ofUnpooled()) //(1)
.readBufferCapacity(1024 * 8)
.stringDecoder(MessageDecoder.ofStrings())
.identifierDecoder(MessageDecoder.ofStrings()))
) { /* read some values */ }
}
- Sets reader options by calling
OptionBuilder
methods. All options are optional; this example shows their defaults.
fun read(channel: ReadableByteChannel) {
MessageReaders.of(
channel,
allocator = BufferAllocators.ofUnpooled(), //(1)
readBufferCapacity = 1024 * 8,
stringDecoder = MessageDecoders.ofStrings(),
identifierDecoder = MessageDecoders.ofStrings(),
).use { /* read some values */ }
}
- Sets reader options by setting named method parameters. All options are optional; this example shows their defaults.
Writing Data#
Write to file#
Also see: Read from file
Write to channel#
Also see: Read from channel
Write to stream#
Also see: Read from stream
Write to buffer#
Also see: Read from buffer
Write array#
Also see: Read array
Write map#
Also see: Read map
Set writer options#
Also see: Set reader options
void write(WritableByteChannel channel) throws IOException {
try (var writer = MessageWriter.of(channel, options -> options
.allocator(BufferAllocator.ofUnpooled()) //(1)
.writeBufferCapacity(1024 * 8)
.stringEncoder(MessageEncoder.ofStrings())
.identifierEncoder(MessageEncoder.ofStrings()))
) { /* write some values */ }
}
- Sets writer options by calling
OptionBuilder
methods. All options are optional; this example shows their defaults.
fun write(channel: WritableByteChannel) {
MessageWriters.of(
channel,
allocator = BufferAllocators.ofUnpooled(), //(1)
writeBufferCapacity = 1024 * 8,
stringEncoder = MessageEncoders.ofStrings(),
identifierEncoder = MessageEncoders.ofStrings()
).use { /* write some values */ }
}
- Sets writer options by settings named method parameters. All options are optional; this example shows their defaults.
Buffer Allocation#
Use default allocator#
Use unpooled allocator#
class Example {
final BufferAllocator allocator =
BufferAllocator.ofUnpooled( options -> options //(1)
.maxByteBufferCapacity(Integer.MAX_VALUE) //(2)
.maxCharBufferCapacity(Integer.MAX_VALUE)
);
void read(ReadableByteChannel channel) throws IOException {
try (var reader = MessageReader.of(
channel,
options -> options.allocator(allocator)) //(3)
) { /* read some values */ }
}
void close() {
allocator.close(); //(4)
}
}
- Creates an allocator that returns a new buffer every time
getByteBuffer()
orgetCharBuffer()
is called. - Sets allocator options by calling
UnpooledOptionBuilder
methods. All options are optional; this example shows their defaults. - Sets the same allocator every time a message reader (or writer) is created. Allocators can be safely shared between multiple threads.
- When the allocator is no longer used, it should be closed.
class Example {
val allocator: BufferAllocator = BufferAllocators.ofUnpooled( //(1)
maxByteBufferCapacity = Integer.MAX_VALUE, //(2)
maxCharBufferCapacity = Integer.MAX_VALUE
)
fun read(channel: ReadableByteChannel) {
MessageReaders.of(channel, allocator = allocator).use { reader -> //(3)
// read some values
}
}
fun close() {
allocator.close() //(4)
}
}
- Creates an allocator that returns a new buffer every time
getByteBuffer()
orgetCharBuffer()
is called. - Sets allocator options by setting named method parameters. All options are optional; this example shows their defaults.
- Sets the same allocator every time a message reader (or writer) is created. Allocators can be safely shared between multiple threads.
- When the allocator is no longer used, it should be closed.
Use pooled allocator#
class Example {
final BufferAllocator allocator =
BufferAllocator.ofPooled(options -> options //(1)
.maxByteBufferCapacity(Integer.MAX_VALUE) //(2)
.maxCharBufferCapacity(Integer.MAX_VALUE)
.maxPooledByteBufferCapacity(1024 * 1024)
.maxPooledCharBufferCapacity(1024 * 512)
.maxByteBufferPoolCapacity(1024 * 1024 * 64)
.maxCharBufferPoolCapacity(1024 * 1024 * 32)
.preferDirectBuffers(false)
);
void read(ReadableByteChannel channel) throws IOException {
try (var reader = MessageReader.of(
channel,
options -> options.allocator(allocator)) //(3)
) { /* read some values */ }
}
void close() {
allocator.close(); //(4)
}
}
- Creates an allocator that reduces buffer allocations by maintaining a buffer pool.
- Sets allocator options by calling
PooledOptionBuilder
methods. All options are optional; this example shows their defaults. - Sets the same allocator every time a message reader (or writer) is created. Allocators can be safely shared between multiple threads.
- When the allocator is no longer used, it should be closed to free its buffer pool.
class Example {
val allocator: BufferAllocator = BufferAllocators.ofPooled( //(1)
maxByteBufferCapacity = Integer.MAX_VALUE, //(2)
maxCharBufferCapacity = Integer.MAX_VALUE,
maxPooledByteBufferCapacity = 1024 * 1024,
maxPooledCharBufferCapacity = 1024 * 512,
maxByteBufferPoolCapacity = 1024 * 1024 * 64,
maxCharBufferPoolCapacity = 1024 * 1024 * 32,
preferDirectBuffers = false
)
fun read(channel: ReadableByteChannel) {
MessageReaders.of(channel, allocator = allocator).use { reader -> //(3)
// read some values
}
}
fun close() {
allocator.close() //(4)
}
}
- Creates an allocator that reduces buffer allocations by maintaining a buffer pool.
- Sets allocator options by setting named method parameters. All options are optional; this example shows their defaults.
- Sets the same allocator every time a message reader (or writer) is created. Allocators can be safely shared between multiple threads.
- When the allocator is no longer used, it should be closed to free its buffer pool.
Kotlin Integration#
Read unsigned integers#
See Kotlin Integration.
Write unsigned integers#
See Kotlin Integration.