java nio.Buffer的属性变化

java nio.Buffer的属性变化

认识Buffer

Channel 提供从文件、网络读取数据的渠道,但是读取或写入的数据都必须经由 Buffer。Buffer,实际上是一个连续数组。

常用的 Buffer 的子类有:ByteBuffer、IntBuffer、 CharBuffer、 LongBuffer、 DoubleBuffer、FloatBuffer、ShortBuffer。

所有缓冲区都有4个属性:capacity、limit、position、mark。

  • Capacity 容量,即可以容纳的最大数据量;在缓冲区创建时被设定并且不能改变
  • Limit 表示缓冲区的当前终点,不能对缓冲区超过极限的位置进行读写操作。且极限是可以修改的
  • Position 位置,下一个要被读或写的元素的索引,每次读写缓冲区数据时都会改变改值,为下次读写作准备
  • Mark 标记,调用mark()来设置mark=position,再调用reset()可以让position恢复到标记的位置

Buffer的核心属性

下面为Buffer中重要的属性

// Invariants: mark <= position <= limit <= capacity
private int mark = -1;
private int position = 0;
private int limit;
private int capacity;

Buffer的属性变化

代码

ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
ByteBuffer byteBuffer2 = ByteBuffer.wrap(new String("12345").getBytes());

System.out.println("******************** b1和b2的初始值对比 ********************");
System.out.println("byteBuffer  ==  "+byteBuffer);
System.out.println("byteBuffer2  ==  "+byteBuffer2);

System.out.println("********************** flip()的作用 *******************");
String str = "123456789";
byteBuffer.put(str.getBytes());
System.out.println("byteBuffer flip()前 ==  "+byteBuffer);
byteBuffer.flip();
System.out.println("byteBuffer flip()后 ==  "+byteBuffer);

System.out.println("******************** clear()的作用 ********************");
System.out.println("byteBuffer clear()前 ==  "+byteBuffer);
byteBuffer.clear();
System.out.println("byteBuffer clear()后 ==  "+byteBuffer);

结果

******************** b1和b2的初始值对比 ********************
byteBuffer  ==  java.nio.HeapByteBuffer[pos=0 lim=1024 cap=1024]
byteBuffer2  ==  java.nio.HeapByteBuffer[pos=0 lim=5 cap=5]
********************** flip()的作用 *******************
byteBuffer flip()前 ==  java.nio.HeapByteBuffer[pos=9 lim=1024 cap=1024]
byteBuffer flip()后 ==  java.nio.HeapByteBuffer[pos=0 lim=9 cap=1024]
******************** clear()的作用 ********************
byteBuffer clear()前 ==  java.nio.HeapByteBuffer[pos=0 lim=9 cap=1024]
byteBuffer clear()后 ==  java.nio.HeapByteBuffer[pos=0 lim=1024 cap=1024]

flip()和clear()的作用

代码

System.out.println("************* 没使用flip()和clear()带来的问题 **************");
String str2 = "123456789";
String str3 = "abc";
byteBuffer.put(str2.getBytes());
System.out.println("从buffer中读取数据  ==  "+new String(byteBuffer.array()));
byteBuffer.put(str3.getBytes());
System.out.println("从buffer中读取数据  ==  "+new String(byteBuffer.array()));

byteBuffer.clear();
System.out.println("************* 使用flip()和clear()和limit()解决重复读问题**************");
String str4 = "123456789";
String str5 = "abc";

byteBuffer.put(str4.getBytes());
byteBuffer.flip();
byte[] tem = new byte[byteBuffer.limit()];
byteBuffer.get(tem);
System.out.println("从buffer中读取数据  ==  "+new String(tem));
byteBuffer.clear();

byteBuffer.put(str5.getBytes());
byteBuffer.flip();
tem = new byte[byteBuffer.limit()];
byteBuffer.get(tem);
System.out.println("从buffer中读取数据  ==  "+new String(tem));
byteBuffer.clear();

结果

************* 没使用flip()和clear()带来的问题 **************
从buffer中读取数据  ==  123456789                  
从buffer中读取数据  ==  123456789abc                     
************* 使用flip()和clear()和limit()解决重复读问题**************
从buffer中读取数据  ==  123456789
从buffer中读取数据  ==  abc
posted @   鸭梨的药丸哥  阅读(5)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示