public class PooledByteBufferAllocator extends java.lang.Object implements ByteBufferAllocator
ByteBufferAllocator
which pools allocated buffers. All buffers are allocated with the size of power of 2 (e.g. 16, 32, 64, ...) This means that you cannot simply assume that the actual capacity of the buffer and the capacity you requested are same.
This allocator releases the buffers which have not been in use for a
certain period. You can adjust the period by calling setTimeout(int)
. The default timeout is 1 minute (60
seconds). To release these buffers periodically, a daemon thread is started when a new instance of the allocator is
created. You can stop the thread by calling dispose()
.
Constructor and Description |
---|
PooledByteBufferAllocator()
Creates a new instance with the default timeout.
|
PooledByteBufferAllocator(int timeout)
Creates a new instance with the specified timeout.
|
Modifier and Type | Method and Description |
---|---|
ByteBuffer |
allocate(int capacity,
boolean direct)
Returns the buffer which is capable of the specified size.
|
void |
dispose()
Stops the thread which releases unused buffers and make this allocator unusable from now on.
|
int |
getTimeout()
Returns the timeout value of this allocator in seconds.
|
long |
getTimeoutMillis()
Returns the timeout value of this allocator in milliseconds.
|
void |
setTimeout(int timeout)
Sets the timeout value of this allocator in seconds.
|
ByteBuffer |
wrap(java.nio.ByteBuffer nioBuffer)
Wraps the specified NIO
ByteBuffer into MINA buffer. |
public PooledByteBufferAllocator()
public PooledByteBufferAllocator(int timeout)
public void dispose()
dispose
in interface ByteBufferAllocator
public int getTimeout()
public long getTimeoutMillis()
public void setTimeout(int timeout)
timeout
- 0 or negative value to disable timeout.public ByteBuffer allocate(int capacity, boolean direct)
ByteBufferAllocator
allocate
in interface ByteBufferAllocator
capacity
- the capacity of the bufferdirect
- true to get a direct buffer,
false to get a heap buffer.public ByteBuffer wrap(java.nio.ByteBuffer nioBuffer)
ByteBufferAllocator
ByteBuffer
into MINA buffer.wrap
in interface ByteBufferAllocator
Copyright © 2004-2019 Apache MINA Project. All Rights Reserved.