package bhz.nio.test;
import java.nio.IntBuffer;
/**
* Created by sujk on 2017/3/14.
*/
public class TestUseBuffer {
public static void main(String[] args) {
IntBuffer buf = IntBuffer.allocate(10);
System.out.println("初始化: " + buf);
assert buf.position()==0;
assert buf.limit()==10;
assert buf.capacity()==10;
buf.put(13);// position位置:0 - > 1
buf.put(21);// position位置:1 - > 2
buf.put(35);// position位置:2 - > 3
System.out.println("调用put添加元素:" + buf);
assert buf.position()==3;
assert buf.limit()==10;
assert buf.capacity()==10;
buf.flip();
System.out.println("使用flip复位:" + buf);
assert buf.position()==0;
assert buf.limit()==3;
assert buf.capacity()==10;
IntBuffer buf1 = IntBuffer.wrap(new int[]{1,2,5});
System.out.println("调用wrap:"+buf1);
assert buf1.position()==0;
assert buf1.limit()==3;
assert buf1.capacity()==3;
System.out.println("调用put(int []):"+ buf1.put(new int[]{1,2,5}));
assert buf1.position()==3;
assert buf1.limit()==3;
assert buf1.capacity()==3;
IntBuffer buf3 = buf1.duplicate();
System.out.println("调用duplicate:"+buf3);
assert buf1.position()==3;
assert buf1.limit()==3;
assert buf1.capacity()==3;
}
}
Buffer中写数据对position,limit,capacity值的影响
最新推荐文章于 2021-02-16 23:26:02 发布