/**
* Netty 内存分配容器 ByteBuf
*/
public class ByteBufDemo {
public void doAlloc(ChannelHandlerContext ctx, UserModel userModel) {
// 初始化容器内存大小,
ByteBuf byteBuf = ctx.alloc().directBuffer().capacity(1024);
/**
* 经验表明,Bytebuf的最佳实践是在IO通信线程的读写缓冲区使用DirectByteBuf,后端业务使用HeapByteBuf
*/
// ByteBuf byteBurWriter = ctx.alloc().directBuffer().writeBytes(userModel.getProtoData().toByteArray);
}
public static void main(String[] args) throws Exception {
// testUnpooledHeapBuffer();
// testUnpooledDirectBuffer();
testComposite();
}
// 直接缓冲区
public static void testUnpooledDirectBuffer() throws Exception {
// 未池化的直接缓冲区
ByteBuf directBuffer = Unpooled.directBuffer(100);
directBuffer.writeBytes("directBUffer".getBytes());
//检查 ByteBuf 是否由数组支撑。如果不是,则这是一个直接缓冲区,是数组则应该放在堆内存
if (!directBuffer.hasArray()) {
int length = directBuffer.readableBytes();
byte[] array = new byte[length];
/**
* 先读取两个字节
* 结果如下: [114, 101, 99, 66, 85, 102, 102, 101, 114, 0, 0]
* 11
*/
directBuffer.readerIndex(2);
/**
* 将字节复制到数组
* 未读取之前结果如下: [100, 105, 114, 101, 99, 66, 85, 102, 102, 101, 114]
*/
directBuffer.getBytes(directBuffer.readerIndex(), array);
System.out.println(Arrays.toString(array));
System.out.println(length);
}
}
// 堆缓冲区
public static void testUnpooledHeapBuffer() {
ByteBuf heapBuf = Unpooled.copiedBuffer("heap buffer", CharsetUtil.UTF_8);
if (heapBuf.hasArray()) {
byte[] array = heapBuf.array();
int offerset = heapBuf.arrayOffset() + heapBuf.readerIndex();
int length = heapBuf.readableBytes();
System.out.println(Arrays.toString(array));
System.out.println(offerset);
System.out.println(length);
/**
* 结果如下:
* [104, 101, 97, 100, 32, 98, 117, 102, 102, 101, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
* 0
* 11
*/
}
}
/**
* 复合缓冲区
* Netty 通过一个 ByteBuf 子类——CompositeByteBuf——实现了组合模式,它提供了一
* 个将多个缓冲区表示为单个合并缓冲区的虚拟表示
*/
public static void printCompositeBuf(CompositeByteBuf compbuf) {
// 可读字节数
int length = compbuf.readableBytes();
byte[] array = new byte[length];
compbuf.getBytes(compbuf.readerIndex(), array);
System.out.println("array: " + Arrays.toString(array));
System.out.println("length: " + length);
}
public static void testComposite() {
// 复合缓冲区
CompositeByteBuf messageBuf = Unpooled.compositeBuffer();
ByteBuf headBuf = Unpooled.directBuffer(1024);
headBuf.writeBytes("head buffer".getBytes(StandardCharsets.UTF_8));
// ByteBuf headBuf = Unpooled.copiedBuffer("head buffer", StandardCharsets.UTF_8);
ByteBuf bodyBuf = Unpooled.copiedBuffer("body buffer", StandardCharsets.UTF_8);
//
messageBuf.addComponents(headBuf, bodyBuf);
System.out.println("headBuf 可读字节数: " + headBuf.readableBytes());
System.out.println("BodyBuf 可读字节数: " + bodyBuf.readableBytes());
System.out.println("messgeBuf可读字节数: " + messageBuf.readableBytes());
System.out.println("remove head before -------------");
printCompositeBuf(messageBuf);
for (ByteBuf buf : messageBuf) {
System.out.println(buf.toString(StandardCharsets.UTF_8));
}
// 根据数组下标删除元素
messageBuf.removeComponent(0);
System.out.println("remove head after-------------");
printCompositeBuf(messageBuf);
for (ByteBuf buf : messageBuf) {
System.out.println(buf.toString(StandardCharsets.UTF_8));
}
/**
* 结果如下:
* headBuf 可读字节数: 11
* BodyBuf 可读字节数: 11
* messgeBuf可读字节数: 0
* remove head before -------------
* array: []
* length: 0
* head buffer
* body buffer
* remove head after-------------
* array: []
* length: 0
* body buffer
*/
}
}
待完善