Package org.glassfish.grizzly.memory
Class ByteBufferManager
- java.lang.Object
-
- org.glassfish.grizzly.memory.AbstractMemoryManager<ByteBufferWrapper>
-
- org.glassfish.grizzly.memory.ByteBufferManager
-
- All Implemented Interfaces:
ByteBufferAware,MemoryManager<ByteBufferWrapper>,ThreadLocalPoolProvider,WrapperAware,MonitoringAware<MemoryProbe>
public class ByteBufferManager extends AbstractMemoryManager<ByteBufferWrapper> implements WrapperAware, ByteBufferAware
The simple Buffer manager implementation, which works as wrapper aboveByteBuffers. It's possible to work either with direct or heapByteBuffers.- Author:
- Jean-Francois Arcand, Alexey Stashok
- See Also:
MemoryManager,ByteBuffer
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classByteBufferManager.SmallByteBufferWrapperByteBufferWrapperimplementation, which supports trimming.-
Nested classes/interfaces inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
AbstractMemoryManager.TrimAware
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_SMALL_BUFFER_SIZETODO: Documentprotected booleanisDirectIs direct ByteBuffer should be used?protected intmaxSmallBufferSize-
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
Constructors Constructor Description ByteBufferManager()ByteBufferManager(boolean isDirect)ByteBufferManager(boolean isDirect, int maxBufferSize, int maxSmallBufferSize)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteBufferWrapperallocate(int size)AllocatedBufferof the required size.ByteBufferWrapperallocateAtLeast(int size)AllocatedBufferat least of the provided size.ByteBufferallocateByteBuffer(int size)AllocatesByteBufferof required size.protected ByteBufferallocateByteBuffer0(int size)ByteBufferallocateByteBufferAtLeast(int size)AllocatesByteBufferof required size.protected ObjectcreateJmxManagementObject()Create the Memory Manager JMX management object.protected ByteBufferManager.SmallByteBufferWrappercreateSmallBuffer()ThreadLocalPoolcreateThreadLocalPool()intgetMaxSmallBufferSize()MonitoringConfig<MemoryProbe>getMonitoringConfig()Return the object associatedMonitoringConfig.booleanisDirect()Returns true, if ByteBufferManager works with directByteBuffers, or false otherwise.ByteBufferWrapperreallocate(ByteBufferWrapper oldBuffer, int newSize)ReallocateBufferto a required size.ByteBufferreallocateByteBuffer(ByteBuffer oldByteBuffer, int newSize)voidrelease(ByteBufferWrapper buffer)Lets JVM Garbage collector to release buffer.voidreleaseByteBuffer(ByteBuffer byteBuffer)voidsetDirect(boolean isDirect)Set true, if ByteBufferManager works with directByteBuffers, or false otherwise.booleanwillAllocateDirect(int size)Return true if nextMemoryManager.allocate(int)orMemoryManager.allocateAtLeast(int)call, made in the current thread for the given memory size, going to return aBufferbased on directByteBuffer, or false otherwise.ByteBufferWrapperwrap(byte[] data)ReturnsBuffer, which wraps the byte array.ByteBufferWrapperwrap(byte[] data, int offset, int length)ReturnsBuffer, which wraps the part of byte array with specific offset and length.ByteBufferWrapperwrap(String s)ByteBufferWrapperwrap(String s, Charset charset)ByteBufferWrapperwrap(ByteBuffer byteBuffer)ReturnsBuffer, which wraps theByteBuffer.-
Methods inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
allocateFromPool, getMaxBufferSize, getReadyThreadBufferSize, getThreadLocalPool
-
-
-
-
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
-
-
Method Detail
-
getMaxSmallBufferSize
public int getMaxSmallBufferSize()
-
allocate
public ByteBufferWrapper allocate(int size)
AllocatedBufferof the required size.- Specified by:
allocatein interfaceMemoryManager<ByteBufferWrapper>- Parameters:
size-Buffersize to be allocated.- Returns:
- allocated
Buffer.
-
allocateAtLeast
public ByteBufferWrapper allocateAtLeast(int size)
AllocatedBufferat 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:
allocateAtLeastin interfaceMemoryManager<ByteBufferWrapper>- Parameters:
size- the minBuffersize to be allocated.- Returns:
- allocated
Buffer.
-
reallocate
public ByteBufferWrapper reallocate(ByteBufferWrapper oldBuffer, int newSize)
ReallocateBufferto a required size. Implementation may choose the way, how reallocation could be done, either by allocating newBufferof required size and copying oldBuffercontent there, or perform more complex logic related to memory pooling etc.- Specified by:
reallocatein interfaceMemoryManager<ByteBufferWrapper>- Parameters:
oldBuffer- oldBufferto be reallocated.newSize- newBufferrequired size.- Returns:
- reallocated
Buffer.
-
release
public void release(ByteBufferWrapper buffer)
Lets JVM Garbage collector to release buffer.- Specified by:
releasein interfaceMemoryManager<ByteBufferWrapper>- Parameters:
buffer-Bufferto be released.
-
isDirect
public boolean isDirect()
Returns true, if ByteBufferManager works with directByteBuffers, 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 directByteBuffers, or false otherwise.- Parameters:
isDirect- true, if ByteBufferManager works with directByteBuffers, or false otherwise.
-
willAllocateDirect
public boolean willAllocateDirect(int size)
Return true if nextMemoryManager.allocate(int)orMemoryManager.allocateAtLeast(int)call, made in the current thread for the given memory size, going to return aBufferbased on directByteBuffer, or false otherwise.- Specified by:
willAllocateDirectin interfaceMemoryManager<ByteBufferWrapper>- Returns:
-
wrap
public ByteBufferWrapper wrap(byte[] data)
ReturnsBuffer, which wraps the byte array.- Specified by:
wrapin interfaceWrapperAware- Parameters:
data- byte array to wrap- Returns:
Bufferwrapper on top of passed byte array.
-
wrap
public ByteBufferWrapper wrap(byte[] data, int offset, int length)
ReturnsBuffer, which wraps the part of byte array with specific offset and length.- Specified by:
wrapin interfaceWrapperAware- Parameters:
data- byte array to wrapoffset- byte buffer offsetlength- byte buffer length- Returns:
Bufferwrapper on top of passed byte array.
-
wrap
public ByteBufferWrapper wrap(String s)
- Specified by:
wrapin interfaceWrapperAware- Parameters:
s-String- Returns:
Bufferwrapper on top of passedString.
-
wrap
public ByteBufferWrapper wrap(String s, Charset charset)
-
createThreadLocalPool
public ThreadLocalPool createThreadLocalPool()
- Specified by:
createThreadLocalPoolin interfaceThreadLocalPoolProvider- Returns:
- a new
ThreadLocalPoolimplementation. This method must return a newThreadLocalPoolinstance per invocation.
-
wrap
public ByteBufferWrapper wrap(ByteBuffer byteBuffer)
ReturnsBuffer, which wraps theByteBuffer.- Specified by:
wrapin interfaceWrapperAware- Parameters:
byteBuffer-ByteBufferto wrap- Returns:
Bufferwrapper on top of passedByteBuffer.
-
allocateByteBuffer
public ByteBuffer allocateByteBuffer(int size)
AllocatesByteBufferof required size.- Specified by:
allocateByteBufferin interfaceByteBufferAware- Parameters:
size-ByteBuffersize.- Returns:
- allocated
ByteBuffer.
-
allocateByteBufferAtLeast
public ByteBuffer allocateByteBufferAtLeast(int size)
AllocatesByteBufferof required size.- Specified by:
allocateByteBufferAtLeastin interfaceByteBufferAware- Parameters:
size-ByteBuffersize.- Returns:
- allocated
ByteBuffer.
-
reallocateByteBuffer
public ByteBuffer reallocateByteBuffer(ByteBuffer oldByteBuffer, int newSize)
- Specified by:
reallocateByteBufferin interfaceByteBufferAware
-
releaseByteBuffer
public void releaseByteBuffer(ByteBuffer byteBuffer)
- Specified by:
releaseByteBufferin interfaceByteBufferAware
-
createSmallBuffer
protected ByteBufferManager.SmallByteBufferWrapper createSmallBuffer()
-
getMonitoringConfig
public MonitoringConfig<MemoryProbe> getMonitoringConfig()
Description copied from interface:MonitoringAwareReturn the object associatedMonitoringConfig.- Specified by:
getMonitoringConfigin interfaceMonitoringAware<MemoryProbe>- Returns:
- the object associated
MonitoringConfig.
-
createJmxManagementObject
protected Object createJmxManagementObject()
Create the Memory Manager JMX management object.- Specified by:
createJmxManagementObjectin classAbstractMemoryManager<ByteBufferWrapper>- Returns:
- the Memory Manager JMX management object.
-
allocateByteBuffer0
protected final ByteBuffer allocateByteBuffer0(int size)
-
-