Java NIO之通道Channel

channel與流的區別:java

  • 流基於字節,且讀寫爲單向的。
  • 通道基於快Buffer,能夠異步讀寫。除了FileChannel以外都是雙向的。

 

channel的主要實現:服務器

  • FileChannel
  • DatagramChannel:UDP讀寫
  • SocketChannel:TCP讀寫
  • ServerSocketChannel

支持scatter/gather(分散和彙集)dom

        1. 分散(scatter)從Channel中讀取是指在讀操做時將讀取的數據寫入多個buffer中。所以,Channel將從Channel中讀取的數據「分散(scatter)」到多個Buffer中。
          彙集(gather)寫入Channel是指在寫操做時將多個buffer的數據寫入同一個Channel,所以,Channel 將多個Buffer中的數據「彙集(gather)」後發送到Channel。
ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body   = ByteBuffer.allocate(1024);
ByteBuffer[] bufferArray = { header, body };
channel.read(bufferArray);

通道的建立:異步

除了FileChannel以外,都使用open建立。socket

FileChannel的建立以下:spa

RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel inChannel = aFile.getChannel();

 

Socket通道.net

分別對應java.net包中的Socket對象ServerSocket、Socket和DatagramSocket;Socket通道被實例化時,都會建立一個對等的Socket對象。code

 

Socket通道能夠運行非阻塞模式而且是可選擇的,非阻塞I/O與可選擇性是緊密相連的,這也正是管理阻塞的API要在 SelectableChannel中定義的緣由。設置非阻塞很是簡單,只要調用configureBlocking(false)方法便可。若是須要中 途更改阻塞模式,那麼必須首先得到blockingLock()方法返回的對象的鎖。對象

 

ServerSocketChannel實例:blog

1. 使用對等socket來bind監聽。

2. 非阻塞狀態下,accept在無鏈接時當即返回null

ByteBuffer buffer = ByteBuffer.wrap("Hello World".getBytes());
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(12345));
ssc.configureBlocking(false);
for (;;) {
    System.out.println("Waiting for connections");
    SocketChannel sc = ssc.accept();
    if (sc == null)
       TimeUnit.SECONDS.sleep(2000);
   else {
        System.out.println("Incoming connection from:" + sc.socket().getRemoteSocketAddress());
        buffer.rewind();
        sc.write(buffer);
        sc.close();
    }
}

 

  • SocketChannel 
           相對於ServerSocketChannel,它扮演客戶端,發起到監聽服務器的鏈接,鏈接成功後,開始接收數據。 
           調用它的open()方法僅僅是打開但並未鏈接,要創建鏈接須要緊接着調用connect()方法;也能夠兩步合爲一步,調用open(SocketAddress remote)方法。 
           你會發現connect()方法並未提供timout參數,做爲替代方案,你能夠用isConnected()、isConnectPending()或finishConnect()方法來檢查鏈接狀態。
  • 1. isConnected判斷是否已經創建
  • 2. finishConnect在非阻塞套接字上阻塞等待鏈接成功

 

非阻塞的read在無數據時當即返回0

  •  
  • DatagramChannel         它是無鏈接的,它既能夠做爲服務器,也能夠做爲客戶端。
相關文章
相關標籤/搜索