Package org.jboss.netty.buffer
Class DirectChannelBufferFactory
java.lang.Object
org.jboss.netty.buffer.AbstractChannelBufferFactory
org.jboss.netty.buffer.DirectChannelBufferFactory
- All Implemented Interfaces:
ChannelBufferFactory
A
ChannelBufferFactory
which pre-allocates a large chunk of direct
buffer and returns its slice on demand. Direct buffers are reclaimed via
ReferenceQueue
in most JDK implementations, and therefore they are
deallocated less efficiently than an ordinary heap buffer. Consequently,
a user will get OutOfMemoryError
when 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
FieldsModifier and TypeFieldDescriptionprivate final Object
private static final DirectChannelBufferFactory
private static final DirectChannelBufferFactory
private final Object
private ChannelBuffer
private int
private final int
private ChannelBuffer
private int
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new factory whose defaultByteOrder
isByteOrder.BIG_ENDIAN
.DirectChannelBufferFactory
(int preallocatedBufferCapacity) Creates a new factory whose defaultByteOrder
isByteOrder.BIG_ENDIAN
.DirectChannelBufferFactory
(ByteOrder defaultOrder) Creates a new factory with the specified defaultByteOrder
.DirectChannelBufferFactory
(ByteOrder defaultOrder, int preallocatedBufferCapacity) Creates a new factory with the specified defaultByteOrder
. -
Method Summary
Modifier and TypeMethodDescriptionprivate ChannelBuffer
allocateBigEndianBuffer
(int capacity) private ChannelBuffer
allocateLittleEndianBuffer
(int capacity) getBuffer
(ByteBuffer nioBuffer) Returns aChannelBuffer
whose content is equal to the sub-region of the specifiednioBuffer
.Returns aChannelBuffer
whose content is equal to the sub-region of the specifiedarray
.static ChannelBufferFactory
static ChannelBufferFactory
getInstance
(ByteOrder defaultEndianness) Methods inherited from class org.jboss.netty.buffer.AbstractChannelBufferFactory
getBuffer, getBuffer, getDefaultOrder
-
Field Details
-
INSTANCE_BE
-
INSTANCE_LE
-
bigEndianLock
-
littleEndianLock
-
preallocatedBufCapacity
private final int preallocatedBufCapacity -
preallocatedBEBuf
-
preallocatedBEBufPos
private int preallocatedBEBufPos -
preallocatedLEBuf
-
preallocatedLEBufPos
private int preallocatedLEBufPos
-
-
Constructor Details
-
DirectChannelBufferFactory
public DirectChannelBufferFactory()Creates a new factory whose defaultByteOrder
isByteOrder.BIG_ENDIAN
. -
DirectChannelBufferFactory
public DirectChannelBufferFactory(int preallocatedBufferCapacity) Creates a new factory whose defaultByteOrder
isByteOrder.BIG_ENDIAN
. -
DirectChannelBufferFactory
Creates a new factory with the specified defaultByteOrder
.- Parameters:
defaultOrder
- the defaultByteOrder
of this factory
-
DirectChannelBufferFactory
Creates a new factory with the specified defaultByteOrder
.- Parameters:
defaultOrder
- the defaultByteOrder
of this factory
-
-
Method Details
-
getInstance
-
getInstance
-
getBuffer
Description copied from interface:ChannelBufferFactory
- Parameters:
order
- the endianness of the returnedChannelBuffer
capacity
- the capacity of the returnedChannelBuffer
- Returns:
- a
ChannelBuffer
with the specifiedendianness
andcapacity
, whosereaderIndex
andwriterIndex
are0
-
getBuffer
Description copied from interface:ChannelBufferFactory
Returns aChannelBuffer
whose content is equal to the sub-region of the specifiedarray
. Depending on the factory implementation, the returned buffer could wrap thearray
or create a new copy of thearray
.- Parameters:
order
- the endianness of the returnedChannelBuffer
array
- the byte arrayoffset
- the offset of the byte arraylength
- the length of the byte array- Returns:
- a
ChannelBuffer
with the specified content, whosereaderIndex
andwriterIndex
are0
and(length - offset)
respectively
-
getBuffer
Description copied from interface:ChannelBufferFactory
Returns aChannelBuffer
whose content is equal to the sub-region of the specifiednioBuffer
. Depending on the factory implementation, the returned buffer could wrap thenioBuffer
or create a new copy of thenioBuffer
.- Parameters:
nioBuffer
- the NIOByteBuffer
- Returns:
- a
ChannelBuffer
with the specified content, whosereaderIndex
andwriterIndex
are0
andnioBuffer.remaining()
respectively
-
allocateBigEndianBuffer
-
allocateLittleEndianBuffer
-