1.Buffer介紹
緩衝區是一個用於特定基本類型數據的容器。說白了就是對byte數組的封裝。緩衝區是特定基本類型元素的線性有限序列。已知子類:ByteBuffer, CharBuffer,ShortBuffer,IntBuffer,LongBuffer, DoubleBuffer, FloatBuffer。
Buffer 的作用是用於數據的傳輸。
非直接緩衝區:通過 allocate() 方法分配緩衝區,將緩衝區建立在 JVM 的內存中。(需要將數據從內核地址空間複製到用戶地址空間)
直接緩衝區:通過 allocateDirect() 方法分配直接緩衝區,將緩衝區建立在物理內存中。可以大大提高效率。(無需將數據從內核地址空間複製到用戶地址空間,直接將數據寫到內存頁中)
直接緩衝區:通過 allocateDirect() 方法分配直接緩衝區,將緩衝區建立在物理內存中。可以大大提高效率。(無需將數據從內核地址空間複製到用戶地址空間,直接將數據寫到內存頁中)
下節再詳細講直接緩衝區與非直接緩衝區。
2. Buffer的核心屬性
// Invariants: mark <= position <= limit <= capacity
private int mark = -1; //標記,表示記錄當前 position 的位置。可以通過 reset() 恢復到 mark 的位置
private int position = 0; //位置,表示緩衝區中正在操作數據的位置。
private int limit; //界限,表示緩衝區中可以操作數據的大小。(limit 後數據不能進行讀寫)
private int capacity; //容量,表示緩衝區中最大存儲數據的容量。一旦聲明不能改變。
3. Buffer的核心方法
put(byte b):將給定單個字節寫入緩衝區的當前位置。
put(byte[] src):將 src 中的字節寫入緩衝區的當前位置。
put(int index, byte b):將指定字節寫入緩衝區的索引位置(不會移動 position)。
get() :讀取單個字節。
get(byte[] dst):批量讀取多個字節到 dst 中。
get(int index):讀取指定索引位置的字節(不會移動 position)。
get(byte[] dst):批量讀取多個字節到 dst 中。
get(int index):讀取指定索引位置的字節(不會移動 position)。
mark():在緩衝區的當前位置設置標記。
reset(): 將此緩衝區的位置重置爲以前標記的位置。
flip():反轉此緩衝區,即由寫入緩衝區模式切換到從緩衝區讀取模式。將 limit = position,position = 0,mark = -1。
clear():清空此緩衝區。它將limit= capacity,position = 0,mark = -1。
rewind():使緩衝區爲重新讀取已包含的數據。它使限制保持不變,將 position = 0;mark= -1。
remaining(): 返回當前位置與限制之間的元素數。
isDirect():判斷此字節緩衝區是否爲直接緩衝區。
4.調用方法後屬性的變化圖
5.使用示例
@Test
public void test1(){
String str = "abcde";
//1. 分配一個指定大小的緩衝區
ByteBuffer buf = ByteBuffer.allocate(1024);
System.out.println("-----------------allocate()----------------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
//2. 利用 put() 存入數據到緩衝區中
buf.put(str.getBytes());
System.out.println("-----------------put()----------------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
//3. 切換讀取數據模式
buf.flip();
System.out.println("-----------------flip()----------------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
//4. 利用 get() 讀取緩衝區中的數據
byte[] dst = new byte[buf.limit()];
buf.get(dst);
System.out.println(new String(dst, 0, dst.length));
System.out.println("-----------------get()----------------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
//5. rewind() : 可重複讀
buf.rewind();
System.out.println("-----------------rewind()----------------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
//6. clear() : 清空緩衝區. 但是緩衝區中的數據依然存在,但是處於“被遺忘”狀態
buf.clear();
System.out.println("-----------------clear()----------------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
System.out.println((char)buf.get());
}
@Test
public void test2(){
String str = "abcde";
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.put(str.getBytes());
buf.flip();
byte[] dst = new byte[buf.limit()];
buf.get(dst, 0, 2);
System.out.println(new String(dst, 0, 2));
System.out.println(buf.position());
//mark() : 標記
buf.mark();
buf.get(dst, 2, 2);
System.out.println(new String(dst, 2, 2));
System.out.println(buf.position());
//reset() : 恢復到 mark 的位置
buf.reset();
System.out.println(buf.position());
//判斷緩衝區中是否還有剩餘數據
if(buf.hasRemaining()){
//獲取緩衝區中可以操作的數量
System.out.println(buf.remaining());
}
}
@Test
public void test3(){
//分配直接緩衝區
ByteBuffer buf = ByteBuffer.allocateDirect(1024);
System.out.println(buf.isDirect());
}