- java.lang.Object
-
- io.netty5.buffer.SensitiveBufferAllocator
-
- All Implemented Interfaces:
BufferAllocator,io.netty5.util.SafeCloseable,AutoCloseable
public final class SensitiveBufferAllocator extends Object implements BufferAllocator
ThisBufferAllocatoris for allocating off-heapBuffers that may contain sensitive information, which should be erased from memory (overwritten) when the buffer is closed.The features and behaviours of this allocator are otherwise exactly the same as
BufferAllocator.offHeapUnpooled().Of particular note, the byte arrays passed to
BufferAllocator.copyOf(byte[])andBufferAllocator.constBufferSupplier(byte[]), and the buffer passed toBufferAllocator.copyOf(ByteBuffer)are not cleared by this allocator. These copies of the sensitive data must be cleared separately! Ideally these methods should not be used for sensitive data in the first place, and the sensitive data should instead be directly written to, or created in, a sensitive buffer.This allocator is stateless, and the
close()method has no effect.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete 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.AllocationTypegetAllocationType()Get theAllocationTypefrom this allocator.booleanisPooling()Determine if this allocator is pooling and reusing its allocated memory.static BufferAllocatorsensitiveOffHeapAllocator()Get the sensitive off-heap buffer allocator instance.
-
-
-
Method Detail
-
sensitiveOffHeapAllocator
public static BufferAllocator sensitiveOffHeapAllocator()
Get the sensitive off-heap buffer allocator instance.- Returns:
- The allocator.
-
isPooling
public boolean isPooling()
Description copied from interface:BufferAllocatorDetermine if this allocator is pooling and reusing its allocated memory.- Specified by:
isPoolingin interfaceBufferAllocator- Returns:
trueif this allocator is pooling and reusing its memory,falseotherwise.
-
getAllocationType
public AllocationType getAllocationType()
Description copied from interface:BufferAllocatorGet theAllocationTypefrom this allocator. This would typically be one of theStandardAllocationTypes.- Specified by:
getAllocationTypein interfaceBufferAllocator- Returns:
- The type of allocations performed by this allocator.
-
allocate
public Buffer allocate(int size)
Description copied from interface:BufferAllocatorAllocate 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.
Note: unlike the JDK
ByteBuffers, NettyBuffersare not guaranteed to be zeroed when allocated. In other words, the memory of a newly allocated buffer may contain garbage data from prior allocations, and the memory is likewise not guaranteed to be erased when the buffer is closed. If the data is sensitive and needs to be overwritten when the buffer is closed, then the buffer should be allocated with theSensitiveBufferAllocator.- Specified by:
allocatein interfaceBufferAllocator- Parameters:
size- The size ofBufferto allocate.- Returns:
- The newly allocated
Buffer.
-
constBufferSupplier
public Supplier<Buffer> constBufferSupplier(byte[] bytes)
Description copied from interface:BufferAllocatorCreate 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.- Specified by:
constBufferSupplierin interfaceBufferAllocator- Parameters:
bytes- The byte contents of the buffers produced by the returned supplier.- Returns:
- A supplier of read-only buffers with the given contents.
-
close
public void close()
Description copied from interface:BufferAllocatorClose 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 interfaceBufferAllocator- Specified by:
closein interfaceio.netty5.util.SafeCloseable
-
-