Netty的ByteBuf是一个强大的字节容器,用于处理字节数据。它提供了比Java标准库中的ByteBuffer更灵活和高效的方式来操作字节数据。
Netty的ByteBuf是一个字节容器,它提供了一种更灵活和高效的方式来操作字节数据。与ByteBuffer不同,ByteBuf具有可扩展的缓冲区,可以动态调整容量,而不需要创建新的缓冲区对象。
ByteBuf有三个关键的指针,分别是readerIndex、writerIndex和capacity。
ByteBuf buffer = Unpooled.buffer(10); // 创建一个初始容量为10的ByteBuf
// 写入数据
buffer.writeBytes("Hello".getBytes());
// 读取数据
byte[] data = new byte[buffer.readableBytes()];
buffer.readBytes(data);
System.out.println(new String(data));
// 引用计数 +1
buffer.retain();
// 引用计数 -1,如果引用计数为0,则释放相关资源
buffer.release();
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
public class NettyByteBuf {
public static void main(String[] args) {
// 创建byteBuf对象,该对象内部包含一个字节数组byte[14]
// 通过readerindex和writerIndex和capacity,将buffer分成三个区域
// 已经读取的区域:[0,readerindex)
// 可读取的区域:[readerindex,writerIndex)
// 可写的区域: [writerIndex,capacity)
ByteBuf byteBuf = Unpooled.buffer(14);
System.out.println("byteBuf=" + byteBuf);
printBytebuf(byteBuf);
System.out.println("=================");
for (int i = 0; i < 8; i++) {
byteBuf.writeByte(i);
}
System.out.println("byteBuf=" + byteBuf);
printBytebuf(byteBuf);
System.out.println("=================");
for (int i = 0; i < 5; i++) {
System.out.println(byteBuf.getByte(i));
}
System.out.println("byteBuf=" + byteBuf);
printBytebuf(byteBuf);
System.out.println("=================");
for (int i = 0; i < 5; i++) {
System.out.println(byteBuf.readByte());
}
System.out.println("byteBuf=" + byteBuf);
printBytebuf(byteBuf);
System.out.println("=================");
//用Unpooled工具类创建ByteBuf
String text = "hello,artisan!" ;
ByteBuf byteBuf2 = Unpooled.copiedBuffer(text , CharsetUtil.UTF_8);
//使用相关的方法
if (byteBuf2.hasArray()) {
byte[] content = byteBuf2.array();
//将 content 转成字符串
System.out.println(new String(content, CharsetUtil.UTF_8));
System.out.println("byteBuf2=" + byteBuf2);
printBytebuf(byteBuf2);
System.out.println("=================");
// 获取数组0这个位置的字符h的ascii码,h=104
System.out.println(byteBuf2.getByte(0));
System.out.println("=================");
//可读的字节数 14
int len = byteBuf2.readableBytes();
System.out.println("len=" + len);
printBytebuf(byteBuf2);
System.out.println("=================");
//使用for取出各个字节
for (int i = 0; i < len; i++) {
System.out.println((char) byteBuf2.getByte(i));
}
printBytebuf(byteBuf2);
System.out.println("=================");
//范围读取
System.out.println(byteBuf2.getCharSequence(0, 6, CharsetUtil.UTF_8));
printBytebuf(byteBuf2);
System.out.println("=================");
System.out.println(byteBuf2.getCharSequence(6, 8, CharsetUtil.UTF_8));
printBytebuf(byteBuf2);
}
}
public static void printBytebuf(ByteBuf byteBuf){
System.out.println("readerIndex:" + byteBuf.readerIndex());
System.out.println("writerIndex:" + byteBuf.writerIndex());
System.out.println("capacity:" + byteBuf.capacity());
}
}
输出
byteBuf=UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 0, cap: 14)
readerIndex:0
writerIndex:0
capacity:14
=================
byteBuf=UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 8, cap: 14)
readerIndex:0
writerIndex:8
capacity:14
=================
0
1
2
3
4
byteBuf=UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 8, cap: 14)
readerIndex:0
writerIndex:8
capacity:14
=================
0
1
2
3
4
byteBuf=UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 5, widx: 8, cap: 14)
readerIndex:5
writerIndex:8
capacity:14
=================
hello,artisan!
byteBuf2=UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 14, cap: 42)
readerIndex:0
writerIndex:14
capacity:42
=================
104
=================
len=14
readerIndex:0
writerIndex:14
capacity:42
=================
h
e
l
l
o
,
a
r
t
i
s
a
n
!
readerIndex:0
writerIndex:14
capacity:42
=================
hello,
readerIndex:0
writerIndex:14
capacity:42
=================
artisan!
readerIndex:0
writerIndex:14
capacity:42