org.glassfish.grizzly.memory
Class ByteBufferManager

java.lang.Object
  extended by org.glassfish.grizzly.memory.AbstractMemoryManager<ByteBufferWrapper>
      extended by org.glassfish.grizzly.memory.ByteBufferManager
All Implemented Interfaces:
ByteBufferAware, MemoryManager<ByteBufferWrapper>, ThreadLocalPoolProvider, WrapperAware, JmxMonitoringAware<MemoryProbe>, MonitoringAware<MemoryProbe>

public class ByteBufferManager
extends AbstractMemoryManager<ByteBufferWrapper>
implements WrapperAware, ByteBufferAware

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

Nested Class Summary
protected  class ByteBufferManager.SmallByteBufferWrapper
          ByteBufferWrapper implementation, which supports trimming.
 
Nested classes/interfaces inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
AbstractMemoryManager.TrimAware
 
Field Summary
static int DEFAULT_SMALL_BUFFER_SIZE
          TODO: Document
protected  boolean isDirect
          Is direct ByteBuffer should be used?
protected  int maxSmallBufferSize
           
 
Fields inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
DEFAULT_MAX_BUFFER_SIZE, maxBufferSize, monitoringConfig
 
Fields inherited from interface org.glassfish.grizzly.memory.MemoryManager
DEFAULT_MEMORY_MANAGER
 
Constructor Summary
ByteBufferManager()
           
ByteBufferManager(boolean isDirect, int maxBufferSize, int maxSmallBufferSize)
           
 
Method Summary
 ByteBufferWrapper allocate(int size)
          Allocated Buffer of the required size.
 ByteBufferWrapper allocateAtLeast(int size)
          Allocated Buffer at least of the provided size.
 ByteBuffer allocateByteBuffer(int size)
          Allocates ByteBuffer of required size.
protected  ByteBuffer allocateByteBuffer0(int size)
           
 ByteBuffer allocateByteBufferAtLeast(int size)
          Allocates ByteBuffer of required size.
protected  JmxObject createJmxManagementObject()
          Create the Memory Manager JMX management object.
protected  ByteBufferManager.SmallByteBufferWrapper createSmallBuffer()
           
 ThreadLocalPool createThreadLocalPool()
           
 int getMaxSmallBufferSize()
           
 JmxMonitoringConfig<MemoryProbe> getMonitoringConfig()
          Return the object associated JmxMonitoringConfig.
 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.
 ByteBuffer reallocateByteBuffer(ByteBuffer oldByteBuffer, int newSize)
           
 void release(ByteBufferWrapper buffer)
          Lets JVM Garbage collector to release buffer.
 void releaseByteBuffer(ByteBuffer byteBuffer)
           
 void setDirect(boolean isDirect)
          Set true, if ByteBufferManager works with direct ByteBuffers, or false otherwise.
 boolean willAllocateDirect(int size)
          Return true if next MemoryManager.allocate(int) or MemoryManager.allocateAtLeast(int) call, made in the current thread for the given memory size, going to return a Buffer based on direct ByteBuffer, 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 org.glassfish.grizzly.memory.AbstractMemoryManager
allocateFromPool, getMaxBufferSize, getReadyThreadBufferSize, getThreadLocalPool
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SMALL_BUFFER_SIZE

public static final int DEFAULT_SMALL_BUFFER_SIZE
TODO: Document

See Also:
Constant Field Values

isDirect

protected boolean isDirect
Is direct ByteBuffer should be used?


maxSmallBufferSize

protected final int maxSmallBufferSize
Constructor Detail

ByteBufferManager

public ByteBufferManager()

ByteBufferManager

public ByteBufferManager(boolean isDirect,
                         int maxBufferSize,
                         int maxSmallBufferSize)
Method Detail

getMaxSmallBufferSize

public int getMaxSmallBufferSize()

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.

allocateAtLeast

public ByteBufferWrapper allocateAtLeast(int size)
Allocated Buffer at least of the provided size. This could be useful for usecases like Socket.read(...), where we're not sure how many bytes are available, but want to read as much as possible.

Specified by:
allocateAtLeast in interface MemoryManager<ByteBufferWrapper>
Parameters:
size - the min 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.

willAllocateDirect

public boolean willAllocateDirect(int size)
Return true if next MemoryManager.allocate(int) or MemoryManager.allocateAtLeast(int) call, made in the current thread for the given memory size, going to return a Buffer based on direct ByteBuffer, or false otherwise.

Specified by:
willAllocateDirect in interface MemoryManager<ByteBufferWrapper>
Returns:

wrap

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

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

createThreadLocalPool

public ThreadLocalPool createThreadLocalPool()
Specified by:
createThreadLocalPool in interface ThreadLocalPoolProvider
Returns:
a new ThreadLocalPool implementation. This method must return a new ThreadLocalPool instance per invocation.

wrap

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

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

allocateByteBuffer

public ByteBuffer allocateByteBuffer(int size)
Allocates ByteBuffer of required size.

Specified by:
allocateByteBuffer in interface ByteBufferAware
Parameters:
size - ByteBuffer size.
Returns:
allocated ByteBuffer.

allocateByteBufferAtLeast

public ByteBuffer allocateByteBufferAtLeast(int size)
Allocates ByteBuffer of required size.

Specified by:
allocateByteBufferAtLeast in interface ByteBufferAware
Parameters:
size - ByteBuffer size.
Returns:
allocated ByteBuffer.

reallocateByteBuffer

public ByteBuffer reallocateByteBuffer(ByteBuffer oldByteBuffer,
                                       int newSize)
Specified by:
reallocateByteBuffer in interface ByteBufferAware

releaseByteBuffer

public void releaseByteBuffer(ByteBuffer byteBuffer)
Specified by:
releaseByteBuffer in interface ByteBufferAware

createSmallBuffer

protected ByteBufferManager.SmallByteBufferWrapper createSmallBuffer()

getMonitoringConfig

public JmxMonitoringConfig<MemoryProbe> getMonitoringConfig()
Description copied from interface: JmxMonitoringAware
Return the object associated JmxMonitoringConfig.

Specified by:
getMonitoringConfig in interface JmxMonitoringAware<MemoryProbe>
Specified by:
getMonitoringConfig in interface MonitoringAware<MemoryProbe>
Returns:
the object associated JmxMonitoringConfig.

createJmxManagementObject

protected JmxObject createJmxManagementObject()
Create the Memory Manager JMX management object.

Specified by:
createJmxManagementObject in class AbstractMemoryManager<ByteBufferWrapper>
Returns:
the Memory Manager JMX management object.

allocateByteBuffer0

protected final ByteBuffer allocateByteBuffer0(int size)


Copyright © 2013 Oracle Corporation. All Rights Reserved.