Netty進階基礎篇之NIO Buffer篇(3)

一、Buffer概念

1.1 緩衝區獲取

Buffer緩衝區是就是一個數組,有着不一樣的數據類型:ByteBuffer、CharBuffer、ShortBuffer、IntBuffer、LongBuffer、FloatBuffer、DoubleBuffer,而後這些數據類型均可以經過 allocate() 獲取緩衝區。數組

static XxxBuffer allocate(int capacity) : 建立一個容量爲 capacity 的 XxxBuffer 對象,以下:測試

//1. 分配一個指定大小的Byte類型的緩衝區
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

1.2  緩衝區存取數據的兩個核心方法

put() : 存入數據到緩衝區中spa

put方法
put(byte b) 將給定單個字節寫入緩衝區的當前位置

put(byte[] src)對象

將 src 中的字節寫入緩衝區的當前位置blog

put(int index, byte b)索引

將指定字節寫入緩衝區的索引位置(不會移動 position)ip


get() : 獲取緩衝區中的數據內存

get方法

get()ci

讀取單個字節rem

get(byte[] dst)

批量讀取多個字節到 dst 中

get(int index)

讀取指定索引位置的字節(不會移動 position)

byteBuffer.put(「Mujiutian」);
byteBuffer.get();

1.3 緩衝區中的四個核心屬性

0 <= mark <= position <= limit <= capacity

capacity : 容量,表示緩衝區中最大存儲數據的容量。一旦聲明不能改變。

byteBuffer.capacity()

limit : 界限,表示緩衝區中能夠操做數據的大小。(limit 後數據不能進行讀寫)

byteBuffer.limit()

position : 位置,表示緩衝區中正在操做數據的位置。

byteBuffer.position()

mark : 標記,表示記錄當前 position 的位置。能夠經過 reset() 恢復到 mark 的位置

byteBuffer.mark()

1.4 Buffer經常使用的方法

1.五、直接緩衝區與非直接緩衝區

非直接緩衝區:經過 allocate() 方法分配緩衝區,將緩衝區創建在 JVM 的內存中

直接緩衝區:經過 allocateDirect() 方法分配直接緩衝區,將緩衝區創建在物理內存中。能夠提升效率

二、代碼講解

2.1 使用緩衝區的各類方法

@Test
public void test1(){
   String str = "MuJiuTian";
   
   //1. 分配一個指定大小的Byte類型的緩衝區
   ByteBuffer buf = ByteBuffer.allocate(1024);
   
   System.out.println("-----------------allocate()----------------");
   //此時緩衝區爲讀,下標位置
   System.out.println(buf.position());
   //此時緩衝區的界線,也就是臨界點,1024
   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. 切換讀取數據模式,切換爲寫的模式,也就是把剛剛讀取的內容從新從下標0開始讀
   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());
}

結果:

看剛剛的流程圖:

2.2 使用簡單方法掌握buffer的基本方法

@Test
public void test2(){

   String str = "Mujiutian";

   //建立Byte類型緩衝區
   ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
   //此時爲讀模式,讀取str內容
   byteBuffer.put(str.getBytes());
   //切換爲寫的模式,處理剛剛讀取的內容
   byteBuffer.flip();

   //此時limit爲9,由於讀取了Mujiutian 9個字節
   byte[] dst = new byte[byteBuffer.limit()];
   //讀取下標0到2的字節
   byteBuffer.get(dst, 0, 2);
   System.out.println(new String(dst, 0, 2));
   System.out.println(byteBuffer.position());
   
   //mark() : 標記
   byteBuffer.mark();

   byteBuffer.get(dst, 2, 2);
   System.out.println(new String(dst, 2, 2));
   System.out.println(byteBuffer.position());
   
   //reset() : 使用該方法,位置恢復到 mark 的位置
   byteBuffer.reset();
   System.out.println(byteBuffer.position());
   
   //判斷緩衝區中是否還有剩餘數據
   if(byteBuffer.hasRemaining()){
      //獲取緩衝區中能夠操做的數量
      System.out.println(byteBuffer.remaining());
   }
}

結果爲:

2.3 測試是否爲緩衝區

@Test
public void test3(){
   //分配直接緩衝區
   ByteBuffer buf = ByteBuffer.allocateDirect(1024);
   System.out.println(buf.isDirect());
}

結果:true

相關文章
相關標籤/搜索