一、 参考资料
参考黑马netty教程
https://www.bilibili.com/video/BV1py4y1E7oA?p=88&spm_id_from=pageDriver&vd_source=4cd1b6f268e2a29a11bea5d2568836ee
二、 ByteBuf测试
app.java
package com.sht.test;import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.WriteBufferWaterMark;/*** Hello world!**/
public class App {public static void main(String[] args) {// ==============================================================// 自动扩容//buffer()方法使用直接内存,heapBuffer()方法使用堆内存ByteBuf buffer1 = ByteBufAllocator.DEFAULT.buffer();printByteBuffer("写数据前",buffer1);for (int i = 0; i < 300; i++) {buffer1.writeByte(i);} printByteBuffer("写数据后",buffer1);System.out.println("--------------------------------------------");// ==============================================================//写入,以write开头函数,会改变写指针//修改,以set开头的函数,不会改变写指针//读取,以read开头函数,会改变读指针// ==============================================================// 重复读取ByteBuf buffer2 = ByteBufAllocator.DEFAULT.buffer();buffer2.writeInt(1);buffer2.writeInt(2);printByteBuffer("写入两个int容量",buffer2);System.out.println("读取int:" + buffer2.readInt());printByteBuffer("读取1个int后容量",buffer2);buffer2.markReaderIndex();System.out.println("读取int:" + buffer2.readInt());printByteBuffer("读取2个int后容量",buffer2);buffer2.resetReaderIndex();printByteBuffer("reset读指针后",buffer2);System.out.println("--------------------------------------------");// ==============================================================// 内存回收//release()方法使得引用计数-1,计数为0时触发回收//retain()方法使得引用计数+1// ==============================================================// 零拷贝ByteBuf buffer3 = ByteBufAllocator.DEFAULT.buffer();for(int i=0;i<4;i++) {buffer3.writeInt(i+1);}ByteBuf buffer3_1 = buffer3.slice(0, 8);printByteBuffer("完整buffer",buffer3);printByteBuffer("分片1",buffer3_1);System.out.println("--------------------------------------------");//释放原有ByteBuf会影响分片ByteBuf,可以使用retain()//duplicate,全拷贝,读写指针独立//copy开头的方法,深拷贝// ==============================================================// coposite 组合小的ByteBuf为一个大的ByteBuf// ==============================================================// 零拷贝ByteBuf buffer4 = ByteBufAllocator.DEFAULT.buffer();int writeNum = Integer.MAX_VALUE;for(int i=0;i<writeNum;i++) {buffer4.writeInt(i+1);buffer4.readInt();if(i % 100000 == 0 && buffer4.readableBytes() == 0){//printByteBuffer("修改前"+i,buffer4);buffer4.readerIndex(0);buffer4.writerIndex(0);//printByteBuffer("修改后"+i,buffer4);}}printByteBuffer("读写结束后",buffer4);System.out.println("--------------------------------------------");}public static void printByteBuffer(String msg,ByteBuf buffer) {System.out.println(String.format("%s:读指针:%d,写指针:%d,容量:%d", msg,buffer.readerIndex(),buffer.writerIndex(),buffer.capacity()));}
}//运行结果
/*
写数据前:读指针:0,写指针:0,容量:256
写数据后:读指针:0,写指针:300,容量:512
--------------------------------------------
写入两个int容量:读指针:0,写指针:8,容量:256
读取int:1
读取1个int后容量:读指针:4,写指针:8,容量:256
读取int:2
读取2个int后容量:读指针:8,写指针:8,容量:256
reset读指针后:读指针:4,写指针:8,容量:256
--------------------------------------------
完整buffer:读指针:0,写指针:16,容量:256
分片1:读指针:0,写指针:8,容量:8
--------------------------------------------
读写结束后:读指针:334584,写指针:334584,容量:524288
--------------------------------------------
*/