美文网首页
Netty的ByteBuf和JDK的ByteBuffer的区别?

Netty的ByteBuf和JDK的ByteBuffer的区别?

作者: 蜡笔没了小新_e8c0 | 来源:发表于2019-12-05 15:59 被阅读0次

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文档的想法,如有不足之处,请各位大佬指出。

相关文章

  • Netty(二) ByteBuf

    Netty ByteBuf 是NIO中ByteBuffer的封装,相比JDK ByteBuffer更加易用; 为读...

  • Netty 之 ByteBuf 分析

    概述 Netty 中的 ByteBuf 和 NIO 中的 ByteBuffer 的区别。 1、Netty 中的 B...

  • Netty的ByteBuf和JDK的ByteBuffer的区别?

    1.Buffer 1.1 重要属性 capacity:buffer中包含元素的个数。其值一旦确认后不可更改。 li...

  • Netty ByteBuf源码分析

    Netty的ByteBuf是JDK中ByteBuffer的升级版,提供了NIO buffer和byte数组的抽象视...

  • 第 5 章 ByteBuf

    Netty 的 ByteBuffer 替代品是 ByteBuf ,既解决了 JDK API 的局限性,又为网络应用...

  • ByteBuf

    jdk自带ByteBuffer,看名字就知道作用差不多,区别是ByteBuffer读写都用一个指针,ByteBuf...

  • Netty中的ByteBuf

    由于JDK中提供的ByteBuffer无法动态扩容,并且API使用复杂等原因,Netty中提供了ByteBuf。B...

  • Netty学习--ByteBuf

    Netty使用ByteBuf替换Java NIO提供的ByteBuffer(使用过于复杂). ByteBuf 的A...

  • Netty in Action — Chapter 5

    第五章 ByteBuf ByteBuf是Netty的数据容器 Netty对ByteBuffer提供了一个可选方案B...

  • netty之ByteBuf

    ByteBuf是netty用于替代nio的ByteBuffer,存储字节的数据容器,相比于ByteBuffer,B...

网友评论

      本文标题:Netty的ByteBuf和JDK的ByteBuffer的区别?

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