cate heap buffer by default. IoBuffer.setUseDirectBuffer(false); // A new heap buffer is returned. IoBuffer buf = IoBuffer.allocate(1024);
Wrapping existing NIO buffers and arrays
This class provides a few wrap(...) methods that wraps any NIO buffers and byte arrays.
AutoExpand
Writing variable-length data using NIO ByteBuffers is not really easy, and it is because its size is fixed. {@link IoBuffer} introduces autoExpand property. If autoExpand property is true, you never get {@link BufferOverflowException} or{@link IndexOutOfBoundsException} (except when index is negative).It automatically expands its capacity and limit value. For example:
String greeting = messageBundle.getMessage( "hello" ); IoBuffer buf = IoBuffer.allocate( 16 ); // Turn on autoExpand (it is off by default) buf.setAutoExpand( true ); buf.putString( greeting, utf8encoder );
The underlying {@link ByteBuffer} is reallocated by {@link IoBuffer} behindthe scene if the encoded data is larger than 16 bytes in the example above. Its capacity will double, and its limit will increase to the last position the string is written.
AutoShrink
You might also want to decrease the capacity of the buffer when most of the allocated memory area is not being used. {@link IoBuffer} providesautoShrink property to take care of this issue. If autoShrink is turned on, {@link IoBuffer} halves the capacityof the buffer when {@link #compact()} is invoked and only 1/4 or less ofthe current capacity is being used.
You can also {@link #shrink()} method manually to shrink the capacity ofthe buffer.
The underlying {@link ByteBuffer} is reallocated by {@link IoBuffer} behindthe scene, and therefore {@link #buf()} will return a different{@link ByteBuffer} instance once capacity changes. Please also note{@link #compact()} or {@link #shrink()} will not decrease the capacity ifthe new capacity is less than the {@link #minimumCapacity()} of the buffer.
Derived Buffers
Derived buffers are the buffers which were created by {@link #duplicate()}, {@link #slice()}, or {@link #asReadOnlyBuffer()}. They are useful especially when you broadcast the same messages to multiple {@link IoSession}s. Please note that the buffer derived from and its derived buffers are not both auto-expandable neither auto-shrinkable. Trying to call {@link #setAutoExpand(boolean)} or {@link #setAutoShrink(boolean)}with true parameter will raise an {@link IllegalStateException}.
Changing Buffer Allocation Policy
{@link IoBufferAllocator} interface lets you override the default buffermanagement behavior. There are two allocators provided out-of-the-box:
- {@link SimpleBufferAllocator} (default)
- {@link CachedBufferAllocator}
You can implement your own allocator and use it by calling {@link #setAllocator(IoBufferAllocator)}.
@author The Apache MINA Project (dev@mina.apache.org)
@version $Rev: 637706 $, $Date: 2008-03-17 12:18:43 +0900 (월, 17 3월 2008) $