美文网首页
netty笔记-ByteBuf(1)概念和基本构建

netty笔记-ByteBuf(1)概念和基本构建

作者: 兴浩 | 来源:发表于2018-07-02 11:56 被阅读8次

1.ByteBuffer读写操作

    @Test
    public void test1()
    {
        ByteBuffer buf = ByteBuffer.allocate(4);
        buf.put((byte) 1);
        buf.put((byte) 2);

        buf.flip(); // 从写模式切换为读模式
        System.out.println(buf.get());  // 取出0x01
        System.out.println(buf.get());  // 取出0x02
    }

调用flip之前


调用flip之前

调用get之后

2.ByteBuf

参考:
ByteBuf总述

1.主要介绍了ByteBuffer和ByteBuf的区别

  1. ByteBuf内部的实现

3.堆缓冲区

    /**
     * Listing 5.1 Backing array
     */
    public static   void heapBuffer() {
        ByteBuf heapBuf = Unpooled.buffer(1024); //get reference form somewhere
        // 检查ByteBuf 是否有一个支撑数组
        if (heapBuf.hasArray()) {
            // 如果有,则获取对该数组的引用
            byte[] array = heapBuf.array();
            // 计算第一个字节的偏移量。
            int offset = heapBuf.arrayOffset() + heapBuf.readerIndex();
            // 获得可读字节数
            int length = heapBuf.readableBytes();
            // 使用数组、偏移量和长度作为参数调用你的方法
            handleArray(array, offset, length);
        }
    }

4.直接缓冲区

/**
 * Listing 5.2 Direct buffer data access
 */
public static void directBuffer() {
    ByteBuf directBuf = Unpooled.directBuffer(1024); //get reference form somewhere
    // 检查ByteBuf 是否由数组支撑。如果不是,则这是一个直接缓冲区
    if (!directBuf.hasArray()) {
        // 获取可读字节数
        int length = directBuf.readableBytes();
        // 分配一个新的数组来保存具有该长度的字节数据
        byte[] array = new byte[length];
        // 将字节复制到该数组
        directBuf.getBytes(directBuf.readerIndex(), array);
        // 使用数组、偏移量和长度作为参数调用你的方法
        handleArray(array, 0, length);
    }
}

5.CompositeByteBuf

5.1.ByteBuffer实现

    /**
     * Listing 5.3 Composite buffer pattern using ByteBuffer
     */
    public static void byteBufferComposite(ByteBuffer header, ByteBuffer body) {
        // Use an array to hold the message parts
        ByteBuffer[] message = new ByteBuffer[]{header, body};

        // Create a new ByteBuffer and use copy to merge the header and body
        ByteBuffer message2 = ByteBuffer.allocate(header.remaining() + body.remaining());
        message2.put(header);
        message2.put(body);
        message2.flip();
    }

    public static void byteBufferCompositeTest()
    {
        ByteBuffer header=ByteBuffer.allocate(1024);
        ByteBuffer body=ByteBuffer.allocate(1024);
        byteBufferComposite(header,body);
    }

5.2.CompositeByteBuf实现

    /**
     * Listing 5.4 Composite buffer pattern using CompositeByteBuf
     */
    public static void byteBufComposite() {
        CompositeByteBuf messageBuf = Unpooled.compositeBuffer();
        ByteBuf headerBuf = Unpooled.buffer(128); // can be backing or direct
        ByteBuf bodyBuf = Unpooled.buffer(1024);   // can be backing or direct
        // 将ByteBuf 实例追加到CompositeByteBuf
        messageBuf.addComponents(headerBuf, bodyBuf);
        //...
        // 删除位于索引位置为 0(第一个组件)的ByteBuf
        messageBuf.removeComponent(0); // remove the header
        // 循环遍历所有的ByteBuf 实例
        for (ByteBuf buf : messageBuf) {
            System.out.println(buf.toString());
        }
    }

相关文章

网友评论

      本文标题:netty笔记-ByteBuf(1)概念和基本构建

      本文链接:https://www.haomeiwen.com/subject/xmukuftx.html