org.apache.mina.common
Class PooledByteBufferAllocator

java.lang.Object
  extended by org.apache.mina.common.PooledByteBufferAllocator
All Implemented Interfaces:
ByteBufferAllocator

public class PooledByteBufferAllocator
extends Object
implements ByteBufferAllocator

A 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().

Version:
$Rev: 451854 $, $Date: 2006-10-02 11:30:11 +0900 (월, 02 10월 2006) $
Author:
The Apache Directory Project (mina-dev@directory.apache.org)

Constructor Summary
PooledByteBufferAllocator()
          Creates a new instance with the default timeout.
PooledByteBufferAllocator(int timeout)
          Creates a new instance with the specified timeout.
 
Method Summary
 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(ByteBuffer nioBuffer)
          Wraps the specified NIO ByteBuffer into MINA buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PooledByteBufferAllocator

public PooledByteBufferAllocator()
Creates a new instance with the default timeout.


PooledByteBufferAllocator

public PooledByteBufferAllocator(int timeout)
Creates a new instance with the specified timeout.

Method Detail

dispose

public void dispose()
Stops the thread which releases unused buffers and make this allocator unusable from now on.

Specified by:
dispose in interface ByteBufferAllocator

getTimeout

public int getTimeout()
Returns the timeout value of this allocator in seconds.


getTimeoutMillis

public long getTimeoutMillis()
Returns the timeout value of this allocator in milliseconds.


setTimeout

public void setTimeout(int timeout)
Sets the timeout value of this allocator in seconds.

Parameters:
timeout - 0 or negative value to disable timeout.

allocate

public ByteBuffer allocate(int capacity,
                           boolean direct)
Description copied from interface: ByteBufferAllocator
Returns the buffer which is capable of the specified size.

Specified by:
allocate in interface ByteBufferAllocator
Parameters:
capacity - the capacity of the buffer
direct - true to get a direct buffer, false to get a heap buffer.

wrap

public ByteBuffer wrap(ByteBuffer nioBuffer)
Description copied from interface: ByteBufferAllocator
Wraps the specified NIO ByteBuffer into MINA buffer.

Specified by:
wrap in interface ByteBufferAllocator