|
IoBuffer has been edited by Emmanuel Lécharny (Feb 26, 2009). Change summary: Updated the doco, fixing some typoes IntroductionA byte buffer used by MINA applications. This is a replacement for ByteBuffer
IoBuffer OperationsAllocating a new BufferIoBuffer is an abstract class, hence can't be instantiated directly. To allocate IoBuffer, we need to use one of the two allocate() methods. // Allocates a new buffer with a specific size, defining its type (direct or heap) public static IoBuffer allocate(int capacity, boolean direct) // Allocates a new buffer with a specific size public static IoBuffer allocate(int capacity) The allocate() method takes one or two arguments. The first form takes two arguments :
The default buffer allocation is handled by SimpleBufferAllocator Alternatively, following form can also be used // Allocates heap buffer by default. IoBuffer.setUseDirectBuffer(false); // A new heap buffer is returned. IoBuffer buf = IoBuffer.allocate(1024); 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 BufferCreating 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 : IoBuffer buffer = IoBuffer.allocate(8); buffer.setAutoExpand(true); buffer.putString("12345678", encoder); // Add more to this buffer buffer.put((byte)10); 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 BufferThere 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 shrink() method. Lets see this in action : IoBuffer buffer = IoBuffer.allocate(16); buffer.setAutoShrink(true); buffer.put((byte)1); System.out.println("Initial Buffer capacity = "+buffer.capacity()); buffer.shrink(); System.out.println("Initial Buffer capacity after shrink = "+buffer.capacity()); buffer.capacity(32); System.out.println("Buffer capacity after incrementing capacity to 32 = "+buffer.capacity()); buffer.shrink(); System.out.println("Buffer capacity after shrink= "+buffer.capacity()); We have initially allocated a capacity as 16, and set the autoShrink property as true. Lets see the output of this : Initial Buffer capacity = 16 Initial Buffer capacity after shrink = 16 Buffer capacity after incrementing capacity to 32 = 32 Buffer capacity after shrink= 16 Lets take a break and analyze the output
Buffer AllocationIoBufferAllocater 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
You can implement you own implementation of IoBufferAllocator and call setAllocator() on IoBuffer to use the same. |
Unsubscribe or edit your notifications preferences
