目录
- Netty自己的ByteBuff
- ByteBuf做的了哪些增强
- ByteBuf操作
- 基本使用
- ByteBuf动态扩容
- 选择合适的ByteBuf实现
- Unsafe的实现
- PooledByteBuf对象、内存复用
- 零拷贝机制
![Netty零拷贝机制](https://img.it610.com/image/info8/1c9df327ff43497683373721a7295ad7.jpg)
文章图片
ByteBuf做的了哪些增强
![Netty零拷贝机制](https://img.it610.com/image/info8/baca1760c6094b2eb4b495a720eeac85.jpg)
文章图片
ByteBuf操作
NIO只有一个位置的字段,即positon。
![Netty零拷贝机制](https://img.it610.com/image/info8/38db303a1acb421d853a18b6e29a2bf6.jpg)
文章图片
![Netty零拷贝机制](https://img.it610.com/image/info8/60b874d08d8c489eb6aa46f9fa72f0a1.jpg)
文章图片
基本使用
/**
* bytebuf的常规API操作示例
*/
public class ByteBufDemo {
@Test
public void apiTest() {
//+-------------------+------------------+------------------+
//| discardable bytes |readable bytes|writable bytes|
//||(CONTENT)||
//+-------------------+------------------+------------------+
//||||
//0<=readerIndex<=writerIndex<=capacity// 1.创建一个非池化的ByteBuf,大小为10个字节
ByteBuf buf = Unpooled.buffer(10);
System.out.println("原始ByteBuf为====================>" + buf.toString());
System.out.println("1.ByteBuf中的内容为===============>" + Arrays.toString(buf.array()) + "\n");
// 2.写入一段内容
byte[] bytes = {1, 2, 3, 4, 5};
buf.writeBytes(bytes);
System.out.println("写入的bytes为====================>" + Arrays.toString(bytes));
System.out.println("写入一段内容后ByteBuf为===========>" + buf.toString());
System.out.println("2.ByteBuf中的内容为===============>" + Arrays.toString(buf.array()) + "\n");
// 3.读取一段内容
byte b1 = buf.readByte();
byte b2 = buf.readByte();
System.out.println("读取的bytes为====================>" + Arrays.toString(new byte[]{b1, b2}));
System.out.println("读取一段内容后ByteBuf为===========>" + buf.toString());
System.out.println("3.ByteBuf中的内容为===============>" + Arrays.toString(buf.array()) + "\n");
// 4.将读取的内容丢弃
buf.discardReadBytes();
System.out.println("将读取的内容丢弃后ByteBuf为========>" + buf.toString());
System.out.println("4.ByteBuf中的内容为===============>" + Arrays.toString(buf.array()) + "\n");
// 5.清空读写指针
buf.clear();
System.out.pr intln("将读写指针清空后ByteBuf为==========>" + buf.toString());
System.out.println("5.ByteBuf中的内容为===============>" + Arrays.toString(buf.array()) + "\n");
// 6.再次写入一段内容,比第一段内容少
byte[] bytes2 = {1, 2, 3};
buf.writeBytes(bytes2);
System.out.println("写入的bytes为====================>" + Arrays.toString(bytes2));
System.out.println("写入一段内容后ByteBuf为===========>" + buf.toString());
System.out.println("6.ByteBuf中的内容为===============>" + Arrays.toString(buf.array()) + "\n");
// 7.将ByteBuf清零
buf.setZero(0, buf.capacity());
System.out.println("将内容清零后ByteBuf为==============>" + buf.toString());
System.out.println("7.ByteBuf中的内容为================>" + Arrays.toString(buf.array()) + "\n");
// 8.再次写入一段超过容量的内容
byte[] bytes3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
buf.writeBytes(bytes3);
System.out.println("写入的bytes为====================>" + Arrays.toString(bytes3));
System.out.println("写入一段内容后ByteBuf为===========>" + buf.toString());
System.out.println("8.ByteBuf中的内容为===============>" + Arrays.toString(buf.array()) + "\n");
//随机访问索引 getByte
//顺序读 read*
//顺序写 write*
//清除已读内容 discardReadBytes
//清除缓冲区 clear
//搜索操作
//标记和重置
//完整代码示例:参考
// 搜索操作 读取指定位置 buf.getByte(1);
//
}}
ByteBuf动态扩容
![Netty零拷贝机制](https://img.it610.com/image/info8/d5bbe3238a0b47c58c79bb53e184789a.jpg)
文章图片
选择合适的ByteBuf实现
【Netty零拷贝机制】unsafe可能会导致不安全。
![Netty零拷贝机制](https://img.it610.com/image/info8/f284f5eb2f8b4b81a173da86db4532c9.jpg)
文章图片
Unsafe的实现
![Netty零拷贝机制](https://img.it610.com/image/info8/c45c1a1ea65b4679821f5fcf0e508aac.jpg)
文章图片
PooledByteBuf对象、内存复用
![Netty零拷贝机制](https://img.it610.com/image/info8/76bc9a41a27c434f85a61dac9469042f.jpg)
文章图片
零拷贝机制
零拷贝机制:不去改变数据,实现数据的合并、拆分、转换操作。通过逻辑上的优化,带来性能上的提升。
- Netty数据不动,逻辑上进行合并。2个数组或者多个数组合并为一个数组,数组需要重新新建一个数组进行拷贝。
- 从逻辑上进行包转转换,数据不动。
- 从逻辑上进行拆分,实际数据不动。
文章图片