走进Netty之 ByteBuf 类源码分析

本文主要是对Netty 中 ByteBuf 类做一些分析:

ByteBuf 发展与 JDK NIO 中的ByteBuffer, 还有其他几种关于基本类型的Buffer实现,前面我们讨论过ByteBuffer 的实现,因此引出ByteBuf 对于其的补充实现。
主要分为 direct bytebuffer 和 heap byteBuffer


ByteBuf 继承关系
  1. 拥有了WriterIndex 和 ReaderIndex, 使用起来比较ByteBuffer 里面的操作更加便利。
 *      +-------------------+------------------+------------------+
 *      | discardable bytes |  readable bytes  |  writable bytes  |
 *      |                   |     (CONTENT)    |                  |
 *      +-------------------+------------------+------------------+
 *      |                   |                  |                  |
 *      0      <=      readerIndex   <=   writerIndex    <=    capacity

图中disardable bytes 表示已被写以及已被读
readable bytes 表示可以被读区域
writable bytes 表示可以被写区域

  1. 缓存区扩展
        // Normalize the current capacity to the power of 2.
int newCapacity = alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity);

  1. 读操作:
    read*() : readerIndex + 相应数据的所占Byte 个数。

4.写操作:
write*() : writerIndex + 相应数据的所占Byte 个数。

  1. clear 操作:
    会将writerIndex 和 Readerindex 设置为原始值。

  2. 查找操作:

indexof() , byteBefore 等等。

UnpooledByteBuffer/pooledByteBuffer:
pooledByteBuffer: 内存池的实现,关系到 chunk 和page 的概念

PoolArena : 内存池实现类,由多个Chunk 组成的大块内存区域,而每个Chunk 则由一个或者多个Page 组成。

poolchunk: 节点标记分配情况

poolSubpage : long数组表示占用情况。 page 被切分为很多歌存储块,每一个page分配大小为第一次申请的内存块决定。

你可能感兴趣的:(走进Netty之 ByteBuf 类源码分析)