(一)JAVA NIO BUFFER

1.变量:

capacity: 指定了可以存储在缓冲区中的最大数据容量,实际上,它指定了底层数组的大小,或者至少是指定了准许我们使用的底层数组的容量。
position: 下一次读取或写入的位置。(每次调用get put方法+1)
limit:指定还有多少数据需要取出,或者还有多少空间可以放入数据。(每次调用get put方法时候,通过position与limit对比)

2.方法:

get:position + 1
put:position + 1
flip:limit = position position = 0 mark = -1
clear:limit = capacity position = 0 mark = -1
mark: mark = position
reset:position = m;
duplicate:浅拷贝(每个缓存区的上界、容量、位置等属性是各自独立的)

3.代码

BufferTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class BufferTest {
public static void main(String[] args) {
IntBuffer buff = IntBuffer.allocate(10);
buff.get();
buff.put(1);
buff.get();
buff.put(2);
buff.get();
buff.put(3);
buff.mark();
buff.get();
buff.put(4);
buff.get();
buff.put(5);
System.out.println(" ========== init ========== ");
System.out.println("capacity:\t" + buff.capacity());
System.out.println("limit:\t" + buff.limit());
System.out.println("position:\t" + buff.position());
System.out.println();
buff.reset();
System.out.println(" ========== mark reset ========== ");
System.out.println("capacity:\t" + buff.capacity());
System.out.println("limit:\t" + buff.limit());
System.out.println("position:\t" + buff.position());
System.out.println();
buff.flip();
System.out.println(" ========== flip ========== ");
System.out.println("capacity:\t" + buff.capacity());
System.out.println("limit:\t" + buff.limit());
System.out.println("position:\t" + buff.position());
for (int i = 0; i < buff.limit(); i++) {
System.out.print(buff.get() + "\t");
}
System.out.println();
System.out.println();
buff.clear();
System.out.println(" ========== clear ========== ");
System.out.println("capacity:\t" + buff.capacity());
System.out.println("limit:\t" + buff.limit());
System.out.println("position:\t" + buff.position());
for (int i = 0; i < buff.limit(); i++) {
System.out.print(buff.get() + "\t");
}
System.out.println();
System.out.println();

//重新数据初始化
System.out.println(" ========== duplicate ========== ");
buff.clear();
IntBuffer copyIntBuffer = buff.duplicate();
buff.get();
buff.put(2);
System.out.println(" ========== buff duplicate ========== ");
System.out.println("capacity:\t" + buff.capacity());
System.out.println("limit:\t" + buff.limit());
System.out.println("position:\t" + buff.position());
System.out.println(buff);
System.out.println(" ========== copyIntBuffer duplicate ========== ");
System.out.println("capacity:\t" + copyIntBuffer.capacity());
System.out.println("limit:\t" + copyIntBuffer.limit());
System.out.println("position:\t" + copyIntBuffer.position());
for (int i = 0; i < buff.limit(); i++) {
System.out.print(copyIntBuffer.get() + "\t");
}
System.out.println();
}
}
4.Console
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 ========== init ========== 
capacity: 10
limit: 10
position: 10

========== mark reset ==========
capacity: 10
limit: 10
position: 6

========== flip ==========
capacity: 10
limit: 6
position: 0
0 1 0 2 0 3

========== clear ==========
capacity: 10
limit: 10
position: 0
0 1 0 2 0 3 0 4 0 5

========== duplicate ==========
========== buff duplicate ==========
capacity: 10
limit: 10
position: 2
java.nio.HeapIntBuffer[pos=2 lim=10 cap=10]
========== copyIntBuffer duplicate ==========
capacity: 10
limit: 10
position: 0
0 2 0 2 0 3 0 4 0 5

Nio源码地址​github.com