说明
io.netty.buffer.ByteBuf可以调用skipBytes(int length)函数跳出length个字节,并将buffer的readerIndex增长length。
示例
package com.thb;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.ByteProcessor;public class Test {public static void main(String[] args) {// 创建一个ByteBufByteBuf buf = Unpooled.buffer();// 写入10个字节的数据for (int i = 0; i < 10; i++) {buf.writeByte(0x68);}System.out.println("before skip:");System.out.println("buf.readerIndex: " + buf.readerIndex());System.out.println("buf.readableBytes: " + buf.readableBytes());// 跳过5个字节,readerIndex会增长5buf.skipBytes(5);System.out.println("----------------------------");System.out.println("after skip 5 bytes:");System.out.println("buf.readerIndex: " + buf.readerIndex());System.out.println("buf.readableBytes: " + buf.readableBytes());}
}
运行输出:
before skip:
buf.readerIndex: 0
buf.readableBytes: 10
----------------------------
after skip 5 bytes:
buf.readerIndex: 5
buf.readableBytes: 5