diff --git a/netty/service/src/main/java/cn/bunny/service/netty/demo3/ByteBufSlice.java b/netty/service/src/main/java/cn/bunny/service/netty/demo3/ByteBufSlice.java new file mode 100644 index 0000000..9212bc9 --- /dev/null +++ b/netty/service/src/main/java/cn/bunny/service/netty/demo3/ByteBufSlice.java @@ -0,0 +1,35 @@ +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 ByteBufSlice { + public static void main(String[] args) { + ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(10); + // +-------------------------------------------------+ + // | 0 1 2 3 4 5 6 7 8 9 a b c d e f | + // +--------+-------------------------------------------------+----------------+ + // |00000000| 61 62 63 64 65 66 67 68 69 6a 6b 6c |abcdefghijkl | + // +--------+-------------------------------------------------+----------------+ + buf.writeBytes(new byte[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'}); + log(buf); + + // +-------------------------------------------------+ + // | 0 1 2 3 4 5 6 7 8 9 a b c d e f | + // +--------+-------------------------------------------------+----------------+ + // |00000000| 61 62 63 64 65 |abcde | + // +--------+-------------------------------------------------+----------------+ + ByteBuf buf1 = buf.slice(0, 5); + log(buf1); + + // +-------------------------------------------------+ + // | 0 1 2 3 4 5 6 7 8 9 a b c d e f | + // +--------+-------------------------------------------------+----------------+ + // |00000000| 66 67 68 69 6a |fghij | + // +--------+-------------------------------------------------+----------------+ + ByteBuf buf2 = buf.slice(5, 5); + log(buf2); + } +}