| 接口 | 描述 |
|---|---|
| ByteBufAllocator |
实现负责分配缓冲区。
|
| ByteBufAllocatorMetric | |
| ByteBufAllocatorMetricProvider | |
| ByteBufHolder |
一个发送或接收的数据包。
|
| ByteBufProcessor | Deprecated
使用 ByteProcessor 。
|
| PoolArenaMetric |
公开竞技场的指标。
|
| PoolChunkListMetric |
指标列表的块。
|
| PoolChunkMetric |
大块的度量。
|
| PoolSubpageMetric |
子页面的度量标准。
|
| Class | 描述 |
|---|---|
| AbstractByteBuf |
缓冲区的骨架实现。
|
| AbstractByteBufAllocator |
骨架 ByteBufAllocator执行延伸。
|
| AbstractDerivedByteBuf | Deprecated
不使用。
|
| AbstractReferenceCountedByteBuf |
用于计算引用的 ByteBuf实现的抽象基类。
|
| ByteBuf |
一个零或更多字节(八位字节)的随机和顺序访问序列。
|
| ByteBufInputStream |
从 InputStream读取数据的InputStream 。
|
| ByteBufOutputStream |
一个 OutputStream这在将数据写入ByteBuf 。
|
| ByteBufUtil |
与处理 ByteBuf相关的实用程序方法的集合,例如生成十六进制转储和交换整数的字节顺序。
|
| CompositeByteBuf |
将多个缓冲区显示为单个合并缓冲区的虚拟缓冲区。
|
| DefaultByteBufHolder |
ByteBufHolder的默认实现将其数据保存在ByteBuf中 。
|
| DuplicatedByteBuf | Deprecated
不使用。
|
| EmptyByteBuf |
一个空的 ByteBuf其容量和最大容量都是0 。
|
| PooledByteBufAllocator | |
| PooledByteBufAllocatorMetric |
暴露指标为 PooledByteBufAllocator 。
|
| ReadOnlyByteBuf | Deprecated
不使用。
|
| SlicedByteBuf | Deprecated
不使用。
|
| SwappedByteBuf | Deprecated
使用Little Endian访问器,例如
|
| Unpooled |
通过分配新空间或包装或复制现有字节数组,字节缓冲区和字符串来创建新的 ByteBuf 。
|
| UnpooledByteBufAllocator |
简单的 ByteBufAllocator实现,不会聚集任何东西。
|
| UnpooledDirectByteBuf |
基于NIO
ByteBuffer的缓冲区。
|
| UnpooledHeapByteBuf |
Big endian Java堆缓冲区实现。
|
| UnpooledUnsafeDirectByteBuf |
基于NIO
ByteBuffer的缓冲区。
|
ByteBuffer来表示一个字节序列。
这种方法比使用ByteBuffer有显着的优势。
Netty的新缓冲区类型ByteBuf的设计从根本上解决了ByteBuffer的问题,并满足了网络应用程序开发人员的日常需求。
列出一些很酷的功能:
StringBuffer一样。 flip()方法。 ByteBuffer快。 ByteBuf具有丰富的操作集,可针对快速协议实施进行优化。
例如, ByteBuf提供了访问无符号值和字符串以及在缓冲区中搜索特定字节序列的各种操作。
您还可以扩展或包装现有的缓冲区类型以添加方便的访问器。
自定义缓冲区类型仍然实现ByteBuf接口,而不是引入不兼容的类型。
+--------+----------+
| header | body |
+--------+----------+
如果使用ByteBuffer则必须创建一个新的大缓冲区,并将这两个部分复制到新缓冲区中。
或者,您可以在NIO中执行收集写入操作,但它限制您将缓冲区组合表示为ByteBuffer的数组而不是单个缓冲区,从而打破抽象并引入复杂的状态管理。
此外,如果您不打算从NIO频道读取或写入,这是没有用的。
// The composite type is incompatible with the component type.
ByteBuffer[] message = new ByteBuffer[] { header, body };
相比之下, ByteBuf没有这样的警告,因为它是完全可扩展的并且具有内置的复合缓冲区类型。
// The composite type is compatible with the component type.
ByteBuf message = Unpooled.wrappedBuffer(header, body);
// Therefore, you can even create a composite by mixing a composite and an
// ordinary buffer.
ByteBuf messageWithFooter = Unpooled.wrappedBuffer(message, footer);
// Because the composite is still a 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);
String 。
您经常估计结果字符串的长度,并让StringBuffer扩展。
// A new dynamic buffer is created. Internally, the actual buffer is created
// lazily to avoid potentially wasted memory space.
ByteBuf b = 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');
ByteBuf最常用的缓冲区实现是一个非常薄的字节数组封装(即byte[] )。
与ByteBuffer不同,它没有复杂的边界检查和索引补偿,因此JVM更容易优化缓冲区访问。
更复杂的缓冲区实现仅用于分片缓冲区或复合缓冲区,它的性能和ByteBuffer 。
Copyright © 2008–2018 The Netty Project. All rights reserved.