diff --git a/netty/service/src/main/java/cn/bunny/service/netty/demo3/ByteByteBufAllocator.java b/netty/service/src/main/java/cn/bunny/service/netty/demo3/ByteByteBufAllocator.java new file mode 100644 index 0000000..199c2f2 --- /dev/null +++ b/netty/service/src/main/java/cn/bunny/service/netty/demo3/ByteByteBufAllocator.java @@ -0,0 +1,31 @@ +package cn.bunny.service.netty.demo3; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; + +import static cn.bunny.service.NettyLogUtil.log; + +public class ByteByteBufAllocator { + public static void main(String[] args) { + ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(10); + buf.writeBytes(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + + log(buf); + + ByteBuf buf1 = buf.slice(0, 5); + buf1.retain(); + log(buf1); + + ByteBuf buf2 = buf.slice(5, 5); + buf2.retain(); + log(buf2); + + // 释放原有的buf 内容 + buf.release(); + log(buf); + + // 释放 buf1和buf2内容 + buf1.release(); + buf2.release(); + } +}