Interface BufferAllocator
- All Superinterfaces:
AutoCloseable,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.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this buffer allocator, freeing any pooled buffers.getByteBuffer(int capacity) Returns a leasedByteBufferwith at least the given capacity.getCharBuffer(int capacity) Returns a leasedCharBufferwith at least the given capacity.intReturns the maximum buffer capacity, in bytes, that may be requested bygetByteBuffer(int).intReturns the maximum buffer capacity, in chars, that may be requested bygetCharBuffer(int).static BufferAllocatorofPooled()Returns a new buffer allocator that maintains a buffer pool to reduce buffer allocations.static BufferAllocatorofPooled(Consumer<BufferAllocator.PooledOptionBuilder> optionHandler) Returns a new buffer allocator with the given options that maintains a buffer pool to reduce buffer allocations.static BufferAllocatorReturns a new buffer allocator that allocates a new buffer each timegetByteBuffer(int)orgetCharBuffer(int)is called.static BufferAllocatorofUnpooled(Consumer<BufferAllocator.UnpooledOptionBuilder> optionHandler) Returns a new buffer allocator with the given options that allocates a new buffer each timegetByteBuffer(int)orgetCharBuffer(int)is called.
-
Method Details
-
ofUnpooled
Returns a new buffer allocator that allocates a new buffer each timegetByteBuffer(int)orgetCharBuffer(int)is called.- Returns:
- a new buffer allocator that allocates a new buffer
each time
getByteBuffer(int)orgetCharBuffer(int)is called
-
ofUnpooled
Returns a new buffer allocator with the given options that allocates a new buffer each timegetByteBuffer(int)orgetCharBuffer(int)is called.- Parameters:
optionHandler- a handler that receives anBufferAllocator.UnpooledOptionBuilder- Returns:
- a new buffer allocator with the given options
that allocates a new buffer each time
getByteBuffer(int)orgetCharBuffer(int)is called
-
ofPooled
Returns a new buffer allocator that maintains a buffer pool to reduce buffer allocations.
This allocator supports the use of direct buffers.
The current pooled allocator implementation behaves as follows:
- The buffer pool is grown as needed until the maximum pool capacity for byte and char buffers is reached.
- If the buffer pool runs out of buffers after the maximum pool capacity has been reached, additional unpooled buffers are allocated.
- The buffer pool is never shrunk. It is freed when the allocator is closed.
- Returns:
- a new buffer allocator that maintains a buffer pool to reduce buffer allocations
-
ofPooled
Returns a new buffer allocator with the given options that maintains a buffer pool to reduce buffer allocations.
This allocator supports the use of direct buffers.
The current pooled allocator implementation behaves as follows:
- The buffer pool is grown as needed until the maximum pool capacity for byte and char buffers is reached.
- If the buffer pool runs out of buffers after the maximum pool capacity has been reached, additional unpooled buffers are allocated.
- The buffer pool is never shrunk. It is freed when the allocator is closed.
- Parameters:
optionHandler- a handler that receives aBufferAllocator.PooledOptionBuilder- Returns:
- a new buffer allocator with the given options that maintains a buffer pool to reduce buffer allocations
-
maxByteBufferCapacity
int maxByteBufferCapacity()Returns the maximum buffer capacity, in bytes, that may be requested by
getByteBuffer(int).The returned value can be set with
BufferAllocator.UnpooledOptionBuilder.maxByteBufferCapacity(int)andBufferAllocator.PooledOptionBuilder.maxByteBufferCapacity(int).Default:
Integer.MAX_VALUE- Returns:
- the maximum buffer capacity, in bytes, that may be requested by
getByteBuffer(int)
-
maxCharBufferCapacity
int maxCharBufferCapacity()Returns the maximum buffer capacity, in chars, that may be requested by
getCharBuffer(int).The returned value can be set with
BufferAllocator.UnpooledOptionBuilder.maxCharBufferCapacity(int)andBufferAllocator.PooledOptionBuilder.maxCharBufferCapacity(int).Default:
Integer.MAX_VALUE- Returns:
- the maximum buffer capacity, in chars, that may be requested by
getCharBuffer(int)
-
getByteBuffer
Returns a leased
ByteBufferwith at least the given capacity.The returned buffer is cleared. When the buffer is no longer used, it should be closed. Failing to close an unused buffer reduces the efficiency of buffer pooling.
- Parameters:
capacity- the minimum capacity, in bytes, of the leased buffer- Returns:
- a leased
ByteBufferwith at least the given capacity - Throws:
MxPackException.SizeLimitExceeded- if the given capacity is greater than the maximum capacityIllegalStateException- if this allocator has already been closed
-
getCharBuffer
Returns a leased
CharBufferwith at least the given capacity.The returned buffer is cleared. When the buffer is no longer used, it should be closed. Failing to close an unused buffer reduces the efficiency of buffer pooling.
- Parameters:
capacity- the minimum capacity, in chars, of the leased buffer- Returns:
- a leased
CharBufferwith at least the given capacity - Throws:
MxPackException.SizeLimitExceeded- if the given capacity is greater than the maximum capacityIllegalStateException- if this allocator has already been closed
-
close
void close()Closes this buffer allocator, freeing any pooled buffers.
Subsequent calls to this method have no effect. Subsequent calls to
getByteBuffer(int)orgetCharBuffer(int)throwIllegalStateException.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-