例如读了 4 次,每次一个字节

    1. System.out.println(buffer.readByte());
    2. System.out.println(buffer.readByte());
    3. System.out.println(buffer.readByte());
    4. System.out.println(buffer.readByte());
    5. log(buffer);

    读过的内容,就属于废弃部分了,再读只能读那些尚未读取的部分

    1. 1
    2. 2
    3. 3
    4. 4
    5. read index:4 write index:12 capacity:16
    6. +-------------------------------------------------+
    7. | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
    8. +--------+-------------------------------------------------+----------------+
    9. |00000000| 00 00 00 05 00 00 00 06 |........ |
    10. +--------+-------------------------------------------------+----------------+

    如果需要重复读取 int 整数 5,怎么办?

    可以在 read 前先做个标记 mark

    1. buffer.markReaderIndex();
    2. System.out.println(buffer.readInt());
    3. log(buffer);

    结果

    1. 5
    2. read index:8 write index:12 capacity:16
    3. +-------------------------------------------------+
    4. | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
    5. +--------+-------------------------------------------------+----------------+
    6. |00000000| 00 00 00 06 |.... |
    7. +--------+-------------------------------------------------+----------------+

    这时要重复读取的话,重置到标记位置 reset

    1. buffer.resetReaderIndex();
    2. log(buffer);

    这时

    1. read index:4 write index:12 capacity:16
    2. +-------------------------------------------------+
    3. | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
    4. +--------+-------------------------------------------------+----------------+
    5. |00000000| 00 00 00 05 00 00 00 06 |........ |
    6. +--------+-------------------------------------------------+----------------+

    还有种办法是采用 get 开头的一系列方法,这些方法不会改变 read index