1.什麼是緩衝區
在Java NIO中負責數據的存取,緩衝區的底層其實就是數組。用於存儲不同數據類型的數組。除了八大基本數據類型中的boolean
類型,其他的類型都有其對應的緩衝區。
ByteBuffer
CharBuffer
IntBuffer
ShortBuffer
LongBuffer
FloatBuffer
DoubleBuffer
2.緩存區屬性
屬性 | 說明 |
---|---|
容量(Capacity) | 緩衝區能夠容納的數據元素的最大數量,緩衝區創建時被設定,永遠不能被改變 |
上界(Limit) | 下一個要被讀或寫的元素的索引,位置會自動由相應的get()和put()方法更新 |
位置(Position) | 一個備忘位置,調用mark()方法來設定,mark=position,調用reset方法設定position=mark。標記在設定前是未定義的(undefined) |
mark <= position <= limit <= capacity
3.緩存區讀寫操作
String bufferStr = "buffer";
System.out.println("------------分配緩衝區allocate()-------------");
ByteBuffer buffer = ByteBuffer.allocate(1024);
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
/* ------------分配緩衝區allocate()-------------
0
1024
1024*/
System.out.println("------------緩衝區存數據put()-------------");
buffer.put(bufferStr.getBytes());
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
/*------------緩衝區存數據put()-------------
6
1024
1024*/
System.out.println("------------緩衝區切換到讀數據模式flip()-------------");
buffer.flip();
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
/* ------------緩衝區切換到讀數據模式flip()-------------
0
6
1024*/
System.out.println("------------緩衝區讀數據get()-------------");
byte[] bytes = new byte[buffer.limit()];
buffer.get(bytes);
System.out.println("緩存區存入的數據是>>>"+new String(bytes));
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
/* ------------緩衝區讀數據get()-------------
緩存區存入的數據是>>>buffer
6
6
1024*/
System.out.println("------------重複讀rewind()-------------");
buffer.rewind();
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
/* ------------重複讀rewind()-------------
0
6
1024*/
System.out.println("------------清除clear():處於被遺忘狀態,起始數據還在-------------");
buffer.clear();
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
/*------------清除clear()-------------
0
1024
1024*/
具體實現不做贅述。
4.mark()與reset()
mark()用作position定位,與reset()共同使用,可以使在調用reset()方法時讓postion返回到mark()方法調用時的postion。
String str = "buffer";
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put(str.getBytes());
buffer.flip();
byte[] bytes = new byte[buffer.limit()];
buffer.get(bytes,0,2);
System.out.println(new String(bytes,0,2));
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
/*2
6
1024*/
System.out.println("------------mark()--------------");
buffer.mark();
buffer.get(bytes,2,2);
System.out.println(new String(bytes,2,2));
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
/*------------mark()--------------
ff
4
6
1024*/
System.out.println("-------------reset()-------------");
buffer.reset();
System.out.println(buffer.position());
System.out.println(buffer.limit());
System.out.println(buffer.capacity());
/*-------------reset()-------------
2
6
1024*/