Netty之认识ByteBuf

╰半夏微凉° 2023-06-30 02:45 115阅读 0赞

目录

ByteBuf的结构

常见API

read

write

get、set

mark、reset

ByteBuf的分类

继承体系图

AbstractByteBuf

Pooled与Unpooled

Unsafe与非Unsafe

Heap与Direct


ByteBuf是Netty底层直接与数据打交道的一层抽象。ByteBuf作为数据流的载体,提供了一些列简单易用的API来操作底层数据流。

ByteBuf的结构

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTEyMTIzOTQ_size_16_color_FFFFFF_t_70

discardable bytes:0 <= readerIndex(无效部分)

readable bytes:readerIndex <= writerIndex(可读部分)

writable bytes:writerIndex <= capacity(可写部分或空闲部分)


常见API

read

常见的read相关API有readByte、readBytes、readInt、readLong、readChar等,读取的过程是从readerIndex开始向前读取类型对应的字节长度,读取完成后readerIndex也会向前移动相应的长度。

write

常见的write相关API有writeByte、writeBytes、writeInt、writeLong、writeChar等,写的过程与读过程相似,从writerIndex开始向前写入数据,对应的writerIndex也会移动到写完的位置。

get、set

与read、write相对应的还有get、set相关的API,这两种API在读数据或写数据时,都需要提供index参数,并且不会移动readerIndex或writerIndex。

mark、reset

我们知道read与write相关的API在执行完成后会移动index指针,而通过mark和reset可以实现index位置还原,例如markReaderIndex与resetReaderIndex可以还原readerIndex,markWriterIndex与resetWriterIndex可以还原writerIndex。


ByteBuf的分类

继承体系图

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTEyMTIzOTQ_size_16_color_FFFFFF_t_70 1


AbstractByteBuf

AbstractByteBuf是ByteBuf的骨架实现,里面定义了各种通用变量与API实现,而以下划线开头的抽象类则交给具体子类实现。

  1. public abstract class AbstractByteBuf extends ByteBuf {
  2. ......
  3. // 保存各种指针的值
  4. int readerIndex;
  5. int writerIndex;
  6. private int markedReaderIndex;
  7. private int markedWriterIndex;
  8. private int maxCapacity;
  9. // 各种API的实现
  10. @Override
  11. public byte readByte() {
  12. checkReadableBytes0(1);
  13. int i = readerIndex;
  14. // 下划线开头的方法都是抽象方法,由具体子类实现
  15. byte b = _getByte(i);
  16. // 位置指针移动
  17. readerIndex = i + 1;
  18. return b;
  19. }
  20. @Override
  21. public byte getByte(int index) {
  22. checkIndex(index);
  23. return _getByte(index);
  24. }
  25. protected abstract byte _getByte(int index);
  26. @Override
  27. public ByteBuf writeByte(int value) {
  28. ensureAccessible();
  29. ensureWritable0(1);
  30. _setByte(writerIndex++, value);
  31. return this;
  32. }
  33. @Override
  34. public ByteBuf setByte(int index, int value) {
  35. checkIndex(index);
  36. _setByte(index, value);
  37. return this;
  38. }
  39. protected abstract void _setByte(int index, int value);
  40. ......
  41. }

Pooled与Unpooled

Pooled(池化)方式每次分配内存时都会从系统预先分配好的一段内存中来取。

Unpooled(非池化)方式每次分配内存时都会调用系统API向操作系统申请内存创建ByteBuf。


Unsafe与非Unsafe

Unsafe会先计算数据的内存地址+偏移量,通过unsafe对象的native API来操作数据。

非Unsafe不依赖JDK的unsafe对象,它是通过数组+下标方式来获取数据,或者是通过JDK 底层的ByteBuffer API进行读写,一般而言unsafe方式效率更高一些。


Heap与Direct

Heap代表堆上内存分配,会被GC管理。

Direct代表堆外内存分配,调用JDK底层API进行分配系统内存,效率更高,但不受GC控制,需要手动释放内存。

发表评论

表情:
评论列表 (有 0 条评论,115人围观)

还没有评论,来说两句吧...

相关阅读

    相关 NettyByteBuf零拷贝

    我们或多或少了解过一些零拷贝的概念,而零拷贝也正是netty能够实现高性能的原因之一,因此我们有必要深入了解netty的零拷贝。但是在深入了解netty的零拷贝之前,让我们先来

    相关 NettyByteBuf

    什么是ByteBuf ByteBuf是Netty中非常重要的一个组件,他就像物流公司的运输工具:卡车,火车,甚至是飞机。而物流公司靠什么盈利,就是靠运输货物,可想而知By

    相关 NettyByteBuf

        网络数据的基本单位总是字节。Java  NIO提供了ByteBuffer作为它的字节容器,但是这个类使用起来过于复杂,而且也有些繁琐。Netty的ByteBuffer替