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:
java.lang.StringBuffer .flip() method anymore.java.nio.ByteBuffer .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.
+--------+----------+ | 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.ByteBuffer s 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.message =netty. buffer. ByteBuf io..wrappedBuffer(header, body); // Therefore, you can even create a composite by mixing a composite and an // ordinary buffer.netty. buffer. Unpooled io.messageWithFooter =netty. buffer. ByteBuf io..wrappedBuffer(message, footer); // Because the composite is still anetty. buffer. Unpooled io., 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);netty. buffer. ByteBuf
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.b =netty. buffer. ByteBuf io..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');netty. buffer. Unpooled
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 .
| Modifier and Type | Interface 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 |
| public interface | ByteBufHolder
A packet which is send or receive. |
| public interface | ByteBufProcessor
Deprecated
Use
ByteProcessor.
|
| 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. |
| Modifier and Type | Class and Description |
|---|---|
| public abstract class | AbstractByteBuf
A skeletal implementation of a buffer. |
| public abstract class | AbstractByteBufAllocator
Skeletal |
| 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 |
| public abstract class | AbstractReferenceCountedByteBuf
Abstract base class for |
| pack-priv abstract class | AbstractUnpooledSlicedByteBuf
Deprecated
as a consequence of
AbstractDerivedByteBuf being deprecated.
|
| pack-priv abstract class | AbstractUnsafeSwappedByteBuf
Deprecated
as a consequence of Special SwappedByteBuf being deprecated.
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 |
| public class | ByteBufOutputStream
An |
| public class | ByteBufUtil
A collection of utility methods that is related with handling |
| public class | CompositeByteBuf
A virtual buffer which shows multiple buffers as a single merged buffer. |
| public class | DefaultByteBufHolder
Default implementation of a |
| 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 |
| pack-priv class | FixedCompositeByteBuf
|
| pack-priv class | HeapByteBufUtil
Utility class for heap buffers. |
| pack-priv class | IntPriorityQueue
Internal primitive priority queue, used by |
| pack-priv class | LongLongHashMap
Internal primitive map implementation that is specifically optimised for the runs availability map use case in |
| 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 |
| pack-priv class | |
| pack-priv abstract class | |
| public class | |
| public class | PooledByteBufAllocatorMetric
Exposed metric for |
| 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 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. Wrapper which swap the getShortLE, getIntLE
instead.
ByteOrder of a ByteBuf.
|
| public class | |
| public class | UnpooledByteBufAllocator
Simplistic |
| public class | UnpooledDirectByteBuf
A NIO |
| 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 A special AbstractUnpooledSlicedByteBuf being deprecated.
AbstractUnpooledSlicedByteBuf that can make optimizations because it knows the sliced buffer is of
type AbstractByteBuf.
|
| public class | UnpooledUnsafeDirectByteBuf
A NIO |
| public class | UnpooledUnsafeHeapByteBuf
Big endian Java heap buffer implementation. |
| pack-priv class | |
| pack-priv class | UnreleasableByteBuf
A |
| pack-priv class | UnsafeByteBufUtil
All operations get and set as |
| pack-priv class | UnsafeDirectSwappedByteBuf
Deprecated
as a consequence of Special AbstractUnsafeSwappedByteBuf being deprecated.
SwappedByteBuf for ByteBufs that are backed by a memoryAddress.
|
| pack-priv class | UnsafeHeapSwappedByteBuf
Deprecated
as a consequence of Special AbstractUnsafeSwappedByteBuf being deprecated.
SwappedByteBuf for ByteBufs that use unsafe to access the byte array.
|
| pack-priv class | WrappedByteBuf
Wraps another |
| pack-priv class | |
| pack-priv class |