Interface BufferAllocator
-
- All Superinterfaces:
AutoCloseable,io.netty5.util.SafeCloseable
- All Known Implementing Classes:
PooledBufferAllocator
public interface BufferAllocator extends io.netty5.util.SafeCloseableInterface for allocatingBuffers.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Bufferallocate(int size)Allocate aBufferof the given size in bytes.voidclose()Close this allocator, freeing all of its internal resources.Supplier<Buffer>constBufferSupplier(byte[] bytes)Create a supplier of "constant" Buffers from this allocator, that all have the given byte contents.default BuffercopyOf(byte[] bytes)Allocate aBufferwith the same size and contents of the given byte array.default BuffercopyOf(ByteBuffer buffer)Allocate aBufferwith the same size and contents, as the contents of the givenByteBuffer.AllocationTypegetAllocationType()Get theAllocationTypefrom this allocator.booleanisPooling()Determine if this allocator is pooling and reusing its allocated memory.static BufferAllocatoroffHeapPooled()Produces a poolingBufferAllocatorthat allocates and recycles off-heap buffers.static BufferAllocatoroffHeapUnpooled()Produces aBufferAllocatorthat allocates unpooled, off-heap buffers.static BufferAllocatoronHeapPooled()Produces a poolingBufferAllocatorthat allocates and recycles on-heap buffers.static BufferAllocatoronHeapUnpooled()Produces aBufferAllocatorthat allocates unpooled, on-heap buffers.
-
-
-
Method Detail
-
onHeapUnpooled
static BufferAllocator onHeapUnpooled()
Produces aBufferAllocatorthat allocates unpooled, on-heap buffers. On-heap buffers have abyte[]internally, and their readable and writable native addresses are zero.The concrete
Bufferimplementation is chosen byMemoryManager.instance().Note: This method always creates a new allocator instance. To get a shared and cached allocator instance, use the
DefaultBufferAllocators.onHeapAllocator()method instead.- Returns:
- A non-pooling allocator of on-heap buffers
-
offHeapUnpooled
static BufferAllocator offHeapUnpooled()
Produces aBufferAllocatorthat allocates unpooled, off-heap buffers. Off-heap buffers a native memory pointer internally, which can be obtained from their readable and writable native address methods.The concrete
Bufferimplementation is chosen byMemoryManager.instance().Note: This method always creates a new allocator instance. To get a shared and cached allocator instance, use the
DefaultBufferAllocators.offHeapAllocator()method instead.- Returns:
- A non-pooling allocator of on-heap buffers
-
onHeapPooled
static BufferAllocator onHeapPooled()
Produces a poolingBufferAllocatorthat allocates and recycles on-heap buffers. On-heap buffers have abyte[]internally, and their readable and writable native addresses are zero.The concrete
Bufferimplementation is chosen byMemoryManager.instance().Note: This method always creates a new allocator instance. To get a shared and cached allocator instance, use the
DefaultBufferAllocators.onHeapAllocator()method instead.- Returns:
- A pooling allocator of on-heap buffers
-
offHeapPooled
static BufferAllocator offHeapPooled()
Produces a poolingBufferAllocatorthat allocates and recycles off-heap buffers. Off-heap buffers a native memory pointer internally, which can be obtained from their readable and writable native address methods.The concrete
Bufferimplementation is chosen byMemoryManager.instance().Note: This method always creates a new allocator instance. To get a shared and cached allocator instance, use the
DefaultBufferAllocators.offHeapAllocator()method instead.- Returns:
- A pooling allocator of on-heap buffers
-
isPooling
boolean isPooling()
Determine if this allocator is pooling and reusing its allocated memory.- Returns:
trueif this allocator is pooling and reusing its memory,falseotherwise.
-
getAllocationType
AllocationType getAllocationType()
Get theAllocationTypefrom this allocator. This would typically be one of theStandardAllocationTypes.- Returns:
- The type of allocations performed by this allocator.
-
allocate
Buffer allocate(int size)
Allocate aBufferof the given size in bytes. This method may throw anOutOfMemoryErrorif there is not enough free memory available to allocate aBufferof the requested size.The buffer will use big endian byte order.
- Parameters:
size- The size ofBufferto allocate.- Returns:
- The newly allocated
Buffer. - Throws:
IllegalStateException- if this allocator has been closed.
-
constBufferSupplier
Supplier<Buffer> constBufferSupplier(byte[] bytes)
Create a supplier of "constant" Buffers from this allocator, that all have the given byte contents. The buffer has the same capacity as the byte array length, and its write offset is placed at the end, and its read offset is at the beginning, such that the entire buffer contents are readable.The buffers produced by the supplier will each have their own independent life-cycle, and closing them will make them inaccessible, just like normally allocated buffers.
The buffers produced are "constants", in the sense that they are read-only.
It can generally be expected, but is not guaranteed, that the returned supplier is more resource efficient than allocating and copying memory with other available APIs. In such optimised implementations, the underlying memory baking the buffers will be shared among all the buffers produced by the supplier.
The primary use case for this API, is when you need to repeatedly produce buffers with the same contents, and you perhaps wish to keep a
static finalfield with these contents. The supplier-based API enforces that each usage get their own distinct buffer instance. Each of these instances cannot interfere with each other, so bugs like closing, or modifying the contents, of a shared buffer cannot occur.- Parameters:
bytes- The byte contents of the buffers produced by the returned supplier.- Returns:
- A supplier of read-only buffers with the given contents.
- Throws:
IllegalStateException- if this allocator has been closed, but any supplier obtained prior to closing the allocator will continue to work.
-
copyOf
default Buffer copyOf(byte[] bytes)
Allocate aBufferwith the same size and contents of the given byte array. The allocated buffer is NOT backed by the given byte array, and changes to the contents of either will not be reflected in the other. This may throw anOutOfMemoryErrorif there is not enough free memory available to allocate aBufferof the requested size.The allocated buffer will use big endian byte order.
- Parameters:
bytes- The byte array that determines the size and contents of the new buffer.- Returns:
- The newly allocated
Buffer. - Throws:
IllegalStateException- if this allocator has been closed.
-
copyOf
default Buffer copyOf(ByteBuffer buffer)
Allocate aBufferwith the same size and contents, as the contents of the givenByteBuffer. The allocated buffer is NOT backed by the give byte buffer, and changes to the contents of either will not be reflected in the other. The position and limit of the given byte buffer defines the region of contents that will be copied. The position and limit are not modified by this method. This may throw anOutOfMemoryErrorif there is not enough free memory available to allocate aBufferof the requested size.The allocated buffer will use big endian byte order, regardless of the byte order of the given buffer. The contents of the byte buffer will be copied without regard to the byte order, as if the bytes are copied one at a time.
- Parameters:
buffer- The byte buffer, whose contents determine the capacity and contents of the allocated buffer.- Returns:
- The newly allocated
Buffer. - Throws:
IllegalStateException- if this allocator has been closed.
-
close
void close()
Close this allocator, freeing all of its internal resources.Existing (currently in-use) allocated buffers will not be impacted by calling this method. If this is a pooling or caching allocator, then existing buffers will be immediately freed when they are closed, instead of being pooled or cached.
The allocator can no longer be used to allocate more buffers after calling this method. Attempting to allocate from a closed allocator will cause
IllegalStateExceptions to be thrown.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceio.netty5.util.SafeCloseable
-
-