中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java.nio.channels.Pipe使用例子

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

Java.nio.channels.Pipe 提供一種機制,可以按照寫入的順序讀取數(shù)據(jù)。 Pipe.SinkChannel用來向管道寫入數(shù)據(jù),而Pipe.SourceChannel用來從管道讀取數(shù)據(jù):

PipeTest.java

package cn.outofmemory.nio.channels;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

public class PipeTest {
  public static void main(String[] args) throws IOException {

      //初始化Pipe實例
      Pipe pipe = Pipe.open();

      // 獲取寫通道
      Pipe.SinkChannel skChannel = pipe.sink();

      String testData = "Test Data to Check java NIO Channels Pipe.";

      ByteBuffer buffer = ByteBuffer.allocate(512);
      buffer.clear();
      buffer.put(testData.getBytes());

      buffer.flip();
       //向?qū)懲ǖ缹懭霐?shù)據(jù)
      while(buffer.hasRemaining()) {
          skChannel.write(buffer);
      }
      //獲得讀取數(shù)據(jù)通道
      Pipe.SourceChannel sourceChannel = pipe.source();
      buffer = ByteBuffer.allocate(512);

     //將讀取數(shù)據(jù)寫到控制臺     
      while(sourceChannel.read(buffer) > 0){

          //flip方法將當前讀取位置設置為0, limit設置為寫入數(shù)據(jù)的size
         buffer.flip();

          while(buffer.hasRemaining()){
              char ch = (char) buffer.get();
              System.out.print(ch);
          }

          //clear方法將buffer的limit設置為其容量capacity, 將position設置為0
          buffer.clear();
      }

  }
}

輸出如下:

Test Data to Check java NIO Channels Pipe.

標簽:

版權申明:本站文章部分自網(wǎng)絡,如有侵權,請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Velocity 使用 java.util.Properties 來配置 Velocity

下一篇:java Velocity 同時初始化兩個 Engine 實例