1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| @Test
public void test2() {
ByteBuf b = getBuf();
System.out.println("原始数据:" + b);
//原始数据:UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 4, widx: 12, cap: 1024)
b = getBuf();
System.out.println("copy:" + b.copy());
//copy:UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 8, cap: 8)
b = getBuf();
System.out.println("duplicate:" + b.duplicate());
//duplicate:UnpooledDuplicatedByteBuf(ridx: 4, widx: 12, cap: 1024, unwrapped: UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 4, widx: 12, cap: 1024))
b = getBuf();
System.out.println("slice;" + b.slice());
//slice;UnpooledSlicedByteBuf(ridx: 0, widx: 8, cap: 8/8, unwrapped: UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 4, widx: 12, cap: 1024))
}
|
1
2
3
4
5
6
7
8
9
10
11
| //返回一个总长度为1024,数据长度为12,读索引为4,写索引为12的bytebuf
public static ByteBuf getBuf() {
ByteBuf b = Unpooled.buffer(1024);
b.writeInt(1);
b.writeInt(2);
b.writeInt(3);
b.readInt();
return b;
}
|