Netty(二) ByteBuf
Netty ByteBuf 是NIO中ByteBuffer的封装,相比JDK ByteBuffer更加易用;
- 为读/写分别维护单独的指针,不需要通过flip()进行读写模式切换
- 容量自定伸缩(类似ArrayList)
- Fluent API(链式调用)
- 通过内置的CompositeBuffer来减少数据拷贝(Zero copy)
- 内存池,减少GC压力
一.内部结构
ByteBuf通过两个索引(reader index、writer index)划分为三个区域:
- read index前面的数据是已经读过的数据,这些数据是可丢弃的数据
- 从reader index开始,到write index之前的数据都是可读数据
-
从write index开始,为可写区域
image.png
二.主要API
顺序读/写:- 改变reader/writer indexwriteByte()
writeLong()
writeXXX() - 增加write index
readByte()
readLong()
readXXX() - 增加read index
public static void writeAndRead() {
ByteBuf buffer = Unpooled.buffer(20); //get reference form somewhere
int i = 0;
;
while (buffer.writableBytes() >= 4) {
buffer.writeInt(i++);
}
while (buffer.isReadable()) {
System.out.println(buffer.readInt());
//清除已读
// buffer.discardReadBytes();
}
}
随机读/写: - 不改变read/write index
getXXX(index)
setXXX(index, byte)
public static void byteBufSetGet() {
Charset utf8 = Charset.forName("UTF-8");
ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", utf8);
System.out.println((char)buf.getByte(0));
int readerIndex = buf.readerIndex();
int writerIndex = buf.writerIndex();
System.out.println("readerIndex = " + readerIndex + "; writerIndex = " + writerIndex);
buf.setByte(0, (byte)'B');
System.out.println((char)buf.getByte(0));
System.out.println("readerIndex = " + buf.readerIndex() + "; writerIndex = " + buf.writerIndex());
}
mark/reset:
markReaderIndex()
markWriterIndex()
resetReaderIndex()
resetWriterIndex()
writerIndex(index)
readerIndex(index)
丢弃不可读数据
discardReadBytes方法 - 类似compact 方法,丢弃不可读的数据,readIndex置为0,writeIndex递减;
clear方法 初始化
image.png
查询方法:
indexOf
bytesBefore
forEachByte(ByteBufProcessor)
根据内存的位置
HeapByteBuf
基于数组- 内部为一个字节数组 (byte array)
hasArray()返回True
array()返回其内部的数组,可以对数组进行直接操作
public static void heapBuffer() {
ByteBuf heapBuf = Unpooled.buffer(16);
if (heapBuf.hasArray()) {
int i = 0;
while (heapBuf.writableBytes() > 0) {
heapBuf.writeByte(i++);
}
byte[] array = heapBuf.array();
int offset = heapBuf.arrayOffset() + heapBuf.readerIndex();
int length = heapBuf.readableBytes();
handleArray(array, offset, length);
}
}
DirectByteBuf
jvm堆外内存
具有更好的性能
创建和释放开销更大
public static void directBuffer() {
ByteBuf directBuf = Unpooled.directBuffer(16);
if (!directBuf.hasArray()) {
int i = 0;
while (directBuf.writableBytes() > 0) {
directBuf.writeByte(i++);
}
int length = directBuf.readableBytes();
byte[] array = new byte[length];
directBuf.getBytes(directBuf.readerIndex(), array);
handleArray(array, 0, length);
}
}
三 ByteBuf分配
不直接通过new来创建,而是通过ByteBufAllocator来创建
UnpooledByteBufAllocator
PooledByteBufAllocator
image.png
private static void createByteBuf() {
ByteBuf buf1 = UnpooledByteBufAllocator.DEFAULT.buffer(10);
ByteBuf buf2 = UnpooledByteBufAllocator.DEFAULT.heapBuffer(10);
ByteBuf buf3 = UnpooledByteBufAllocator.DEFAULT.directBuffer(10);
ByteBuf buf4 = UnpooledByteBufAllocator.DEFAULT.compositeBuffer(10);
ByteBuf buf5 = UnpooledByteBufAllocator.DEFAULT.compositeHeapBuffer(10);
ByteBuf buf6 = UnpooledByteBufAllocator.DEFAULT.compositeDirectBuffer(10);
ByteBuf buf7 = UnpooledByteBufAllocator.DEFAULT.ioBuffer(10);
}
Unpooled 的工具类,它提供了静态的辅助方法来创建未池化的ByteBuf实例
image.png
private static void createByteBuf2() {
Charset utf8 = Charset.forName("UTF-8");
ByteBuf buf1 = Unpooled.buffer(10);
ByteBuf buf2 = Unpooled.directBuffer(10);
String content = "Netty in Action rocks!";
ByteBuf buf3 = Unpooled.wrappedBuffer(content.getBytes());
ByteBuf buf4 = Unpooled.copiedBuffer(content, utf8);
}
以上,简单介绍了下Netty中ByteBuf的使用,如果想详细了解内部实现,可自行翻阅源码.如有错误,欢迎指正;