Package org.jboss.netty.buffer
Class DirectChannelBufferFactory
- java.lang.Object
-
- org.jboss.netty.buffer.AbstractChannelBufferFactory
-
- org.jboss.netty.buffer.DirectChannelBufferFactory
-
- All Implemented Interfaces:
ChannelBufferFactory
public class DirectChannelBufferFactory extends AbstractChannelBufferFactory
AChannelBufferFactorywhich pre-allocates a large chunk of direct buffer and returns its slice on demand. Direct buffers are reclaimed viaReferenceQueuein most JDK implementations, and therefore they are deallocated less efficiently than an ordinary heap buffer. Consequently, a user will getOutOfMemoryErrorwhen one tries to allocate small direct buffers more often than the GC throughput of direct buffers, which is much lower than the GC throughput of heap buffers. This factory avoids this problem by allocating a large chunk of pre-allocated direct buffer and reducing the number of the garbage collected internal direct buffer objects.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.ObjectbigEndianLockprivate static DirectChannelBufferFactoryINSTANCE_BEprivate static DirectChannelBufferFactoryINSTANCE_LEprivate java.lang.ObjectlittleEndianLockprivate ChannelBufferpreallocatedBEBufprivate intpreallocatedBEBufPosprivate intpreallocatedBufCapacityprivate ChannelBufferpreallocatedLEBufprivate intpreallocatedLEBufPos
-
Constructor Summary
Constructors Constructor Description DirectChannelBufferFactory()Creates a new factory whose defaultByteOrderisByteOrder.BIG_ENDIAN.DirectChannelBufferFactory(int preallocatedBufferCapacity)Creates a new factory whose defaultByteOrderisByteOrder.BIG_ENDIAN.DirectChannelBufferFactory(java.nio.ByteOrder defaultOrder)Creates a new factory with the specified defaultByteOrder.DirectChannelBufferFactory(java.nio.ByteOrder defaultOrder, int preallocatedBufferCapacity)Creates a new factory with the specified defaultByteOrder.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private ChannelBufferallocateBigEndianBuffer(int capacity)private ChannelBufferallocateLittleEndianBuffer(int capacity)ChannelBuffergetBuffer(java.nio.ByteBuffer nioBuffer)Returns aChannelBufferwhose content is equal to the sub-region of the specifiednioBuffer.ChannelBuffergetBuffer(java.nio.ByteOrder order, byte[] array, int offset, int length)Returns aChannelBufferwhose content is equal to the sub-region of the specifiedarray.ChannelBuffergetBuffer(java.nio.ByteOrder order, int capacity)static ChannelBufferFactorygetInstance()static ChannelBufferFactorygetInstance(java.nio.ByteOrder defaultEndianness)-
Methods inherited from class org.jboss.netty.buffer.AbstractChannelBufferFactory
getBuffer, getBuffer, getDefaultOrder
-
-
-
-
Field Detail
-
INSTANCE_BE
private static final DirectChannelBufferFactory INSTANCE_BE
-
INSTANCE_LE
private static final DirectChannelBufferFactory INSTANCE_LE
-
bigEndianLock
private final java.lang.Object bigEndianLock
-
littleEndianLock
private final java.lang.Object littleEndianLock
-
preallocatedBufCapacity
private final int preallocatedBufCapacity
-
preallocatedBEBuf
private ChannelBuffer preallocatedBEBuf
-
preallocatedBEBufPos
private int preallocatedBEBufPos
-
preallocatedLEBuf
private ChannelBuffer preallocatedLEBuf
-
preallocatedLEBufPos
private int preallocatedLEBufPos
-
-
Constructor Detail
-
DirectChannelBufferFactory
public DirectChannelBufferFactory()
Creates a new factory whose defaultByteOrderisByteOrder.BIG_ENDIAN.
-
DirectChannelBufferFactory
public DirectChannelBufferFactory(int preallocatedBufferCapacity)
Creates a new factory whose defaultByteOrderisByteOrder.BIG_ENDIAN.
-
DirectChannelBufferFactory
public DirectChannelBufferFactory(java.nio.ByteOrder defaultOrder)
Creates a new factory with the specified defaultByteOrder.- Parameters:
defaultOrder- the defaultByteOrderof this factory
-
DirectChannelBufferFactory
public DirectChannelBufferFactory(java.nio.ByteOrder defaultOrder, int preallocatedBufferCapacity)Creates a new factory with the specified defaultByteOrder.- Parameters:
defaultOrder- the defaultByteOrderof this factory
-
-
Method Detail
-
getInstance
public static ChannelBufferFactory getInstance()
-
getInstance
public static ChannelBufferFactory getInstance(java.nio.ByteOrder defaultEndianness)
-
getBuffer
public ChannelBuffer getBuffer(java.nio.ByteOrder order, int capacity)
Description copied from interface:ChannelBufferFactory- Parameters:
order- the endianness of the returnedChannelBuffercapacity- the capacity of the returnedChannelBuffer- Returns:
- a
ChannelBufferwith the specifiedendiannessandcapacity, whosereaderIndexandwriterIndexare0
-
getBuffer
public ChannelBuffer getBuffer(java.nio.ByteOrder order, byte[] array, int offset, int length)
Description copied from interface:ChannelBufferFactoryReturns aChannelBufferwhose content is equal to the sub-region of the specifiedarray. Depending on the factory implementation, the returned buffer could wrap thearrayor create a new copy of thearray.- Parameters:
order- the endianness of the returnedChannelBufferarray- the byte arrayoffset- the offset of the byte arraylength- the length of the byte array- Returns:
- a
ChannelBufferwith the specified content, whosereaderIndexandwriterIndexare0and(length - offset)respectively
-
getBuffer
public ChannelBuffer getBuffer(java.nio.ByteBuffer nioBuffer)
Description copied from interface:ChannelBufferFactoryReturns aChannelBufferwhose content is equal to the sub-region of the specifiednioBuffer. Depending on the factory implementation, the returned buffer could wrap thenioBufferor create a new copy of thenioBuffer.- Parameters:
nioBuffer- the NIOByteBuffer- Returns:
- a
ChannelBufferwith the specified content, whosereaderIndexandwriterIndexare0andnioBuffer.remaining()respectively
-
allocateBigEndianBuffer
private ChannelBuffer allocateBigEndianBuffer(int capacity)
-
allocateLittleEndianBuffer
private ChannelBuffer allocateLittleEndianBuffer(int capacity)
-
-