A byte buffer used by MINA applications.
This is a replacement for ByteBuffer. MINA does not use NIO ByteBuffer directly for two reasons:
- It doesn't provide useful getters and putters such as fill, get/putString, and get/putAsciiInt() .
- It is difficult to write variable-length data due to its fixed capacity
Allocating a new Buffer
IoBuffer is an abstract class, hence can't be instantiated directly. To allocate IoBuffer, we need to use one of the two allocate() methods.
allocate() method takes one or two arguments. The first form takes two arguments :
- capacity - the capacity of the buffer
- direct - type of buffer. true to get direct buffer, false to get heap buffer
The default buffer allocation is handled by SimpleBufferAllocator
Alternatively, following form can also be used
When using the second form, don't forget to set the default buffer type before, otherwise you will get Heap buffers by default.
Creating Auto Expanding Buffer
Creating auto expanding buffer is not very easy with java NIO API's, because of the fixed size of the buffers. Having a buffer, that can auto expand on needs is a big plus for networking applications. To address this, IoBuffer has introduced the
autoExpand property. It automatically expands its capacity and limit value.
Lets see how to create an auto expanding buffer :
The underlying ByteBuffer is reallocated by IoBuffer behind the scene if the encoded data is larger than 8 bytes in the example above. Its capacity will double, and its limit will increase to the last position the string is written. This behavior is very similar to the way StringBuffer class works.
Creating Auto Shrinking Buffer
There are situations which calls for releasing additionally allocated bytes from the buffer, to preserve memory. IoBuffer provides
autoShrink property to address the need. If autoShrink is turned on, IoBuffer halves the capacity of the buffer when compact() is invoked and only 1/4 or less of the current capacity is being used. To manually shrink the buffer, use
Lets see this in action :
We have initially allocated a capacity as 16, and set the autoShrink property as true.
Lets see the output of this :
Lets take a break and analyze the output
- Initial buffer capacity is 16, as we created the buffer with this capacity. Internally this becomes the minimum capacity of the buffer
- After calling
shrink(), the capacity remains 16, as capacity shall never be less than minimum capacity
- After incrementing capacity to 32, the capacity becomes 32
- Call to
shrink(), reduces the capacity to 16, thereby eliminating extra storage
IoBufferAllocater is responsible for allocating and managing buffers. To have precise control on the buffer allocation policy, implement the IoBufferAllocater interface.
MINA ships with following implementations of IoBufferAllocater
- SimpleBufferAllocator (default) - Create a new buffer every time
- CachedBufferAllocator - caches the buffer which are likely to be reused during expansion
You can implement you own implementation of IoBufferAllocator and call setAllocator() on IoBuffer to use the same.