Netty的ByteBuf和JDK的ByteBuffer的区别?

1.Buffer

1.1 重要属性
  • capacity:buffer中包含元素的个数。其值一旦确认后不可更改。
  • limit:第一个不可被读元素的索引值。
  • position:下一个要被读或者写元素的索引值。

三个属性之间的关系:0 <= mark <= position <= limit <= capacity 。

1.2 重要方法

clear
用来让一个buffer的属性回到特定值,相当于达到了清空buffer中元素的效果。

    public final Buffer clear() {
        position = 0;
        limit = capacity;
        mark = -1;
        return this;
    }

flip
在读写进行切换操作的时候使用。

    public final Buffer flip() {
        limit = position;
        position = 0;
        mark = -1;
        return this;
    }

rewind
用来对buffer进行重读。

    public final Buffer rewind() {
        position = 0;
        mark = -1;
        return this;
    }

2.ByteBuffer

ByteBuffer是Buffer的一个实现。

该类定义了6种byte buffers的操作方法:

  • 用来读取/写入的绝对/相对方法
  • 将缓冲区中的字节序列输出到数组中
  • 将数组或者其他的buffer中的字节序列输出到另一个buffer中
  • 与字符有关的操作
  • 创建buffer的方法
  • 压缩、复制、划分buffer

3.ByteBuf

ByteBuf的划分
ByteBuf划分

3.1 discardReadBytes方法

当buffer中可写空间不足时,可以调用该方法,通过将readerIndex设置为0,并将readable bytes区间的内容移到buffer的开头,并将writeIndex设置为writeIndex-readerIndex,实现了清空已读部分内容的作用。

    @Override
    public ByteBuf discardReadBytes() {
        ensureAccessible();
        if (readerIndex == 0) {
            return this;
        }

        if (readerIndex != writerIndex) {
            // 移动剩余未读部分
            setBytes(0, this, readerIndex, writerIndex - readerIndex);
            // 重现调整Index值
            writerIndex -= readerIndex;
            adjustMarkers(readerIndex);
            readerIndex = 0;
        } else {
            adjustMarkers(readerIndex);
            writerIndex = readerIndex = 0;
        }
        return this;
    }
3.2 calculateNewCapacity方法

该方法用来重新计算buffer的capacity的值。

    @Override
    public int calculateNewCapacity(int minNewCapacity, int maxCapacity) {
        checkPositiveOrZero(minNewCapacity, "minNewCapacity");
      //当已分配的空间大于最大可分配空间时直接抛出异常
        if (minNewCapacity > maxCapacity) {
            throw new IllegalArgumentException(String.format(
                    "minNewCapacity: %d (expected: not greater than maxCapacity(%d)",
                    minNewCapacity, maxCapacity));
        }
        final int threshold = CALCULATE_THRESHOLD; // 1048576 * 4

    
        if (minNewCapacity == threshold) {
            return threshold;
        }

        // If over threshold, do not double but just increase by threshold.
        if (minNewCapacity > threshold) {
            int newCapacity = minNewCapacity / threshold * threshold;
            if (newCapacity > maxCapacity - threshold) {
                newCapacity = maxCapacity;
            } else {
                newCapacity += threshold;
            }
            return newCapacity;
        }

        // Not over threshold. Double up to 4 MiB, starting from 64.
        int newCapacity = 64;
        while (newCapacity < minNewCapacity) {
            newCapacity <<= 1;
        }

        return Math.min(newCapacity, maxCapacity);
    }

扩容分为两种:
1.当capacity<=threshold时,通过2的指数获取到新的capacity;
2.当capacity>threshold时,capacity每次加上一个threshold的值,知道到达maxCapacity。

4.最主要区别?

  • ByteBuffer通过position和limit来控制读取和写入,每次切换读写时都需要调用flip方法,而ByteBuf通过writerIndex和readerIndex来简化控制。
  • ByteBuffer中的capacity是一个固定值,ByteBuf可以调用calculateNewCapacity方法来重新计算capacity值。

以上都是我个人看完doc文档的想法,如有不足之处,请各位大佬指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值