slice
slice 【零拷贝】的体现之一,对原始 ByteBuf 进行切片成多个ByteBuf ,切片后的ByteBuf并没有发生内存复制,还是使用原来 ByteBuf 的内存,切片后的 ByteBuf 维护独立的 read、write 指针
@Slf4j
public class TestSlice {
public static void main(String[] args) {
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(10);
buffer.writeBytes(new byte[]{'a','b','c','d','e','f','g','h','i'});
byte[] readBytes = new byte[buffer.readableBytes()];
// buffer.readBytes(readBytes);
// log.debug("{}",new String(readBytes, StandardCharsets.UTF_8));
// 在切片过程中,没有发生内存复制
ByteBuf s1 = buffer.slice(0, 5);
s1.retain();
ByteBuf s2 = buffer.slice(5, 5);
s2.retain();
// byte[] r1 = new byte[s1.readableBytes()];
// s1.readBytes(r1);
// log.debug("{}",new String(r1, StandardCharsets.UTF_8));
byte[] r2 = new byte[s2.readableBytes()];
s2.readBytes(r2);
log.debug("{}",new String(r2, StandardCharsets.UTF_8));
s1.setByte(0,'b');
byte[] r1 = new byte[s1.readableBytes()];
s1.readBytes(r1);
log.debug("{}",new String(r1, StandardCharsets.UTF_8));
buffer.readBytes(readBytes);
log.debug("{}",new String(readBytes, StandardCharsets.UTF_8));
buffer.release();
s1.release();
s2.release();
}
}
duplicate
【零拷贝】的体现之一,就好比截取了原始ByteBuf 所有内容,并且没有max capacity 的限制,也是与原始 ByteBuf 使用同一块底层内存,只是读写指针是独立的
copy
会将底层内存数据进行深拷贝,因此无论读写,都与原始 ByteBuf 无关
Composite
@Slf4j
public class TestComposite {
public static void main(String[] args) {
ByteBuf buf1 = ByteBufAllocator.DEFAULT.buffer();
buf1.writeBytes(new byte[]{1,2,3,4,5});
ByteBuf buf2 = ByteBufAllocator.DEFAULT.buffer();
buf2.writeBytes(new byte[]{6,7,8,9,10});
// ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
// buffer.writeBytes(buf1).writeBytes(buf2);
// byte[] r1 = new byte[buffer.readableBytes()];
// buffer.readBytes(r1);
// log.debug("{}",r1);
CompositeByteBuf byteBufs = ByteBufAllocator.DEFAULT.compositeBuffer();
byteBufs.addComponents(true, buf1,buf2);
byte[] r1 = new byte[byteBufs.readableBytes()];
byteBufs.readBytes(r1);
log.debug("{}",r1);
}
}
优势
- 池化思想,可以重用池中 ByteBuf 实例,更节约内存,减少内存溢出的可能
- 读写指针分离,不需要像 ByteBuffer 一样切换读写模式
- 可以自动扩容
- 支持链式调用,使用更流畅
- 很多地方体现零拷贝,例如:slice、duplicate、compositeBytebuf
ByteBuf 的基本知识就到这里了,到本文这里,Netty的基础知识就学习完毕了,后面就是进阶篇了,坚持了半个月真是不容易,搞技术就是贵在坚持,希望你也能跟我一直坚持下去,共同进步。
持续关注,后续更精彩