Top Description Inners Fields Constructors Methods
io.netty5.buffer

public final Class SensitiveBufferAllocator

extends Object
implements BufferAllocator
Class Inheritance
All Implemented Interfaces
io.netty5.buffer.BufferAllocator, io.netty5.util.SafeCloseable, java.lang.AutoCloseable
Imports
io.netty5.buffer.internal.ArcDrop, .CleanerDrop, .MemoryManagerOverride, java.nio.ByteBuffer, java.util.function.Function, .Supplier

This BufferAllocator is for allocating off-heap Buffers 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[]) and BufferAllocator#constBufferSupplier(byte[]), and the buffer passed to BufferAllocator#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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final AllocatorControl
private final Function<Drop<Buffer>, Drop<Buffer>>
private static final BufferAllocator
private final MemoryManager

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Buffer
allocate(int
The size of Buffer to allocate.
size
)

Implements io.netty5.buffer.BufferAllocator.allocate.

Allocate a Buffer of the given size in bytes.
public void
close()

Implements io.netty5.buffer.BufferAllocator.close.

Close this allocator, freeing all of its internal resources.
public Supplier<Buffer>
constBufferSupplier(byte[]
The byte contents of the buffers produced by the returned supplier.
bytes
)

Implements io.netty5.buffer.BufferAllocator.constBufferSupplier.

Create a supplier of "constant" Buffers from this allocator, that all have the given byte contents.
private Drop<Buffer>
public AllocationType
private MemoryManager
public boolean
isPooling()

Implements io.netty5.buffer.BufferAllocator.isPooling.

Determine if this allocator is pooling and reusing its allocated memory.
public static BufferAllocator

Returns:

The allocator.
sensitiveOffHeapAllocator
()

Get the sensitive off-heap buffer allocator instance.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

controlback to summary
private final AllocatorControl control
decoratorback to summary
private final Function<Drop<Buffer>, Drop<Buffer>> decorator
INSTANCEback to summary
private static final BufferAllocator INSTANCE
managerback to summary
private final MemoryManager manager

Constructor Detail

SensitiveBufferAllocatorback to summary
private SensitiveBufferAllocator(MemoryManager manager)

Method Detail

allocateback to summary
public Buffer allocate(int size)

Implements io.netty5.buffer.BufferAllocator.allocate.

Doc from io.netty5.buffer.BufferAllocator.allocate.

Allocate a Buffer of the given size in bytes. This method may throw an OutOfMemoryError if there is not enough free memory available to allocate a Buffer of the requested size.

The buffer will use big endian byte order.

Note

unlike the JDK ByteBuffers, Netty Buffers are 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 the SensitiveBufferAllocator.

Parameters
size:int

The size of Buffer to allocate.

Returns:Buffer

The newly allocated Buffer.

Annotations
@Override
closeback to summary
public void close()

Implements io.netty5.buffer.BufferAllocator.close.

Doc from io.netty5.buffer.BufferAllocator.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.

Annotations
@Override
constBufferSupplierback to summary
public Supplier<Buffer> constBufferSupplier(byte[] bytes)

Implements io.netty5.buffer.BufferAllocator.constBufferSupplier.

Doc from io.netty5.buffer.BufferAllocator.constBufferSupplier.

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 final field 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:byte[]

The byte contents of the buffers produced by the returned supplier.

Returns:Supplier<Buffer>

A supplier of read-only buffers with the given contents.

Annotations
@Override
decorateback to summary
private Drop<Buffer> decorate(Drop<Buffer> base)
getAllocationTypeback to summary
public AllocationType getAllocationType()

Implements io.netty5.buffer.BufferAllocator.getAllocationType.

Doc from io.netty5.buffer.BufferAllocator.getAllocationType.

Get the AllocationType from this allocator. This would typically be one of the StandardAllocationTypes.

Returns:AllocationType

The type of allocations performed by this allocator.

Annotations
@Override
getManagerback to summary
private MemoryManager getManager()
isPoolingback to summary
public boolean isPooling()

Implements io.netty5.buffer.BufferAllocator.isPooling.

Doc from io.netty5.buffer.BufferAllocator.isPooling.

Determine if this allocator is pooling and reusing its allocated memory.

Returns:boolean

true if this allocator is pooling and reusing its memory, false otherwise.

Annotations
@Override
sensitiveOffHeapAllocatorback to summary
public static BufferAllocator sensitiveOffHeapAllocator()

Get the sensitive off-heap buffer allocator instance.

Returns:BufferAllocator

The allocator.

io.netty5.buffer back to summary

private final Class SensitiveBufferAllocator.ZeroingDrop

extends Object
implements Drop<Buffer>
Class Inheritance
All Implemented Interfaces
io.netty5.buffer.Drop

Field Summary

Modifier and TypeField and Description
private final Drop<Buffer>
private final MemoryManager

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
attach(Buffer
The new Resource instance with the new owner.
obj
)

Implements io.netty5.buffer.Drop.attach.

Called when the resource changes owner.
public void
drop(Buffer
The Resource instance being dropped.
obj
)

Implements io.netty5.buffer.Drop.drop.

Dispose of the resources in the given Resource instance.
public Drop<Buffer>
fork()

Implements io.netty5.buffer.Drop.fork.

Branch the responsibility of dropping a resource.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

baseback to summary
private final Drop<Buffer> base
managerback to summary
private final MemoryManager manager

Constructor Detail

ZeroingDropback to summary
pack-priv ZeroingDrop(MemoryManager manager, Drop<Buffer> base)

Method Detail

attachback to summary
public void attach(Buffer obj)

Implements io.netty5.buffer.Drop.attach.

Doc from io.netty5.buffer.Drop.attach.

Called when the resource changes owner.

Parameters
obj:Buffer

The new Resource instance with the new owner.

Annotations
@Override
dropback to summary
public void drop(Buffer obj)

Implements io.netty5.buffer.Drop.drop.

Doc from io.netty5.buffer.Drop.drop.

Dispose of the resources in the given Resource instance.

Parameters
obj:Buffer

The Resource instance being dropped.

Annotations
@Override
forkback to summary
public Drop<Buffer> fork()

Implements io.netty5.buffer.Drop.fork.

Doc from io.netty5.buffer.Drop.fork.

Branch the responsibility of dropping a resource. This drop instance will remain associated with its current resource, while the returned drop instance must be attached to its new resource.

Returns:Drop<Buffer>

A drop instance, similar to this one, but for the purpose of being associated with a different but related resource.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override