Class DirectChannelBufferFactory

java.lang.Object
org.jboss.netty.buffer.AbstractChannelBufferFactory
org.jboss.netty.buffer.DirectChannelBufferFactory
All Implemented Interfaces:
ChannelBufferFactory

public class DirectChannelBufferFactory extends AbstractChannelBufferFactory
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 Details

    • INSTANCE_BE

      private static final DirectChannelBufferFactory INSTANCE_BE
    • INSTANCE_LE

      private static final DirectChannelBufferFactory INSTANCE_LE
    • bigEndianLock

      private final Object bigEndianLock
    • littleEndianLock

      private final Object littleEndianLock
    • preallocatedBufCapacity

      private final int preallocatedBufCapacity
    • preallocatedBEBuf

      private ChannelBuffer preallocatedBEBuf
    • preallocatedBEBufPos

      private int preallocatedBEBufPos
    • preallocatedLEBuf

      private ChannelBuffer preallocatedLEBuf
    • preallocatedLEBufPos

      private int preallocatedLEBufPos
  • Constructor Details

    • DirectChannelBufferFactory

      public DirectChannelBufferFactory()
      Creates a new factory whose default ByteOrder is ByteOrder.BIG_ENDIAN.
    • DirectChannelBufferFactory

      public DirectChannelBufferFactory(int preallocatedBufferCapacity)
      Creates a new factory whose default ByteOrder is ByteOrder.BIG_ENDIAN.
    • DirectChannelBufferFactory

      public DirectChannelBufferFactory(ByteOrder defaultOrder)
      Creates a new factory with the specified default ByteOrder.
      Parameters:
      defaultOrder - the default ByteOrder of this factory
    • DirectChannelBufferFactory

      public DirectChannelBufferFactory(ByteOrder defaultOrder, int preallocatedBufferCapacity)
      Creates a new factory with the specified default ByteOrder.
      Parameters:
      defaultOrder - the default ByteOrder of this factory
  • Method Details

    • getInstance

      public static ChannelBufferFactory getInstance()
    • getInstance

      public static ChannelBufferFactory getInstance(ByteOrder defaultEndianness)
    • getBuffer

      public ChannelBuffer getBuffer(ByteOrder order, int capacity)
      Description copied from interface: ChannelBufferFactory
      Returns a ChannelBuffer with the specified endianness and capacity.
      Parameters:
      order - the endianness of the returned ChannelBuffer
      capacity - the capacity of the returned ChannelBuffer
      Returns:
      a ChannelBuffer with the specified endianness and capacity, whose readerIndex and writerIndex are 0
    • getBuffer

      public ChannelBuffer getBuffer(ByteOrder order, byte[] array, int offset, int length)
      Description copied from interface: ChannelBufferFactory
      Returns a ChannelBuffer whose content is equal to the sub-region of the specified array. Depending on the factory implementation, the returned buffer could wrap the array or create a new copy of the array.
      Parameters:
      order - the endianness of the returned ChannelBuffer
      array - the byte array
      offset - the offset of the byte array
      length - the length of the byte array
      Returns:
      a ChannelBuffer with the specified content, whose readerIndex and writerIndex are 0 and (length - offset) respectively
    • getBuffer

      public ChannelBuffer getBuffer(ByteBuffer nioBuffer)
      Description copied from interface: ChannelBufferFactory
      Returns a ChannelBuffer whose content is equal to the sub-region of the specified nioBuffer. Depending on the factory implementation, the returned buffer could wrap the nioBuffer or create a new copy of the nioBuffer.
      Parameters:
      nioBuffer - the NIO ByteBuffer
      Returns:
      a ChannelBuffer with the specified content, whose readerIndex and writerIndex are 0 and nioBuffer.remaining() respectively
    • allocateBigEndianBuffer

      private ChannelBuffer allocateBigEndianBuffer(int capacity)
    • allocateLittleEndianBuffer

      private ChannelBuffer allocateLittleEndianBuffer(int capacity)