Interface BufferAllocator

All Superinterfaces:
AutoCloseable, Closeable

public sealed interface BufferAllocator extends Closeable

An allocator of byte and char buffers.

By default, MxPack allocates buffers with an unpooled allocator with default options. To use a different allocator, set MessageReader.OptionBuilder.allocator(org.odenix.mxpack.core.BufferAllocator) when creating a MessageReader, or MessageWriter.OptionBuilder.allocator(org.odenix.mxpack.core.BufferAllocator) when creating a MessageWriter.

If frequent buffer allocations are not a concern, use an unpooled allocator. To reduce buffer allocations, use a single pooled allocator for all message sources and sinks. Allocators can be safely shared by multiple threads. Only pooled allocators can allocate direct byte buffers.

To create an allocator, call ofUnpooled() or ofPooled(). Because unpooled and pooled allocators implement the same BufferAllocator interface, switching between them is easy. To obtain a buffer, call getByteBuffer(int) or getCharBuffer(int). When a buffer is no longer used, call LeasedByteBuffer.close() or LeasedCharBuffer.close(). Failing to close an unused buffer reduces the efficiency of buffer pooling.

When an allocator is no longer used, call close() to free its buffer pool.

For usage examples, see Buffer allocation.