io.netty包下ByteBuf基本使用

代码:


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.util.Arrays;

public class ByteBufTest {
    public static void main(String[] args) {
        // 0.创建一个非池化的ByteBuf,大小为10个字节
        ByteBuf buf = Unpooled.buffer(10);
        System.out.println("原始ByteBuf为:" + buf.toString());
        System.out.println("0.ByteBuf中的内容为:" + Arrays.toString(buf.array()) + "\n");

        // 1.写入一段内容
        byte[] bytes = {1, 2, 3, 4, 5};
        buf.writeBytes(bytes);
        System.out.println("写入的bytes为:" + Arrays.toString(bytes));
        System.out.println("写入一段内容后ByteBuf为:" + buf.toString());
        System.out.println("1.ByteBuf中的内容为:" + Arrays.toString(buf.array()) + "\n");

        // 2. 读取一段内容
        byte b0 = buf.getByte(0);
        byte b4 = buf.getByte(4);
        System.out.println("get的bytes为:" + Arrays.toString(new byte[] {b0, b4}));
        byte[] header = new byte[3];
        buf.readBytes(header);
        System.out.println("header读取的bytes为:" + Arrays.toString(header));
        System.out.println("读取一段内容后ByteBuf为:" + buf.toString());

        byte b1 = buf.readByte();
        byte b2 = buf.readByte();
        System.out.println("读取的bytes为:" + Arrays.toString(new byte[] {b1, b2}));
        System.out.println("读取一段内容后ByteBuf为:" + buf.toString());
        System.out.println("2.ByteBuf中的内容为:" + Arrays.toString(buf.array()) + "\n");

        // 3.再次写入一段内容,比第一段内容少
        byte[] bytes2 = {6, 6};
        buf.writeBytes(bytes2);
        System.out.println("写入的bytes2为:" + Arrays.toString(bytes2));
        System.out.println("写入一段内容后ByteBuf为:" + buf.toString());
        System.out.println("3.ByteBuf中的内容为:" + Arrays.toString(buf.array()) + "\n");

        // 4.将读取的内容丢弃
        buf.discardReadBytes();
        System.out.println("将读取的内容丢弃后ByteBuf为:" + buf.toString());
        System.out.println("4.ByteBuf中的内容为:" + Arrays.toString(buf.array()) + "\n");

        // 5.清空读写指针
        buf.clear();
        System.out.println("清空读写指针后ByteBuf为:" + buf.toString());
        System.out.println("5.ByteBuf中的内容为:" + Arrays.toString(buf.array()) + "\n");

        // 6.再次写入一段内容,比第一段内容少
        byte[] bytes6 = {1, 2, 3};
        buf.writeBytes(bytes6);
        System.out.println("写入的bytes6为:" + Arrays.toString(bytes6));
        System.out.println("写入一段内容后ByteBuf为:" + buf.toString());
        System.out.println("6.ByteBuf中的内容为:" + Arrays.toString(buf.array()) + "\n");

        // 7.将ByteBuf清零
        buf.setZero(0, buf.capacity());
        System.out.println("清零后ByteBuf为:" + buf.toString());
        System.out.println("7.ByteBuf中的内容为:" + Arrays.toString(buf.array()) + "\n");

        // 8.再次写入一段超过容量的内容
        byte[] bytes3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        buf.writeBytes(bytes3);
        System.out.println("写入的bytes3为:" + Arrays.toString(bytes3));
        System.out.println("写入一段内容后ByteBuf为:" + buf.toString());
        System.out.println("8.ByteBuf中的内容为:" + Arrays.toString(buf.array()) + "\n");
    }
}

结果:
在这里插入图片描述
ByteBuf动态扩容
capacity默认值:256字节,最大值:Integer.MAX_VALUE (2G)

buf.writeBytes(bytes3);方法调用时,通过AbstractByteBuf.ensureWritable0()方法进行检查
最终新容量计算方法:AbstractByteBufAllocator.calculateNewCapacity

根据capacity的最小值要求,对应有两套计算方法:
没超过4兆:从64字节开始,每次递增一倍,直至计算出来的newCapacity满足新容量最小要求
举例:
1.如果当前大小256,已写250,继续写10字节的数据,需要的最小容量要求是261,则新容量为64x2x2x2=512
2.(测试代码中)当前大小10,已写3,继续写11字节的数据,需要的最小容量要求应该是14,但最小值为为64,所以新容量为64(最小值)

超过4兆:新容量=新容量最小要求/4兆x4兆+4兆
示例:当前大小为3兆,已写3兆,继续写2兆,需要的最小容量大小为5兆,则新容量是8兆(不能超过最大值)

4兆的来源:一个固定的阈值AbstractByteBufAllocator.CALCULATE_THRESHOLD
计算源码如下
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值