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.netty5.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.netty5.buffer.ByteBuf has rich set of operations
optimized for rapid protocol implementation. For example,
io.netty5.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.netty5.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.netty5.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 =netty5. buffer. ByteBuf io..wrappedBuffer(header, body); // Therefore, you can even create a composite by mixing a composite and an // ordinary buffer.netty5. buffer. Unpooled io.messageWithFooter =netty5. buffer. ByteBuf io..wrappedBuffer(msg, footer); // Because the composite is still anetty5. 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);netty5. 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 =netty5. 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');netty5. buffer. Unpooled
io.netty5.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 | AllocationType
An object used by buffer allocators to communicate desirable properties of an allocation to a memory manager, such as whether an allocation should be off-heap. |
| public interface | AllocatorControl
Methods for accessing and controlling the internals of an allocator. |
| public interface | Buffer
A life cycled buffer of memory, with separate reader and writer offsets. |
| public interface | BufferAccessor
This interface is just the primitive data accessor methods that |
| public interface | BufferAllocator
Interface for allocating |
| public interface | BufferComponent
A view onto the buffer component being processed in a given iteration of |
| public interface | ByteCursor
The ByteCursor scans through a sequence of bytes. |
| public interface | ComponentIterator<
A type that implements T extends ComponentIterator.Next and BufferComponent.A facade for iterating the readable or writable components of a |
| public interface | CompositeBuffer
The |
| public interface | |
| public interface | LeakInfo
Information about a resource leak that happened. |
| public interface | MemoryManager
The choice of |
| public interface |
| Modifier and Type | Class and Description |
|---|---|
| public class | BufferClosedException
An exception thrown when an operation is attempted on a |
| public abstract class | BufferHolder<
The concrete T extends ResourceBufferHolder type.The |
| public class | BufferInputStream
An |
| public class | BufferOutputStream
An |
| public class | BufferReadOnlyException
An exception thrown when an operation is attempted on a read-only |
| public class | BufferRef
A mutable reference to a buffer. |
| public class | BufferStub
A stub of a |
| public class | BufferUtil
A collection of utility methods that is related with handling |
| public class | DefaultBufferAllocators
Accessor for the default, shared |
| pack-priv class | DefaultCompositeBuffer
The default implementation of the |
| public class | LoggingLeakCallback
The |
| pack-priv class | |
| public class | SensitiveBufferAllocator
This |
| Modifier and Type | Enum and Description |
|---|---|
| public enum | StandardAllocationTypes
Standard implementations of |