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 above
ByteBuffers. It's possible to work
either with direct or heap ByteBuffers.- Author:
- Jean-Francois Arcand, Alexey Stashok
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected final classByteBufferWrapperimplementation, which supports trimming.Nested classes/interfaces inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
AbstractMemoryManager.TrimAware -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intTODO: Documentprotected booleanIs direct ByteBuffer should be used?protected final intFields inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
DEFAULT_MAX_BUFFER_SIZE, maxBufferSize, monitoringConfigFields inherited from interface org.glassfish.grizzly.memory.MemoryManager
DEFAULT_MEMORY_MANAGER -
Constructor Summary
ConstructorsConstructorDescriptionByteBufferManager(boolean isDirect) ByteBufferManager(boolean isDirect, int maxBufferSize, int maxSmallBufferSize) -
Method Summary
Modifier and TypeMethodDescriptionallocate(int size) AllocatedBufferof the required size.allocateAtLeast(int size) AllocatedBufferat least of the provided size.allocateByteBuffer(int size) AllocatesByteBufferof required size.protected final ByteBufferallocateByteBuffer0(int size) allocateByteBufferAtLeast(int size) AllocatesByteBufferof required size.protected ObjectCreate the Memory Manager JMX management object.protected ByteBufferManager.SmallByteBufferWrapperintReturn the object associatedMonitoringConfig.booleanisDirect()Returns true, if ByteBufferManager works with directByteBuffers, or false otherwise.reallocate(ByteBufferWrapper oldBuffer, int newSize) ReallocateBufferto a required size.reallocateByteBuffer(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.wrap(byte[] data) ReturnsBuffer, which wraps the byte array.wrap(byte[] data, int offset, int length) ReturnsBuffer, which wraps the part of byte array with specific offset and length.wrap(ByteBuffer byteBuffer) ReturnsBuffer, which wraps theByteBuffer.Methods inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
allocateFromPool, getMaxBufferSize, getReadyThreadBufferSize, getThreadLocalPool
-
Field Details
-
DEFAULT_SMALL_BUFFER_SIZE
public static final int DEFAULT_SMALL_BUFFER_SIZETODO: Document- See Also:
-
isDirect
protected boolean isDirectIs direct ByteBuffer should be used? -
maxSmallBufferSize
protected final int maxSmallBufferSize
-
-
Constructor Details
-
ByteBufferManager
public ByteBufferManager() -
ByteBufferManager
public ByteBufferManager(boolean isDirect) -
ByteBufferManager
public ByteBufferManager(boolean isDirect, int maxBufferSize, int maxSmallBufferSize)
-
-
Method Details
-
getMaxSmallBufferSize
public int getMaxSmallBufferSize() -
allocate
AllocatedBufferof the required size.- Specified by:
allocatein interfaceMemoryManager<ByteBufferWrapper>- Parameters:
size-Buffersize to be allocated.- Returns:
- allocated
Buffer.
-
allocateAtLeast
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
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
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
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
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
- Specified by:
wrapin interfaceWrapperAware- Parameters:
s-String- Returns:
Bufferwrapper on top of passedString.
-
wrap
-
createThreadLocalPool
- Specified by:
createThreadLocalPoolin interfaceThreadLocalPoolProvider- Returns:
- a new
ThreadLocalPoolimplementation. This method must return a newThreadLocalPoolinstance per invocation.
-
wrap
ReturnsBuffer, which wraps theByteBuffer.- Specified by:
wrapin interfaceWrapperAware- Parameters:
byteBuffer-ByteBufferto wrap- Returns:
Bufferwrapper on top of passedByteBuffer.
-
allocateByteBuffer
AllocatesByteBufferof required size.- Specified by:
allocateByteBufferin interfaceByteBufferAware- Parameters:
size-ByteBuffersize.- Returns:
- allocated
ByteBuffer.
-
allocateByteBufferAtLeast
AllocatesByteBufferof required size.- Specified by:
allocateByteBufferAtLeastin interfaceByteBufferAware- Parameters:
size-ByteBuffersize.- Returns:
- allocated
ByteBuffer.
-
reallocateByteBuffer
- Specified by:
reallocateByteBufferin interfaceByteBufferAware
-
releaseByteBuffer
- Specified by:
releaseByteBufferin interfaceByteBufferAware
-
createSmallBuffer
-
getMonitoringConfig
Description copied from interface:MonitoringAwareReturn the object associatedMonitoringConfig.- Specified by:
getMonitoringConfigin interfaceMonitoringAware<MemoryProbe>- Returns:
- the object associated
MonitoringConfig.
-
createJmxManagementObject
Create the Memory Manager JMX management object.- Specified by:
createJmxManagementObjectin classAbstractMemoryManager<ByteBufferWrapper>- Returns:
- the Memory Manager JMX management object.
-
allocateByteBuffer0
-