本篇内容介绍了“网状的中ByteBuf的三个重要属性介绍”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
由于JDK中提供的ByteBuffer无法动态扩容,并且API使用复杂等原因,网状的中提供了ByteBuf。
ByteBuf的API操作更加便捷,可以动态扩容,提供了多种ByteBuf的实现,以及高效的零拷贝机制。
ByteBuf的操作
ByteBuf有三个重要的属性:容量容量,readerIndex读取位置,writerIndex写入位置
提供了readerIndex和weiterIndex两个变量指针来支持顺序读和写操作
下图显示了一个缓冲区是如何被两个指针分割成三个区域的:
代码示例:
import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import java.util.Arrays; public class  ByteBufDemo { public 才能;static  void main (String [], args), { ,,,//,1。创建一个非池化的ByteBuf,大小为10个字节 ,,,ByteBuf buf =, Unpooled.buffer (10); ,,,System.out.println(“原始ByteBuf为:“,+,buf.toString ()); ,,,System.out.println (“1. bytebuf中的内容为:“,+,Arrays.toString (buf.array ()), +,“\ n"); ,,,//2。写入一段内容 ,,,byte [], bytes =, {1,, 2,, 3,, 4,, 5}; ,,,buf.writeBytes(字节); ,,,System.out.println(“写入的字节为:“,+,Arrays.toString(字节)); ,,,System.out.println(“写入一段内容后ByteBuf为:“,+,buf.toString ()); ,,,System.out.println (“2. bytebuf中的内容为:“,+,Arrays.toString (buf.array ()), +,“\ n"); ,,,//,3只;读取一段内容 ,,,byte b1 =, buf.readByte (); ,,,byte b2 =, buf.readByte (); ,,,System.out.println(“读取的字节为:“,+,Arrays.toString (new byte [], {b1, b2})); ,,,System.out.println(“读取一段内容后ByteBuf为:“,+,buf.toString ()); ,,,System.out.println (“3. bytebuf中的内容为:“,+,Arrays.toString (buf.array ()), +,“\ n"); ,,,//4。将读取的内容丢弃 ,,,buf.discardReadBytes (); ,,,System.out.println(“将读取的内容丢弃后ByteBuf为:“,+,buf.toString ()); ,,,System.out.println (“4. bytebuf中的内容为:“,+,Arrays.toString (buf.array ()), +,“\ n"); ,,,//5。清空读写指针 ,,,buf.clear (); ,,,System.out.println(“清空读写指针后ByteBuf为:“,+,buf.toString ()); ,,,System.out.println (“5. bytebuf中的内容为:“,+,Arrays.toString (buf.array ()), +,“\ n"); ,,,//,6。再次写入一段内容,比第一段内容少 ,,,byte [], bytes2 =, {1,, 2,, 3}; ,,,buf.writeBytes (bytes2); ,,,System.out.println(“写入的字节为:“,+,Arrays.toString (bytes2)); ,,,System.out.println(“写入一段内容后ByteBuf为:“,+,buf.toString ()); ,,,System.out.println (“6. bytebuf中的内容为:“,+,Arrays.toString (buf.array ()), +,“\ n"); ,,,//7。将ByteBuf清零 ,,,buf.setZero (0,, buf.capacity ()); ,,,System.out.println(“清零后ByteBuf为:“,+,buf.toString ()); ,,,System.out.println (“7. bytebuf中的内容为:“,+,Arrays.toString (buf.array ()), +,“\ n"); ,,,//8。再次写入一段超过容量的内容 ,,,byte [], bytes3 =, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; ,,,buf.writeBytes (bytes3); ,,,System.out.println(“写入的字节为:“,+,Arrays.toString(字节)); ,,,System.out.println(“写入一段内容后ByteBuf为:“,+,buf.toString ()); ,,,System.out.println (“8. bytebuf中的内容为:“,+,Arrays.toString (buf.array ()), +,“\ n"); ,,} }
ButeBuf动态扩容
能力默认值:256字节,最大值:整数。MAX_VALUE (2 g)
writeXXX方法调用时,通过AbstractByteBuf.ensureWritable0()方法进行检查
容量计算方法:AbstractByteBufAllocator.calculateNewCapacity
根据能力的最小值要求,对应有两套计算方法:
没超过4兆:从64字节开始,每次递增一倍,直至计算出来的newCapacity满足新容量最小要求
示例:当前大256年小,已写250年,继续写10字节的数据,需要的最小容量要求是261,则新容量为64 x2x2x2=512
超过4兆:新容量=新容量最小要求/4兆x4兆+ 4兆
示例:当前大小为3兆,已写3兆,继续写2兆,需要的最小容量大小兆为5,则新容量是8兆(不能超过最大值)
4兆的一个固定的阈值AbstractByteBufAllocator。null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null