<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java NIO – 分散/聚集或向量 IO > 原文: [https://howtodoinjava.com/java7/nio/java-nio-2-0-scatter-gather-or-vectored-io/](https://howtodoinjava.com/java7/nio/java-nio-2-0-scatter-gather-or-vectored-io/) [**通道**](//howtodoinjava.com/java-7/nio/java-nio-2-0-channels/ "Java NIO 2.0 : Channels")提供了一項重要的新特性,稱為**分散/聚集**(在某些圈子中稱為**向量 I/O**)。 分散/聚集是一個簡單而強大的概念。 分散/聚集是一種技術,通過該技術,可以通過一次`read()`調用將字節從流中讀取到一組緩沖區(向量)中,并且可以通過一次`write()`調用,將字節從一組緩沖區中寫入到流中。大多數現代操作系統都支持本機向量 I/O。 當您請求通道上的分散/聚集操作時,該請求將轉換為適當的本機調用以直接填充或耗盡緩沖區。 這是一個巨大的勝利,因為減少了或消除了緩沖區副本和系統調用。 直到最近,Java 還沒有執行向量 I/O 操作的能力。 因此,我們習慣于將字節直接讀取到單個字節數組中,或者進行多次讀取以獲取數據。 ## 分散/聚集 API 從通道讀取的散射是將數據讀取到多個緩沖區中的讀取操作。因此,通道將來自通道的數據分散到多個緩沖區中。對通道的聚集寫入是一種將來自多個緩沖區的數據寫入單個通道的寫入操作。因此,通道將來自多個緩沖器的數據聚集到一個通道中。在需要分別處理傳輸數據的各個部分的情況下,分散/聚集可能非常有用。 ```java public interface ScatteringByteChannel extends ReadableByteChannel { public long read (ByteBuffer [] dsts) throws IOException; public long read (ByteBuffer [] dsts, int offset, int length) throws IOException; } public interface GatheringByteChannel extends WritableByteChannel { public long write(ByteBuffer[] srcs) throws IOException; public long write(ByteBuffer[] srcs, int offset, int length) throws IOException; } ``` 您可以看到每個接口都添加了兩個新方法,這些新方法將緩沖區數組作為參數。 現在,讓我們寫一個簡單的例子來了解如何使用此特性。 ## 分散/聚集 IO 示例 在此示例中,我創建了兩個緩沖區。 一個緩沖區將存儲一個隨機數,另一個緩沖區將存儲一個隨機字符串。 我將使用`GatheringByteChannel`讀寫文件通道中兩個緩沖區中存儲的數據。 然后,我將使用`ScatteringByteChannel`將文件中的數據讀回到兩個單獨的緩沖區中,并在控制臺中打印內容以驗證存儲和檢索的數據是否匹配。 ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; public class ScatteringAndGatheringIOExample { public static void main(String params[]) { String data = "Scattering and Gathering example shown in howtodoinjava.com"; gatherBytes(data); scatterBytes(); } /* * gatherBytes() reads bytes from different buffers and writes to file * channel. Note that it uses a single write for both the buffers. */ public static void gatherBytes(String data) { //First Buffer holds a random number ByteBuffer bufferOne = ByteBuffer.allocate(4); //Second Buffer holds data we want to write ByteBuffer buffer2 = ByteBuffer.allocate(200); //Writing Data sets to Buffer bufferOne.asIntBuffer().put(13); buffer2.asCharBuffer().put(data); //Calls FileOutputStream(file).getChannel() GatheringByteChannel gatherer = createChannelInstance("test.txt", true); //Write data to file try { gatherer.write(new ByteBuffer[] { bufferOne, buffer2 }); } catch (Exception e) { e.printStackTrace(); } } /* * scatterBytes() read bytes from a file channel into a set of buffers. Note that * it uses a single read for both the buffers. */ public static void scatterBytes() { //First Buffer holds a random number ByteBuffer bufferOne = ByteBuffer.allocate(4); //Second Buffer holds data we want to write ByteBuffer bufferTwo = ByteBuffer.allocate(200); //Calls FileInputStream(file).getChannel() ScatteringByteChannel scatterer = createChannelInstance("test.txt", false); try { //Reading from the channel scatterer.read(new ByteBuffer[] { bufferOne, bufferTwo }); } catch (Exception e) { e.printStackTrace(); } //Read the buffers seperately bufferOne.rewind(); bufferTwo.rewind(); int bufferOneContent = bufferOne.asIntBuffer().get(); String bufferTwoContent = bufferTwo.asCharBuffer().toString(); //Verify the content System.out.println(bufferOneContent); System.out.println(bufferTwoContent); } public static FileChannel createChannelInstance(String file, boolean isOutput) { FileChannel fc = null; try { if (isOutput) { fc = new FileOutputStream(file).getChannel(); } else { fc = new FileInputStream(file).getChannel(); } } catch (Exception e) { e.printStackTrace(); } return fc; } } ``` ## 總結 正確使用分散/聚集工具可能會非常強大。 它使您可以將將讀取的數據分離到多個存儲桶中或將不同的數據塊組裝成一個整體的繁瑣工作委托給操作系統。 這是一個巨大的勝利,因為**操作系統已針對此類事物**進行了高度優化。 它節省了您四處移動的工作,從而避免了**緩沖區復制,并減少了編寫和調試**所需的代碼量。 由于基本上是通過提供對數據容器的引用來組裝數據,因此可以通過以不同組合構建多個緩沖區引用數組來以不同的方式來組裝各種塊。 **祝您學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看