欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

BAT面试必问细节:关于Netty中的ByteBuf详解

最编程 2024-01-15 07:06:50
...

在Netty中,还有另外一个比较常见的对象ByteBuf,它其实等同于Java Nio中的ByteBuffer,但是ByteBuf对Nio中的ByteBuffer的功能做了很作增强,下面我们来简单了解一下ByteBuf。

下面这段代码演示了ByteBuf的创建以及内容的打印,这里显示出了和普通ByteBuffer最大的区别之一,就是ByteBuf可以自动扩容,默认长度是256,如果内容长度超过阈值时,会自动触发扩容

public class ByteBufExample {

public static void main(String[] args) {
ByteBuf buf= ByteBufAllocator.DEFAULT.buffer();//可自动扩容
log(buf);
StringBuilder sb=new StringBuilder();
for (int i = 0; i < 32; i++) { //演示的时候,可以把循环的值扩大,就能看到扩容效果
sb.append(" - "+i);
}
buf.writeBytes(sb.toString().getBytes());
log(buf);
}

private static void log(ByteBuf buf){
StringBuilder builder=new StringBuilder()
.append(" read index:").append(buf.readerIndex()) //获取读索引
.append(" write index:").append(buf.writerIndex()) //获取写索引
.append(" capacity:").append(buf.capacity()) //获取容量
.append(StringUtil.NEWLINE);
//把ByteBuf中的内容,dump到StringBuilder中
ByteBufUtil.appendPrettyHexDump(builder,buf);
System.out.println(builder.toString());
}
}

ByteBuf创建的方法有两种

  • 第一种,创建基于堆内存的ByteBuf
    ByteBuf buffer=ByteBufAllocator.DEFAULT.heapBuffer(10);
  • 第二种,创建基于直接内存(堆外内存)的ByteBuf(默认情况下用的是这种

Java中的内存分为两个部分,一部分是不需要jvm管理的直接内存,也被称为堆外内存。堆外内存就是把内存对象分配在JVM堆以外的内存区域,这部分内存不是虚拟机管理,而是由操作系统来管理,这样可以减少垃圾回收对应用程序的影响


  • ByteBufAllocator.DEFAULT.directBuffer(10);
    直接内存的好处是读写性能会高一些,如果数据存放在堆中,此时需要把Java堆空间的数据发送到远程服务器,首先需要把堆内部的数据拷贝到直接内存(堆外内存),然后再发送。如果是把数据直接存储到堆外内存中,发送的时候就少了一个复制步骤。
    但是它也有缺点,由于缺少了JVM的内存管理,所以需要我们自己来维护堆外内存,防止内存溢出。

另外,需要注意的是,ByteBuf默认采用了池化技术来创建。关于池化技术在前面的课程中已经重复讲过,它的核心思想是实现对象的复用,从而减少对象频繁创建销毁带来的性能开销。

池化功能是否开启,可以通过下面的环境变量来控制,其中unpooled表示不开启。

-Dio.netty.allocator.type={unpooled|pooled}

public class NettyByteBufExample {
public static void main(String[] args) {
ByteBuf buf= ByteBufAllocator.DEFAULT.buffer();
System.out.println(buf);
}
}

ByteBuf的存储结构

ByteBuf的存储结构如图3-1所示,从这个图中可以看到ByteBuf其实是一个字节容器,该容器中包含三个部分

  • 已经丢弃的字节,这部分数据是无效的
  • 可读字节,这部分数据是ByteBuf的主体数据,从ByteBuf里面读取的数据都来自这部分;可写字节,所有写到ByteBuf的数据都会存储到这一段
  • 可扩容字节,表示ByteBuf最多还能扩容多少容量。

BAT面试必问细节:关于Netty中的ByteBuf详解_面试

图3-1


在ByteBuf中,有两个指针

  • readerIndex:读指针,每读取一个字节,readerIndex自增加1。ByteBuf里面总共有witeIndex-readerIndex个字节可读,当readerIndex和writeIndex相等的时候,ByteBuf不可读
  • writeIndex:写指针,每写入一个字节,writeIndex自增加1,直到增加到capacity后,可以触发扩容后继续写入。
  • ByteBuf中还有一个maxCapacity最大容量,默认的值是Integer.MAX_VALUE,当ByteBuf写入数据时,如果容量不足时,会触发扩容,直到capacity扩容到maxCapacity。
ByteBuf中常用的方法

对于ByteBuf来说,常见的方法就是写入和读取

Write相关方法

对于write方法来说,ByteBuf提供了针对各种不同数据类型的写入,比如

  • writeChar,写入char类型
  • writeInt,写入int类型
  • writeFloat,写入float类型
  • writeBytes, 写入nio的ByteBuffer
  • writeCharSequence, 写入字符串
public class ByteBufExample {

public static void main(String[] args) {
ByteBuf buf= ByteBufAllocator.DEFAULT.heapBuffer();//可自动扩容
buf.writeBytes(new byte[]{1,2,3,4}); //写入四个字节
log(buf);
buf.writeInt(5); //写入一个int类型,也是4个字节
log(buf);
}
private static void log(ByteBuf buf){
System.out.println(buf);
StringBuilder builder=new StringBuilder()
.append(" read index:").append(buf.readerIndex())
.append(" write index:").append(buf.writerIndex())
.append(" capacity:").append(buf.capacity())
.append(StringUtil.NEWLINE);
//把ByteBuf中的内容,dump到StringBuilder中
ByteBufUtil.appendPrettyHexDump(builder,buf);
System.out.println(builder.toString());
}
}

扩容

当向ByteBuf写入数据时,发现容量不足时,会触发扩容,而具体的扩容规则是

假设ByteBuf初始容量是10。

  • 如果写入后数据大小未超过512个字节,则选择下一个16的整数倍进行库容。比如写入数据后大小为12,则扩容后的capacity是16。
  • 如果写入后数据大小超过512个字节,则选择下一个2^n^。比如写入后大小是512字节,则扩容后的capacity是2^10^=1024 。(因为2^9^=512,长度已经不够了)
  • 扩容不能超过max capacity,否则会报错。
Reader相关方法

reader方法也同样针对不同数据类型提供了不同的操作方法,

  • readByte ,读取单个字节
  • readInt , 读取一个int类型
  • readFloat ,读取一个float类型
public class ByteBufExample {

public static void main(String[] args) {
ByteBuf buf= ByteBufAllocator.DEFAULT.heapBuffer();//可自动扩容
buf.writeBytes(new byte[]{1,2,3,4});
log(buf);
System.out.println(buf.readByte());
log(buf);
}
private static void log(ByteBuf buf){
StringBuilder builder=new StringBuilder()
.append(" read index:").append(buf.readerIndex())
.append(" write index:").append(buf.writerIndex())
.append(" capacity:").append(buf.capacity())
.append(StringUtil.NEWLINE);
//把ByteBuf中的内容,dump到StringBuilder中
ByteBufUtil.appendPrettyHexDump(builder,buf);
System.out.println(builder.toString());
}
}

从下面结果中可以看到,读完一个字节后,这个字节就变成了废弃部分,再次读取的时候只能读取 未读取的部分数据。

read index:0 write index:7 capacity:256
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 01 02 03 04 05 06 07 |....... |
+--------+-------------------------------------------------+----------------+
1
read index:1 write index:

上一篇: 部标JT1078协议RTP包解析

下一篇: 深度解析Netty中的ByteBuf缓冲区(二)