-
缓冲区(Buffer):
一个用于特定基本数据类行的容器。有java.nio包定义的,所有缓冲区都是抽象类Buffer的子类。
Java NIO中的Buffer主要用于与NIO通道进行交互,数据是从通道读入到缓冲区,从缓冲区写入通道中的。
Buffer就像一个数组,可以保存多个相同类型的数据。根据类型不同(boolean除外),有以下Buffer常用子类:
-
ByteBuffer
-
CharBuffer
-
ShortBuffer
-
IntBuffer
-
LongBuffer
-
FloatBuffer
-
DoubleBuffer
上述Buffer类他们都采用相似的方法进行管理数据,只是各自管理的数据类型不同而已,都是通过以下方法获取一个Buffer对象:
static XxxBuffer allocate(int capacity)
创建一个容量为capacity的XxxBuffer对象。
-
Buffer中的重要概念:
1)容量(capacity):表示Buffer最大数据容量,缓冲区容量不能为负,并且建立后不能修改。
2)限制(limit):第一个不应该读取或者写入的数据的索引,即位于limit后的数据不可以读写。缓冲区的限制不能为负,并且不能大于其容量(capacity)。
3)位置(position):下一个要读取或写入的数据的索引。缓冲区的位置不能为负,并且不能大于其限制(limit)。
4)标记(mark)与重置(reset):标记是一个索引,通过Buffer中的mark()方法指定Buffer中一个特定的position,之后可以通过调用reset()方法恢复到这个position。
注意:0<=mark<=position<=capacity
测试代码:
1 package com.dx.nios;
2
3 import java.nio.ByteBuffer;
4
5 import org.junit.Test;
6
7 public class BufferTest {
8
9 @Test
10 public void TestBuffer() {
11 ByteBuffer byteBuffer = ByteBuffer.allocate(10);
12
13 System.out.println("------------allocate------------------");
14 System.out.println(byteBuffer.position());
15 System.out.println(byteBuffer.limit());
16 System.out.println(byteBuffer.capacity());
17
18
19 byteBuffer.put("abcde".getBytes());
20
21 System.out.println("------------put------------------");
22 System.out.println(byteBuffer.position());
23 System.out.println(byteBuffer.limit());
24 System.out.println(byteBuffer.capacity());
25
26 byteBuffer.flip();
27
28 System.out.println("------------flip------------------");
29 System.out.println(byteBuffer.position());
30 System.out.println(byteBuffer.limit());
31 System.out.println(byteBuffer.capacity());
32
33 }
34 }
输出结果:
------------allocate------------------ 0 10 10 ------------put------------------ 5 10 10 ------------flip------------------ 0 5 10
分析:


-
Buffer常用函数测试:
1 package com.dx.nios;
2
3 import java.nio.ByteBuffer;
4
5 import org.junit.Test;
6
7 public class BufferTest {
8
9 @Test
10 public void TestBuffer() {
11 // 1.使用allocate()申请10个字节的缓冲区
12 ByteBuffer byteBuffer = ByteBuffer.allocate(10);
13 System.out.println("------------allocate------------------");
14 System.out.println(byteBuffer.position());
15 System.out.println(byteBuffer.limit());
16 System.out.println(byteBuffer.capacity());
17
18 // 2.使用put()存放5个字节到缓冲区
19 byteBuffer.put("abcde".getBytes());
20 System.out.println("------------put------------------");
21 System.out.println(byteBuffer.position());
22 System.out.println(byteBuffer.limit());
23 System.out.println(byteBuffer.capacity());
24
25 // 3.切换到读取数据模式
26 byteBuffer.flip();
27 System.out.println("------------flip------------------");
28 System.out.println(byteBuffer.position());
29 System.out.println(byteBuffer.limit());
30 System.out.println(byteBuffer.capacity());
31
32 // 4.从缓冲区中读取数据
33 System.out.println("------------get------------------");
34 byte[] bytes = new byte[byteBuffer.limit()];
35 byteBuffer.get(bytes);
36 System.out.println(new String(bytes, 0, bytes.length));
37 System.out.println(byteBuffer.position());
38 System.out.println(byteBuffer.limit());
39 System.out.println(byteBuffer.capacity());
40
41 // 5.设置为可重复读取
42 System.out.println("------------rewind------------------");
43 byteBuffer.rewind();
44 System.out.println(byteBuffer.position());
45 System.out.println(byteBuffer.limit());
46 System.out.println(byteBuffer.capacity());
47 byte[] bytes2 = new byte[byteBuffer.limit()];
48 byteBuffer.get(bytes2);
49 System.out.println(new String(bytes2, 0, bytes2.length));
50 System.out.println(byteBuffer.position());
51 System.out.println(byteBuffer.limit());
52 System.out.println(byteBuffer.capacity());
53
54 // 6。clear清空缓存区,但是内容没有被清掉,还存在。只不过这些数据状态为被遗忘状态。
55 System.out.println("------------clear------------------");
56 byteBuffer.clear();
57 System.out.println(byteBuffer.position());
58 System.out.println(byteBuffer.limit());
59 System.out.println(byteBuffer.capacity());
60 byte[] bytes3 = new byte[10];
61 byteBuffer.get(bytes3);
62 System.out.println(new String(bytes3, 0, bytes3.length));
63 }
64 }
输出:
1 ------------allocate------------------ 2 0 3 10 4 10 5 ------------put------------------ 6 5 7 10 8 10 9 ------------flip------------------ 10 0 11 5 12 10 13 ------------get------------------ 14 abcde 15 5 16 5 17 10 18 ------------rewind------------------ 19 0 20 5 21 10 22 abcde 23 5 24 5 25 10 26 ------------clear------------------ 27 0 28 10 29 10 30 abcde
-
mark与reset的用法:
1 @Test
2 public void testMark() {
3 ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
4 byteBuffer.put("abcde".getBytes());
5 byteBuffer.flip();
6
7 byte[] bytes = new byte[byteBuffer.limit()];
8 byteBuffer.get(bytes, 0, 2);
9 System.out.println(new String(bytes, 0, bytes.length));
10
11 System.out.println(byteBuffer.position());
12 System.out.println(byteBuffer.limit());
13 System.out.println(byteBuffer.capacity());
14
15 byteBuffer.mark();
16 System.out.println("---------mark----------");
17
18 byteBuffer.get(bytes, 0, 2);
19 System.out.println(new String(bytes, 0, bytes.length));
20
21 System.out.println(byteBuffer.position());
22 System.out.println(byteBuffer.limit());
23 System.out.println(byteBuffer.capacity());
24
25 byteBuffer.reset();
26 System.out.println("---------reset----------");
27
28 System.out.println(byteBuffer.position());
29 System.out.println(byteBuffer.limit());
30 System.out.println(byteBuffer.capacity());
31 }
打印信息:
ab
2
5
1024
———mark———-
cd
4
5
1024
———reset———-
2
5
1024
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/15746.html