【Netty4】netty ByteBuf (一)如何创建ByteBuf对象

作者:逅弈
链接:https://www.jianshu.com/p/8e407689c15a
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关文章:
ByteBuf是什么》
netty ByteBuf (一)如何创建ByteBuf对象
netty ByteBuf (二) 引用计数对象(reference counted objects)
netty ByteBuf(三)如何释放ByteBuf

有一点我们需要知道的是,ByteBuf的jar包,是可以单独使用的。比如某个项目中有一个场景,需要处理某个自定义的协议,那么我们在解析协议时,就可以将接收到的将字节内容写入一个ByteBuf,然后从ByteBuf中慢慢的将内容读取出来。下面让我们用一个例子简单的了解下ByteBuf的使用。

ByteBuf的创建

要想使用ByteBuf,首先肯定是要创建一个ByteBuf,更确切的说法就是要申请一块内存,后续可以在这块内存中执行写入数据读取数据等等一系列的操作。

那么如何创建一个ByteBuf呢?Netty中设计了一个专门负责分配ByteBuf的接口:ByteBufAllocator该接口有一个抽象子类和两个实现类,分别对应了用来分配池化的ByteBuf和非池化的ByteBuf。

具体的层级关系如下图所示:
在这里插入图片描述
有了Allocator之后,Netty又为我们提供了两个工具类:PooledUnpooled,分别用来分配池化的和未池化ByteBuf,进一步简化了创建ByteBuf的步骤,只需要调用这两个工具类的静态方法即可。

不同的创建方法

我们以Unpooled类为例,查看Unpooled的源码可以发现,他为我们提供了许多创建ByteBuf的方法,但最终都是以下这几种,只是参数不一样而已:

public final class Unpooled {

	// 在堆上分配一个ByteBuf,并指定初始容量和最大容量
	public static ByteBuf buffer(int initialCapacity, int maxCapacity) {
	    return ALLOC.heapBuffer(initialCapacity, maxCapacity);
	}
	// 在堆外分配一个ByteBuf,并指定初始容量和最大容量
	public static ByteBuf directBuffer(int initialCapacity, int maxCapacity) {
	    return ALLOC.directBuffer(initialCapacity, maxCapacity);
	}
	// 使用包装的方式,将一个byte[]包装成一个ByteBuf后返回
	public static ByteBuf wrappedBuffer(byte[] array) {
	    if (array.length == 0) {
	        return EMPTY_BUFFER;
	    }
	    return new UnpooledHeapByteBuf(ALLOC, array, array.length);
	}
	// 返回一个组合ByteBuf,并指定组合的个数
	public static CompositeByteBuf compositeBuffer(int maxNumComponents){
	    return new CompositeByteBuf(ALLOC, false, maxNumComponents);
	}

其中包装方法除了上述这个方法之外,还有一些其他常用的包装方法,比如参数是一个ByteBuf的包装方法,比如参数是一个原生的ByteBuffer的包装方法,比如指定一个内存地址和大小的包装方法等等。

另外还有一些copy*开头的方法,实际是调用了buffer(int initialCapacity, int maxCapacity)或directBuffer(int initialCapacity, int maxCapacity)方法,然后将具体的内容write进生成的ByteBuf中返回。

以上所有的这些方法都实际通过一个叫ALLOC的静态变量进行了调用,来实现具体的ByteBuf的创建,而这个ALLOC实际是一个ByteBufAllocator:

public final class Unpooled {
  private static final ByteBufAllocator ALLOC = UnpooledByteBufAllocator.DEFAULT;

涉及的设计模式

ByteBufByteBufAllocator之间是一种相辅相成的关系,ByteBufAllocator用来创建一个ByteBuf,而ByteBuf亦可以返回创建他的Allocator。ByteBuf和ByteBufAllocator之间是一种 抽象工厂模式,具体可以用一张图描述如下:
在这里插入图片描述
下面我来用一个实际的例子来说明ByteBuf的使用,并通过观察在不同阶段ByteBuf的读写指针的值和ByteBuf的容量变化来更加深入的了解ByteBuf的设计,为了方便,我会用非池化的分配器来创建ByteBuf

使用示例

private static void simpleUse(){
    // 1.创建一个非池化的ByteBuf,大小为10个字节
    ByteBuf buf = Unpooled.buffer(10);
    System.out.println("原始ByteBuf为====================>"+buf.toString());
    System.out.println("1.ByteBuf中的内容为===============>"+Arrays.toString(buf.array())+"\n");

    // 2.写入一段内容
    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("2.ByteBuf中的内容为===============>"+Arrays.toString(buf.array())+"\n");

    // 3.读取一段内容
    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("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[] bytes2 = {1,2,3};
    buf.writeBytes(bytes2);
    System.out.println("写入的bytes为====================>"+Arrays.toString(bytes2));
    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("写入的bytes为====================>"+Arrays.toString(bytes3));
    System.out.println("写入一段内容后ByteBuf为===========>"+buf.toString());
    System.out.println("8.ByteBuf中的内容为===============>"+Arrays.toString(buf.array())+"\n");
}

执行结果如下:

原始ByteBuf为====================>UnpooledByteBufAllocator...(ridx: 0, widx: 0, cap: 10)
1.ByteBuf中的内容为===============>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

写入的bytes为====================>[1, 2, 3, 4, 5]
写入一段内容后ByteBuf为===========>UnpooledByteBufAllocator...(ridx: 0, widx: 5, cap: 10)
2.ByteBuf中的内容为===============>[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]

读取的bytes为====================>[1, 2]
读取一段内容后ByteBuf为===========>UnpooledByteBufAllocator...(ridx: 2, widx: 5, cap: 10)
3.ByteBuf中的内容为===============>[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]

将读取的内容丢弃后ByteBuf为========>UnpooledByteBufAllocator...(ridx: 0, widx: 3, cap: 10)
4.ByteBuf中的内容为===============>[3, 4, 5, 4, 5, 0, 0, 0, 0, 0]

将读写指针清空后ByteBuf为==========>UnpooledByteBufAllocator...(ridx: 0, widx: 0, cap: 10)
5.ByteBuf中的内容为===============>[3, 4, 5, 4, 5, 0, 0, 0, 0, 0]

写入的bytes为====================>[1, 2, 3]
写入一段内容后ByteBuf为===========>UnpooledByteBufAllocator...(ridx: 0, widx: 3, cap: 10)
6.ByteBuf中的内容为===============>[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]

将内容清零后ByteBuf为==============>UnpooledByteBufAllocator...(ridx: 0, widx: 3, cap: 10)
7.ByteBuf中的内容为================>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

写入的bytes为====================>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
写入一段内容后ByteBuf为===========>UnpooledByteBufAllocator...(ridx: 0, widx: 14, cap: 64)
8.ByteBuf中的内容为===============>[0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

执行过程分析

下面让我们来仔细的研究下执行的过程,并分析下为什么会产生这样的执行结果。

1.初始化一个大小为10的ByteBuf
ByteBuf buf = Unpooled.buffer(10);
在这里插入图片描述
2.写入一段内容
buf.writeBytes(bytes);
在这里插入图片描述
当写入一段内容后(这里写入的是5个字节),写指针向后移动了5个字节,写指针的值变成了5,而读指针没有发生变化还是0,但是读指针和写指针之间的字节现在变成了“可读”的状态了,我们用紫色来表示。

3.读取一段内容
byte b1 = buf.readByte();
byte b2 = buf.readByte();
在这里插入图片描述
接着我们有读取了2个字节的内容,这时读指针向后移动了2个字节,读指针的值变成了2,写指针不变,此时0和读指针之间的内容变成了“可丢弃”的状态了,我们用粉色来表示

4.将读取的内容丢弃
buf.discardReadBytes();
在这里插入图片描述
紧接着,我们将刚刚读取完的2个字节丢弃掉,这时ByteBuf把读指针与写指针之间的内容(即 3、4、5 三个字节)移动到了0的位置,并且将读指针更新为0,写指针更新为原来写指针的值减去原来读指针的值。但是需要注意的是,第4和第5个字节的位置上,还保留的原本的内容,只是这两个字节由原来的“可读”变成了现在的“可写”。

此时wtiterIndex=3,指向第四位,表示从该位起,往后的格子都可以写入新值,等价于都是空的格子

5.将读写指针清空
buf.clear();

然后,我们执行了一个 clear方法,将读写指针同时都置为0了,此时所有的字节都变成“可写”了,但是需要注意的是,clear方法只是更改的读写指针的值,每个位置上原本的字节内容并没有发生改变
6.再次写入一段较少的内容
在这里插入图片描述
然后再次写入一段内容后,读指针不变,写指针向后移动了具体的字节数,这里是向后移动了三个字节。且写入的这三个字节变成了“可读”状态。

7.将ByteBuf中的内容清零
buf.setZero(0,buf.capacity());
在这里插入图片描述
清零(setZero)和清空(clear)的方法是两个概念完全不同的方法,“清零”是把指定位置上的字节的值设置为0,除此之外不改变任何的值,所以读写指针的值和字节的“可读写”状态与上次保持一致,而“清空”则只是将读写指针都置为0,并且所有字节都变成了“可写”状态。

8.写入一段超过容量的内容
在这里插入图片描述
最后我们往ByteBuf中写入超过ByteBuf容量的内容,这里是写入了11个字节,此时ByteBuf原本的容量不足以写入这些内容了,所以ByteBuf发生了扩容。其实只要写入的字节数超过可写字节数,就会发生扩容了。

扩容分析

那么扩容是怎么扩的呢,为什么容量从10扩容到64呢?我们从源码中找答案。

扩容肯定发生在写入字节的时候,让我们找到 writeBytes(byte[] bytes) 方法,netty 4.1.6.Final源码具体如下

public abstract class AbstractByteBuf extends ByteBuf {
	@Override
	public ByteBuf writeBytes(byte[] src) {
	    writeBytes(src, 0, src.length);
	    return this;
	}
	@Override
	public ByteBuf writeBytes(byte[] src, int srcIndex, int length) {
	    // 该方法检查是否有足够的可写空间,是否需要进行扩容
	    ensureWritable(length);
	    setBytes(writerIndex, src, srcIndex, length);
	    writerIndex += length;
	    return this;
	}

再进入 ensureWritable(length)方法内部查看,具体的代码如下:

public abstract class AbstractByteBuf extends ByteBuf {
	@Override
	    public ByteBuf ensureWritable(int minWritableBytes) {
	        if (minWritableBytes < 0) {
	            throw new IllegalArgumentException(String.format(
	                    "minWritableBytes: %d (expected: >= 0)", minWritableBytes));
	        }
	        ensureWritable0(minWritableBytes);
	        return this;
	    }
	
	    private void ensureWritable0(int minWritableBytes) {
	        if (minWritableBytes <= writableBytes()) {
	            return;
	        }
	
	        if (minWritableBytes > maxCapacity - writerIndex) {
	            throw new IndexOutOfBoundsException(String.format(
	                    "writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s",
	                    writerIndex, minWritableBytes, maxCapacity, this));
	        }
	
	        // Normalize the current capacity to the power of 2.
	        int newCapacity = alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity);
	
	        // Adjust to the new capacity.
	        capacity(newCapacity);
	    }

从上面的代码中可以很清楚的看出来,计算新的容量的方法是调用的 ByteBufAllocatorcalculateNewCapacity()方法,继续跟进去该方法,这里的 ByteBufAllocator的实现类是 AbstractByteBufAllocator ,具体的代码如下:

  @Override
    public int calculateNewCapacity(int minNewCapacity, int maxCapacity) {
        if (minNewCapacity < 0) {
            throw new IllegalArgumentException("minNewCapacity: " + minNewCapacity + " (expectd: 0+)");
        }
        if (minNewCapacity > maxCapacity) {
            throw new IllegalArgumentException(String.format(
                    "minNewCapacity: %d (expected: not greater than maxCapacity(%d)",
                    minNewCapacity, maxCapacity));
        }
        // 扩容的阈值,4兆字节大小
        final int threshold = 1048576 * 4; // 4 MiB page

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

        // 如果要扩容后新的容量大于扩容的阈值,那么扩容的方式改为用新的容量加上阈值, 否则将新容量改为双倍大小进行扩容
        if (minNewCapacity > threshold) {
            int newCapacity = minNewCapacity / threshold * threshold;
            if (newCapacity > maxCapacity - threshold) {
                newCapacity = maxCapacity;
            } else {
                newCapacity += threshold;
            }
            return newCapacity;
        }

         // 如果要扩容后新的容量小于4兆字节,则从64字节开始扩容,每次双倍扩容,直到小于指定的新容量位置
        int newCapacity = 64;
        while (newCapacity < minNewCapacity) {
            newCapacity <<= 1;
        }

        return Math.min(newCapacity, maxCapacity);
    }

到这里就很清楚了,每次扩容时,有一个阈值t(4MB),计划扩容的大小为c,扩容后的值为n。

扩容的规则可以用下面的逻辑表示:

  • 如果c<t,则n从阈值t(4MB)开始,以每次增加2倍的方式扩容,直到双倍后的大小小于c;
  • 如果c>t,则n=c/t*t+t

得出的结论

  • ByteBuf有读和写两个指针,用来标记“可读”、“可写”、“可丢弃”的字节
  • 调用write*方法写入数据后,写指针将会向后移动
  • 调用read*方法读取数据后,读指针将会向后移动
  • 写入数据或读取数据时会检查是否有足够多的空间可以写入和是否有数据可以读取
  • 写入数据之前,会进行容量检查,当剩余可写的容量小于需要写入的容量时,需要执行扩容操作
  • 扩容时有一个4MB的阈值,需要扩容的容量小于阈值或大于阈值所对应的扩容逻辑不同
  • clear等修改读写指针的方法,只会更改读写指针的值,并不会影响ByteBuf中已有的内容
  • setZero等修改字节值的方法,只会修改对应字节的值,不会影响读写指针的值以及字节的可读写状态
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值