/**
* ByteBuf是一个连续随机访问的bytes序列,是原始byte数组及ByteBuffer的顶层抽象。
*
* buffer建立:
* 推荐使用工具类Unpooled中的方法进行建立,也能够调用实现类的构造方法建立。
*
* 随机访问索引:
* 和一般的原始byte数组同样,ByteBuf使用基于0的索引,即第一个字节的索引为0,最后一个字节的索引为 capacity - 1,
*
* ByteBuf buffer = ...;
* for (int i = 0; i < buffer.capacity(); i ++) {
* byte b = buffer.getByte(i);
* System.out.println((char) b);
* }
*
* 连续访问索引:
* ByteBuf提供两个指针变量readerIndex、writerIndex分别用于读写操做,以下图表,两个变量将
* ByteBuf分割为三部分。
*
* +-------------------+------------------+------------------+
* | discardable bytes | readable bytes | writable bytes |
* | | (CONTENT) | |
* +-------------------+------------------+------------------+
* | | | |
* 0 <= readerIndex <= writerIndex <= capacity
*
* 可读取的字节(实际存储的内容):
* 这一部分是数据实际存储的位置,read和skip方法就是在这块数据上执行获取和跳过操做,同时read操
* 做会增长可读取字节数。若是read操做的参数是一个ByteBuf,而且未指定存储位置,则参数ByteBuf
* 的writeIndex 也会随着增加。
*
* 可读取的数据不足会抛出IndexOutOfBoundsException异常,新分配内存,包装或复制的buffer的
* readerIndex为0.
*
* 遍历buffer;
*
* ByteBuf buffer = ...;
* while (buffer.isReadable()) {
* System.out.println(buffer.readByte());
* }
*
* 可写入字节: *
* 这一部分是须要填充的未定义空间,write方法会从当前writeIndex位置写入数据并增长writeIndex值,
* 若是参数是ByteBuf且并未指定源索引,则参数ByteBuf的readerIndex也会随着增加。
*
* 若是当前可写空间不足,则抛出IndexOutOfBoundsException异常,新分配内存,包装或复制的buffer
* 的writerIndex为buffe的capacity容量。
* 随机填充
* ByteBuf buffer = ...;
* while (buffer.maxWritableBytes() >= 4) {
* buffer.writeInt(random.nextInt());
* }
*
* 可废弃字节:
* 这一部分为已经执行过读取操做的数据,初始空间为0,随着read操做的执行,最大增加至writeIndex值。
* 可废弃字节能够经过调用discardReadBytes()来丢弃,并从新标识为未使用。以下所示:
* discardReadBytes()执行以前:
*
* +-------------------+------------------+------------------+
* | discardable bytes | readable bytes | writable bytes |
* +-------------------+------------------+------------------+
* | | | |
* 0 <= readerIndex <= writerIndex <= capacity
*
*
* discardReadBytes()执行以后:
*
* +------------------+--------------------------------------+
* | readable bytes | writable bytes (got more space) |
* +------------------+--------------------------------------+
* | | |
* readerIndex (0) <= writerIndex (decreased) <= capacity
*
* 须要注意的是,一般状况下,discardReadBytes()操做没法保证buffer的可写数据。根据同的buffer
* 实现,大多数状况下,可写数据空间不会被移动,甚至会被彻底不一样的数据填充。
*
* 清除索引:
* clear()方法能够将buffer的readerIndex和writeIndex都置为0,可是并不清除buffer的数据,
* 另外须要注意是区分和ByteBuffer#clear()方法的不一样。
* <h4>Clearing the buffer indexes</h4>
*
* <pre>
* clear()执行以前:
*
* +-------------------+------------------+------------------+
* | discardable bytes | readable bytes | writable bytes |
* +-------------------+------------------+------------------+
* | | | |
* 0 <= readerIndex <= writerIndex <= capacity
*
*
* clear()执行以后:
*
* +---------------------------------------------------------+
* | writable bytes (got more space) |
* +---------------------------------------------------------+
* | |
* 0 = readerIndex = writerIndex <= capacity
*
* 查询操做:
* 单字节查询可使用 indexOf(int, int, byte) 方法,bytesBefore(int, int, byte)
* 方法对于处理null结尾的字符串会特别有用。
* 复杂查询可使用 forEachByte(int, int, ByteProcessor) 方法(ByteProcessor 提供遍历字节集合机制, 方法中使用ByteProcessor的特定实现类)
*
* 标记和重置:
* ByteBuf中存在两种标记变量,分别用于存储readerIndex和writerIndex,能够经过调用reset()
* 方法来重置任意其一。除了没有readlimit变量,ByteBuf和InputStream具备相同的标记和和重置操做机制,
*
* 衍生buffer:
* 经过调用ByteBuf的如下方法能够建立一个已知buffer的镜像视图。
*
* #duplicate()
* #slice()
* #slice(int, int)
* #readSlice(int)
* #retainedDuplicate()
* #retainedSlice()
* #retainedSlice(int, int)
* #readRetainedSlice(int)
*
* buffer视图虽然和源buffer共享同一起内存,可是拥有彻底独立的readerIndex,writeIndex和标记索引。
* 这和NIO buffer机制很是类似。
*
* 若是要获取一份全新的buffer备份,能够执行buffer的copy()方法。
*
* <h4>Non-retained and retained derived buffers</h4>
*
* duplicate()、slice(int, int)、readSlice(int)并未在新生成的衍生buffer执行retain()操做,所以
* 衍生buffer的引用计数没有增加。retainedDuplicate()、retainedSlice()、readRetainedSlice()
* 方法能够生成一个包含引用计数增加而且产生较少垃圾()的衍生buffer。
*
* JDK byte array转换
*
* Byte array
*
* 由byte数组(byte[])包装生成的ByteBuf,能够直接经过数据中的方法进行操做。
* 判断方法:hasArray()。
*
* NIO Buffers
* 若是一个ByteBuf能够被转换为NIO ByteBuffer,那么就能够经过nioBuffer的方法来操做ByteBuf。
* 判断方法:nioBufferCount()。
*
* 字符串输出:
* ByteBuf存在多种toString()方法,须要注意的是toString()方法并非一个转换方法。
*
* I/O Streams
*
* 详见 缓冲输入输出流 ByteBufInputStream ByteBufOutputStream.
*/