public class DefaultAllocator extends Object implements IByteBufferAllocator
IByteBufferAllocator
interface.Modifier and Type | Field and Description |
---|---|
static DefaultAllocator |
DEFAULT
A constant holding the default non-direct buffer allocator.
|
protected boolean |
direct
Tells if the allocator should allocate direct buffers.
|
Constructor and Description |
---|
DefaultAllocator(boolean direct)
Constructs a default allocator.
|
DefaultAllocator(boolean direct,
IDefaultAllocatorMetricCollector metric)
Constructs a default allocator with specified metric data collector.
|
Modifier and Type | Method and Description |
---|---|
ByteBuffer |
allocate(int capacity)
Allocates a new byte buffer.
|
protected ByteBuffer |
allocate(int capacity,
boolean direct)
Allocates a new buffer with given capacity and type
|
protected ByteBuffer |
allocate(int capacity,
ByteBuffer buffer)
Allocates a new buffer that will replace the original not empty buffer.
|
protected ByteBuffer |
allocateEmpty(int capacity,
ByteBuffer buffer)
Allocates a new buffer that will replace the original empty buffer.
|
ByteBuffer |
ensure(ByteBuffer buffer,
int size,
int minCapacity,
int maxCapacity)
Assures that returned buffer will contain room for new data.
|
ByteBuffer |
ensureSome(ByteBuffer buffer,
int minCapacity,
int maxCapacity)
Ensures that returned buffer will contain some room for new data.
|
ByteBuffer |
extend(ByteBuffer buffer,
int maxCapacity)
Extends the capacity of the buffer based on its current capacity.
|
boolean |
isReleasable()
Tells if an implementation of the allocator supports releasing of no
longer used buffers.
|
ByteBuffer |
reduce(ByteBuffer buffer,
int minCapacity)
Tries to reduce the capacity of the buffer based on its content.
|
void |
release(ByteBuffer buffer)
Informs that given buffer that was allocated by this allocator is no
longer used and can be released.
|
boolean |
usesArray()
Tells whether or not this allocator allocates buffers that are backed by
an accessible byte array.
|
public static final DefaultAllocator DEFAULT
protected final boolean direct
public DefaultAllocator(boolean direct)
direct
- true
if the allocator should allocate direct
buffers, or false
to allocate non-direct buffers
that have a backing arraypublic DefaultAllocator(boolean direct, IDefaultAllocatorMetricCollector metric)
direct
- true
if the allocator should allocate direct
buffers, or false
to allocate non-direct buffers
that have a backing arraymetric
- a metric data collectorpublic boolean usesArray()
IByteBufferAllocator
usesArray
in interface IByteBufferAllocator
public boolean isReleasable()
isReleasable
in interface IByteBufferAllocator
false
public void release(ByteBuffer buffer)
IByteBufferAllocator
release
in interface IByteBufferAllocator
buffer
- buffer to be releasedprotected ByteBuffer allocate(int capacity, boolean direct)
capacity
- the capacity of the allocated bufferdirect
- the type of the allocated bufferprotected ByteBuffer allocateEmpty(int capacity, ByteBuffer buffer)
capacity
- the capacity of the allocated bufferbuffer
- the buffer to be replaced. If the allocator support releasing it
can be safely released in this method.protected ByteBuffer allocate(int capacity, ByteBuffer buffer)
capacity
- the capacity of the allocated bufferbuffer
- the buffer to be replaced. If the allocator support releasing it
can be safely released in this method.public ByteBuffer allocate(int capacity)
IByteBufferAllocator
allocate
in interface IByteBufferAllocator
capacity
- The new buffer's capacity, in bytespublic ByteBuffer ensureSome(ByteBuffer buffer, int minCapacity, int maxCapacity)
IndexOutOfBoundsException
.
It is only used to manage the capacity of the input buffer associated with the stream-oriented sessions.
This implementation uses following rules to calculate the capacity of the new buffer:
a) if there is no room in the buffer it tries to increase the capacity by doubling its size. However, if the doubled size is greater than the maximum capacity then the maximum will be used as the new capacity.
b) if there is no data in the buffer then the new capacity will be reduced to the minimum unless the buffer's capacity already equals the minimum capacity.
c) if the buffer contains data which size is still greater than a quarter of the buffer's capacity then the capacity will not be changed and no re-allocation will be performed.
d) if the buffer contains data which size is equal or less than a quarter of the buffer's capacity then the capacity will be reduced by half so many times until the capacity will be still greater or equal to the doubled size of the data in the buffer, or when the minimum capacity is reached.
ensureSome
in interface IByteBufferAllocator
buffer
- buffer in the write mode (i.e. not flipped yet)minCapacity
- min capacitymaxCapacity
- max capacitypublic ByteBuffer ensure(ByteBuffer buffer, int size, int minCapacity, int maxCapacity)
IndexOutOfBoundsException
.
It is only used to manage the capacity of the input buffer associated with the engine driven stream-oriented sessions.
This implementation uses following rules to calculate the capacity of the new buffer:
a) if the new minimal capacity that is required to store a new data is less than 4MB the capacity will calculated as a minimum power of two that is greater or equal to the new minimal required capacity. However, if the calculated size is greater than the maximum capacity then the maximum will be used as the new capacity.
b) if the new minimal capacity that is required to store a new data is greater or equal to 4MB the capacity will calculated as a minimum multiple of 4MB that is greater or equal to the new minimal required capacity. However, if the calculated size is greater than the maximum capacity then the maximum will be used as the new capacity.
ensure
in interface IByteBufferAllocator
buffer
- buffer in the write mode (i.e. not flipped yet)size
- size of data to be put to the bufferminCapacity
- min capacitymaxCapacity
- max capacitypublic ByteBuffer reduce(ByteBuffer buffer, int minCapacity)
It is only used to manage the capacity of the output buffers associated with the stream-oriented sessions.
This implementation uses following rules to calculate the capacity of the new buffer:
a) if the buffer's capacity equals the minimum capacity then the capacity will not be changed and no re-allocation will be performed.
b) if the buffer contains data which size is equal or less than the minimum capacity then the capacity will be reduced to the minimum value.
c) if the buffer contains data which size is greater than the minimum capacity then the capacity will not be changed and no re-allocation will be performed.
reduce
in interface IByteBufferAllocator
buffer
- buffer in the write mode (i.e. not flipped yet)minCapacity
- min capacitypublic ByteBuffer extend(ByteBuffer buffer, int maxCapacity)
It is only used to manage the capacity of the input buffer associated with the datagram-oriented sessions. The method is called after receiving a datagram that fully filled up the input buffer.
This implementation uses following rule to calculate the capacity of the new buffer:
a) if the buffer's capacity is less than the maximum capacity it will try to increase the capacity by doubling its size. However, if the doubled size is greater than the maximum capacity then the maximum will be used as the new capacity.
extend
in interface IByteBufferAllocator
buffer
- buffer in the write mode (i.e. not flipped yet)maxCapacity
- max capacityCopyright © 2017–2021 SNF4J.ORG. All rights reserved.