com.sun.grizzly.memory
Class ByteBufferManager

java.lang.Object
  extended by com.sun.grizzly.memory.ByteBufferManager
All Implemented Interfaces:
MemoryManager<ByteBufferWrapper>, WrapperAware<ByteBufferWrapper>
Direct Known Subclasses:
ByteBufferViewManager, SlabMemoryManagerBase

public class ByteBufferManager
extends Object
implements MemoryManager<ByteBufferWrapper>, WrapperAware<ByteBufferWrapper>

The simple Buffer manager implementation, which works as wrapper above ByteBuffers. It's possible to work either with direct or heap ByteBuffers.

Author:
Jean-Francois Arcand, Alexey Stashok
See Also:
MemoryManager, ByteBuffer

Field Summary
protected  boolean isDirect
          Is direct ByteBuffer should be used?
 
Constructor Summary
ByteBufferManager()
           
ByteBufferManager(boolean isDirect)
           
 
Method Summary
 ByteBufferWrapper allocate(int size)
          Allocated Buffer of the required size.
protected  ByteBuffer allocate0(int size)
          Allocates ByteBuffer of required size.
 boolean isDirect()
          Returns true, if ByteBufferManager works with direct ByteBuffers, or false otherwise.
 ByteBufferWrapper reallocate(ByteBufferWrapper oldBuffer, int newSize)
          Reallocate Buffer to a required size.
 void release(ByteBufferWrapper buffer)
          Lets JVM Garbage collector to release buffer.
 void setDirect(boolean isDirect)
          Set true, if ByteBufferManager works with direct ByteBuffers, or false otherwise.
 ByteBufferWrapper wrap(byte[] data)
          Returns Buffer, which wraps the byte array.
 ByteBufferWrapper wrap(byte[] data, int offset, int length)
          Returns Buffer, which wraps the part of byte array with specific offset and length.
 ByteBufferWrapper wrap(ByteBuffer byteBuffer)
          Returns Buffer, which wraps the ByteBuffer.
 ByteBufferWrapper wrap(String s)
          Returns Buffer, which wraps the String.
 ByteBufferWrapper wrap(String s, Charset charset)
          Returns Buffer, which wraps the String with the specific Charset.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

isDirect

protected boolean isDirect
Is direct ByteBuffer should be used?

Constructor Detail

ByteBufferManager

public ByteBufferManager()

ByteBufferManager

public ByteBufferManager(boolean isDirect)
Method Detail

allocate0

protected ByteBuffer allocate0(int size)
Allocates ByteBuffer of required size.

Parameters:
size - ByteBuffer size.
Returns:
allocated ByteBuffer.

allocate

public ByteBufferWrapper allocate(int size)
Allocated Buffer of the required size.

Specified by:
allocate in interface MemoryManager<ByteBufferWrapper>
Parameters:
size - Buffer size to be allocated.
Returns:
allocated Buffer.

reallocate

public ByteBufferWrapper reallocate(ByteBufferWrapper oldBuffer,
                                    int newSize)
Reallocate Buffer to a required size. Implementation may choose the way, how reallocation could be done, either by allocating new Buffer of required size and copying old Buffer content there, or perform more complex logic related to memory pooling etc.

Specified by:
reallocate in interface MemoryManager<ByteBufferWrapper>
Parameters:
oldBuffer - old Buffer to be reallocated.
newSize - new Buffer required size.
Returns:
reallocated Buffer.

release

public void release(ByteBufferWrapper buffer)
Lets JVM Garbage collector to release buffer.

Specified by:
release in interface MemoryManager<ByteBufferWrapper>
Parameters:
buffer - Buffer to be released.

isDirect

public boolean isDirect()
Returns true, if ByteBufferManager works with direct ByteBuffers, or false otherwise.

Returns:
true, if ByteBufferManager works with direct ByteBuffers, or false otherwise.

setDirect

public void setDirect(boolean isDirect)
Set true, if ByteBufferManager works with direct ByteBuffers, or false otherwise.

Parameters:
isDirect - true, if ByteBufferManager works with direct ByteBuffers, or false otherwise.

wrap

public ByteBufferWrapper wrap(byte[] data)
Returns Buffer, which wraps the byte array.

Specified by:
wrap in interface WrapperAware<ByteBufferWrapper>
Parameters:
data - byte array to wrap
Returns:
Buffer wrapper on top of passed byte array.

wrap

public ByteBufferWrapper wrap(byte[] data,
                              int offset,
                              int length)
Returns Buffer, which wraps the part of byte array with specific offset and length.

Specified by:
wrap in interface WrapperAware<ByteBufferWrapper>
Parameters:
data - byte array to wrap
offset - byte buffer offset
length - byte buffer length
Returns:
Buffer wrapper on top of passed byte array.

wrap

public ByteBufferWrapper wrap(String s)
Returns Buffer, which wraps the String.

Specified by:
wrap in interface WrapperAware<ByteBufferWrapper>
Parameters:
s - String
Returns:
Buffer wrapper on top of passed String.

wrap

public ByteBufferWrapper wrap(String s,
                              Charset charset)
Returns Buffer, which wraps the String with the specific Charset.

Specified by:
wrap in interface WrapperAware<ByteBufferWrapper>
Parameters:
s - String
charset - Charset, which will be used, when converting String to byte array.
Returns:
Buffer wrapper on top of passed String.

wrap

public ByteBufferWrapper wrap(ByteBuffer byteBuffer)
Returns Buffer, which wraps the ByteBuffer.

Specified by:
wrap in interface WrapperAware<ByteBufferWrapper>
Parameters:
byteBuffer - ByteBuffer to wrap
Returns:
Buffer wrapper on top of passed ByteBuffer.


Copyright © 2009 SUN Microsystems. All Rights Reserved.