Java NIO Buffer中各种状态属性的含义

关于NIO Buffer中的3个重要状态属性的含义: postion, limit与capacity。

public class NioTest
{
public static void main(String[] args) {
IntBuffer buffer = IntBuffer.allocate(10); System.out.println("capacity:" + buffer.capacity()); for(int i = 0 ; i < 5; i++){
int randomNumber = new SecureRandom().nextInt(20);
buffer.put(randomNumber);
} System.out.println("before flip limit: " + buffer.limit()); buffer.flip(); System.out.println("after flip limit: " + buffer.limit()); while (buffer.hasRemaining()){
System.out.println("position: " + buffer.position());
System.out.println("limit: " + buffer.limit());
System.out.println("capacity: " + buffer.capacity());
System.out.println(buffer.get());
}
}
}

  输出结果:

capacity:10
before flip limit: 10
after flip limit: 5
position: 0
limit: 5
capacity: 10
7
position: 1
limit: 5
capacity: 10
14
position: 2
limit: 5
capacity: 10
11
position: 3
limit: 5
capacity: 10
14
position: 4
limit: 5
capacity: 10
0

  

上一篇:Effective Objective-C 2.0 — 第二条:类的头文件中尽量少引入其他头文件


下一篇:【BZOJ】1923 [Sdoi2010]外星千足虫(高斯消元)