Top Description Interfaces Classes
module io.netty.buffer

Package io.netty.buffer


Abstraction of a byte buffer - the fundamental data structure to represent a low-level binary and text message. Netty uses its own buffer API instead of NIO java.nio.ByteBuffer to represent a sequence of bytes. This approach has significant advantage over using java.nio.ByteBuffer. Netty's new buffer type, io.netty.buffer.ByteBuf, has been designed from ground up to address the problems of java.nio.ByteBuffer and to meet the daily needs of network application developers. To list a few cool features:

Extensibility

io.netty.buffer.ByteBuf has rich set of operations optimized for rapid protocol implementation. For example, io.netty.buffer.ByteBuf provides various operations for accessing unsigned values and strings and searching for certain byte sequence in a buffer. You can also extend or wrap existing buffer type to add convenient accessors. The custom buffer type still implements io.netty.buffer.ByteBuf interface rather than introducing an incompatible type.

Transparent Zero Copy

To lift up the performance of a network application to the extreme, you need to reduce the number of memory copy operation. You might have a set of buffers that could be sliced and combined to compose a whole message. Netty provides a composite buffer which allows you to create a new buffer from the arbitrary number of existing buffers with no memory copy. For example, a message could be composed of two parts; header and body. In a modularized application, the two parts could be produced by different modules and assembled later when the message is sent out.
+--------+----------+
| header |   body   |
+--------+----------+
If java.nio.ByteBuffer were used, you would have to create a new big buffer and copy the two parts into the new buffer. Alternatively, you can perform a gathering write operation in NIO, but it restricts you to represent the composite of buffers as an array of java.nio.ByteBuffers rather than a single buffer, breaking the abstraction and introducing complicated state management. Moreover, it's of no use if you are not going to read or write from an NIO channel.
// The composite type is incompatible with the component type.
ByteBuffer[] message = new ByteBuffer[] { header, body };
By contrast, io.netty.buffer.ByteBuf does not have such caveats because it is fully extensible and has a built-in composite buffer type.
// The composite type is compatible with the component type.
io.netty.buffer.ByteBuf message = io.netty.buffer.Unpooled.wrappedBuffer(header, body);

// Therefore, you can even create a composite by mixing a composite and an
// ordinary buffer.
io.netty.buffer.ByteBuf messageWithFooter = io.netty.buffer.Unpooled.wrappedBuffer(message, footer);

// Because the composite is still a io.netty.buffer.ByteBuf, you can access its content
// easily, and the accessor method will behave just like it's a single buffer
// even if the region you want to access spans over multiple components.  The
// unsigned integer being read here is located across body and footer.
messageWithFooter.getUnsignedInt(
    messageWithFooter.readableBytes() - footer.readableBytes() - 1);

Automatic Capacity Extension

Many protocols define variable length messages, which means there's no way to determine the length of a message until you construct the message or it is difficult and inconvenient to calculate the length precisely. It is just like when you build a java.lang.String. You often estimate the length of the resulting string and let java.lang.StringBuffer expand itself on demand.
// A new dynamic buffer is created.  Internally, the actual buffer is created
// lazily to avoid potentially wasted memory space.
io.netty.buffer.ByteBuf b = io.netty.buffer.Unpooled.buffer(4);

// When the first write attempt is made, the internal buffer is created with
// the specified initial capacity (4).
b.writeByte('1');

b.writeByte('2');
b.writeByte('3');
b.writeByte('4');

// When the number of written bytes exceeds the initial capacity (4), the
// internal buffer is reallocated automatically with a larger capacity.
b.writeByte('5');

Better Performance

Most frequently used buffer implementation of io.netty.buffer.ByteBuf is a very thin wrapper of a byte array (i.e. byte[]). Unlike java.nio.ByteBuffer, it has no complicated boundary check and index compensation, and therefore it is easier for a JVM to optimize the buffer access. More complicated buffer implementation is used only for sliced or composite buffers, and it performs as well as java.nio.ByteBuffer.

Interface Summary

Modifier and TypeInterface and Description
public interface
ByteBufAllocator

Implementations are responsible to allocate buffers.

public interface
public interface
public interface
ByteBufConvertible

An interface that can be implemented by any object that know how to turn itself into a ByteBuf.

public interface
ByteBufHolder

A packet which is send or receive.

public interface
public interface
PoolArenaMetric

Expose metrics for an arena.

public interface
PoolChunkListMetric

Metrics for a list of chunks.

public interface
PoolChunkMetric

Metrics for a chunk.

public interface
PoolSubpageMetric

Metrics for a sub-page.

public interface
SizeClassesMetric

Expose metrics for an SizeClasses.

Class Summary

Modifier and TypeClass and Description
public abstract class
AbstractByteBuf

A skeletal implementation of a buffer.

public abstract class
AbstractByteBufAllocator

Skeletal ByteBufAllocator implementation to extend.

public abstract class
AbstractDerivedByteBuf

Deprecated Do not use.
Abstract base class for ByteBuf implementations that wrap another ByteBuf.
pack-priv abstract class
AbstractPooledDerivedByteBuf

Abstract base class for derived ByteBuf implementations.

public abstract class
AbstractReferenceCountedByteBuf

Abstract base class for ByteBuf implementations that count references.

pack-priv abstract class
AbstractUnpooledSlicedByteBuf

Deprecated as a consequence of AbstractDerivedByteBuf being deprecated.
pack-priv abstract class
AbstractUnsafeSwappedByteBuf

Deprecated as a consequence of SwappedByteBuf being deprecated.
Special SwappedByteBuf for ByteBufs that is using unsafe.
pack-priv class
pack-priv class
public abstract class
ByteBuf

A random and sequential accessible sequence of zero or more bytes (octets).

public class
ByteBufInputStream

An InputStream which reads data from a ByteBuf.

public class
ByteBufOutputStream

An OutputStream which writes data to a ByteBuf.

public class
ByteBufUtil

A collection of utility methods that is related with handling ByteBuf, such as the generation of hex dump and swapping an integer's byte order.

public class
CompositeByteBuf

A virtual buffer which shows multiple buffers as a single merged buffer.

public class
DefaultByteBufHolder

Default implementation of a ByteBufHolder that holds it's data in a ByteBuf.

public class
DuplicatedByteBuf

Deprecated Do not use.
A derived buffer which simply forwards all data access requests to its parent.
public class
EmptyByteBuf

An empty ByteBuf whose capacity and maximum capacity are all 0.

pack-priv class
FixedCompositeByteBuf

ByteBuf implementation which allows to wrap an array of ByteBuf in a read-only mode.

pack-priv class
HeapByteBufUtil

Utility class for heap buffers.

pack-priv class
IntPriorityQueue

Internal primitive priority queue, used by PoolChunk.

pack-priv class
LongLongHashMap

Internal primitive map implementation that is specifically optimised for the runs availability map use case in PoolChunk.

pack-priv abstract class
pack-priv class
PoolChunk<T>

Description of algorithm for PageRun/PoolSubpage allocation from PoolChunk Notation: The following terms are important to understand the code > page - a page is the smallest unit of memory chunk that can be allocated > run - a run is a collection of pages > chunk - a chunk is a collection of runs > in this code chunkSize = maxPages * pageSize To begin we allocate a byte array of size = chunkSize Whenever a ByteBuf of given size needs to be created we search for the first position in the byte array that has enough empty space to accommodate the requested size and return a (long) handle that encodes this offset information, (this memory segment is then marked as reserved so it is always used by exactly one ByteBuf and no more) For simplicity all sizes are normalized according to PoolArena#sizeClass#size2SizeIdx(int) method.

pack-priv class
pack-priv abstract class
public class
public class
pack-priv class
pack-priv class
pack-priv class
pack-priv class
pack-priv class
pack-priv class
pack-priv class
pack-priv class
PoolThreadCache

Acts a Thread cache for allocations.

public class
ReadOnlyByteBuf

Deprecated Do not use.
A derived buffer which forbids any write requests to its parent.
pack-priv class
ReadOnlyByteBufferBuf

Read-only ByteBuf which wraps a read-only ByteBuffer.

pack-priv class
ReadOnlyUnsafeDirectByteBuf

Read-only ByteBuf which wraps a read-only direct ByteBuffer and use unsafe for best performance.

pack-priv class
pack-priv class
pack-priv class
SizeClasses

SizeClasses requires pageShifts to be defined prior to inclusion, and it in turn defines:

LOG2_SIZE_CLASS_GROUP: Log of size class count for each size doubling.

public class
SlicedByteBuf

Deprecated Do not use.
A derived buffer which exposes its parent's sub-region only.
public class
SwappedByteBuf

Deprecated use the Little Endian accessors, e.g. getShortLE, getIntLE instead.
Wrapper which swap the ByteOrder of a ByteBuf.
public class
Unpooled

Creates a new ByteBuf by allocating new space or by wrapping or copying existing byte arrays, byte buffers and a string.

public class
UnpooledByteBufAllocator

Simplistic ByteBufAllocator implementation that does not pool anything.

public class
UnpooledDirectByteBuf

A NIO ByteBuffer based buffer.

pack-priv class
UnpooledDuplicatedByteBuf

Deprecated as a consequence of DuplicatedByteBuf being deprecated.
DuplicatedByteBuf implementation that can do optimizations because it knows the duplicated buffer is of type AbstractByteBuf.
public class
UnpooledHeapByteBuf

Big endian Java heap buffer implementation.

pack-priv class
UnpooledSlicedByteBuf

Deprecated as a consequence of AbstractUnpooledSlicedByteBuf being deprecated.
A special AbstractUnpooledSlicedByteBuf that can make optimizations because it knows the sliced buffer is of type AbstractByteBuf.
public class
public class
UnpooledUnsafeHeapByteBuf

Big endian Java heap buffer implementation.

pack-priv class
pack-priv class
UnreleasableByteBuf

A ByteBuf implementation that wraps another buffer to prevent a user from increasing or decreasing the wrapped buffer's reference count.

pack-priv class
UnsafeByteBufUtil

All operations get and set as ByteOrder#BIG_ENDIAN.

pack-priv class
UnsafeDirectSwappedByteBuf

Deprecated as a consequence of AbstractUnsafeSwappedByteBuf being deprecated.
Special SwappedByteBuf for ByteBufs that are backed by a memoryAddress.
pack-priv class
UnsafeHeapSwappedByteBuf

Deprecated as a consequence of AbstractUnsafeSwappedByteBuf being deprecated.
Special SwappedByteBuf for ByteBufs that use unsafe to access the byte array.
pack-priv class
WrappedByteBuf

Wraps another ByteBuf.

pack-priv class
pack-priv class