Skip to content

Latest commit

 

History

History
126 lines (79 loc) · 3.43 KB

19.3.CompositeBuffer.md

File metadata and controls

126 lines (79 loc) · 3.43 KB
title date categories tags description
19.3.CompositeBuffer
2020-03-31
Netty
Netty
网络
零拷贝

Composite Buffer

Netty ByteBuf提供的三种缓冲区类型

  1. HeapBuffer
  2. DirectBuffer
  3. Composite Buffer

CompositeBuffer, 复合缓冲区, 相当于一个 容器, 里面可以装不同类型的 Buffer。(Java未提供的)

自定义协议中,消息头,消息体。 可以把两个Buffer 放到一个CompositeBuffer里。

/**
 * A virtual buffer which shows multiple buffers as a single merged buffer.  It is recommended to use
 * {@link ByteBufAllocator#compositeBuffer()} or {@link Unpooled#wrappedBuffer(ByteBuf...)} instead of calling the
 * constructor explicitly.
 一个虚拟的缓冲区,它可以用于将多个缓冲区合并成一个缓冲区。
 推荐使用  ByteBufAllocator#compositeBuffer()} or {@link Unpooled#wrappedBuffer(ByteBuf...) 来创建, 不要直接显示调用它的构造方法
 */
public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements Iterable<ByteBuf> {
/**
 * Returns a new big-endian composite buffer with no components.
 返回一个大端的Composite Buffer。里面没有组件(一个组件就是一个Buffer)
 */
public static CompositeByteBuf compositeBuffer() {
    return compositeBuffer(AbstractByteBufAllocator.DEFAULT_MAX_COMPONENTS);
}
public class CompositeBufferTest {
    public static void main(String[] args) {
        CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer(); // CompositeByteBuf类型

        // 创建一个堆缓冲区
        ByteBuf heapBuffer = Unpooled.buffer();// 类型:UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 0, cap: 256)
        heapBuffer.writeByte('a');

        // 创建一个直接缓冲
        ByteBuf directBuffer = Unpooled.directBuffer();// UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeNoCleanerDirectByteBuf(ridx: 0, widx: 0, cap: 256)
        directBuffer.writeByte('b');

        System.out.println(heapBuffer);
        System.out.println(directBuffer);

        compositeByteBuf.addComponent(heapBuffer);
        compositeByteBuf.addComponent(directBuffer);


        //compositeByteBuf.removeComponent(0);

        // 遍历
        Iterator<ByteBuf> iterator = compositeByteBuf.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
            // UnpooledSlicedByteBuf(ridx: 0, widx: 0, cap: 0/0, unwrapped: UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 0, cap: 256))
            // UnpooledSlicedByteBuf(ridx: 0, widx: 0, cap: 0/0, unwrapped: UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeNoCleanerDirectByteBuf(ridx: 0, widx: 0, cap: 256))
        }


        // 报错,Exception in thread "main" java.lang.UnsupportedOperationException  原因:
        byte[] array = compositeByteBuf.array();

        /*
         * @Override
         *         public byte[] array() {
         *             switch (componentCount) {
         *                 case 0:
         *                     return EmptyArrays.EMPTY_BYTES;
         *                 case 1:
         *                     return components[0].buf.array();
         *                 default:
         *                     throw new UnsupportedOperationException();
         *             }
         *         }
         */

    }
}