feat(新增): 🚀 netty的 ByteBufSlice使用

This commit is contained in:
Bunny 2024-05-24 22:39:16 +08:00
parent 596a22efd0
commit a616b10885
1 changed files with 35 additions and 0 deletions

View File

@ -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);
}
}