<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                <!-- View Buffers --> ## 視圖緩沖區 “視圖緩沖區”(view buffer)是通過特定的基本類型的窗口來查看底層 **ByteBuffer**。**ByteBuffer** 仍然是“支持”視圖的實際存儲,因此對視圖所做的任何更改都反映在對 **ByteBuffer** 中的數據的修改中。 如前面的示例所示,這方便地將基本類型插入 **ByteBuffer**。視圖緩沖區還可以從 **ByteBuffer** 讀取基本類型數據,每次單個(**ByteBuffer** 規定),或者批量讀取到數組。下面是一個通過 **IntBuffer** 在 **ByteBuffer** 中操作 **int** 的例子: ```java // newio/IntBufferDemo.java // (c)2017 MindView LLC: see Copyright.txt // 我們無法保證該代碼是否適用于其他用途。 // 訪問 http://OnJava8.com 了解更多本書信息。 // 利用 IntBuffer 保存 int 數據到 ByteBuffer import java.nio.*; public class IntBufferDemo { private static final int BSIZE = 1024; public static void main(String[] args) { ByteBuffer bb = ByteBuffer.allocate(BSIZE); IntBuffer ib = bb.asIntBuffer(); // 保存 int 數組: ib.put(new int[]{ 11, 42, 47, 99, 143, 811, 1016 }); //絕對位置讀寫: System.out.println(ib.get(3)); ib.put(3, 1811); // 在重置緩沖區前設置新的限制 ib.flip(); while(ib.hasRemaining()) { int i = ib.get(); System.out.println(i); } } } ``` 輸出結果: ``` 99 11 42 47 1811 143 811 1016 ``` `put()` 方法重載,首先用于存儲 **int** 數組。下面的 `get()` 和 `put()` 方法調用直接訪問底層 **ByteBuffer** 中的 **int** 位置。**注意**,通過直接操作 **ByteBuffer** ,這些絕對位置訪問也可以用于基本類型。 一旦底層 **ByteBuffer** 通過視圖緩沖區填充了 **int** 或其他基本類型,那么就可以直接將該 **ByteBuffer** 寫入通道。你可以輕松地從通道讀取數據,并使用視圖緩沖區將所有內容轉換為特定的基本類型。下面是一個例子,通過在同一個 **ByteBuffer** 上生成不同的視圖緩沖區,將相同的字節序列解釋為 **short**、**int**、**float**、**long** 和 **double**。代碼示例: ```java // newio/ViewBuffers.java // (c)2017 MindView LLC: see Copyright.txt // 我們無法保證該代碼是否適用于其他用途。 // 訪問 http://OnJava8.com 了解更多本書信息。 import java.nio.*; public class ViewBuffers { public static void main(String[] args) { ByteBuffer bb = ByteBuffer.wrap( new byte[]{ 0, 0, 0, 0, 0, 0, 0, 'a' }); bb.rewind(); System.out.print("Byte Buffer "); while(bb.hasRemaining()) System.out.print( bb.position()+ " -> " + bb.get() + ", "); System.out.println(); CharBuffer cb = ((ByteBuffer)bb.rewind()).asCharBuffer(); System.out.print("Char Buffer "); while(cb.hasRemaining()) System.out.print( cb.position() + " -> " + cb.get() + ", "); System.out.println(); FloatBuffer fb = ((ByteBuffer)bb.rewind()).asFloatBuffer(); System.out.print("Float Buffer "); while(fb.hasRemaining()) System.out.print( fb.position()+ " -> " + fb.get() + ", "); System.out.println(); IntBuffer ib = ((ByteBuffer)bb.rewind()).asIntBuffer(); System.out.print("Int Buffer "); while(ib.hasRemaining()) System.out.print( ib.position()+ " -> " + ib.get() + ", "); System.out.println(); LongBuffer lb = ((ByteBuffer)bb.rewind()).asLongBuffer(); System.out.print("Long Buffer "); while(lb.hasRemaining()) System.out.print( lb.position()+ " -> " + lb.get() + ", "); System.out.println(); ShortBuffer sb = ((ByteBuffer)bb.rewind()).asShortBuffer(); System.out.print("Short Buffer "); while(sb.hasRemaining()) System.out.print( sb.position()+ " -> " + sb.get() + ", "); System.out.println(); DoubleBuffer db = ((ByteBuffer)bb.rewind()).asDoubleBuffer(); System.out.print("Double Buffer "); while(db.hasRemaining()) System.out.print( db.position()+ " -> " + db.get() + ", "); } } ``` 輸出結果: ``` Byte Buffer 0 -> 0, 1 -> 0, 2 -> 0, 3 -> 0, 4 -> 0, 5 -> 0, 6 -> 0, 7 -> 97, Char Buffer 0 -> NUL, 1 -> NUL, 2 -> NUL, 3 -> a, Float Buffer 0 -> 0.0, 1 -> 1.36E-43, Int Buffer 0 -> 0, 1 -> 97, Long Buffer 0 -> 97, Short Buffer 0 -> 0, 1 -> 0, 2 -> 0, 3 -> 97, Double Buffer 0 -> 4.8E-322, ``` **ByteBuffer** 通過“包裝”一個 8 字節數組生成,然后通過所有不同基本類型的視圖緩沖區顯示該數組。下圖顯示了從不同類型的緩沖區讀取數據時,數據顯示的差異: ![1554546258113](../images/1554546258113.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看