天天看點

Java NIO?看這一篇就夠了!1.概述2.FileChannel3.SocketChannel4.TCP服務端的NIO寫法5.記憶體映射檔案6.其餘功能介紹

現在使用NIO的場景越來越多,很多網上的技術架構或多或少的使用NIO技術,譬如TOMCAT、JETTY。學習和掌握NIO技術已經不是一個JAVA工程師的加分技能,而是一個必備技能。在前面兩篇文章《什麼是Zero-Copy》和《NIO相關基礎篇》中我們學習了NIO的相關理論知識,而在本篇中我們一起來學習一下Java NIO的實踐知識。

1.概述

NIO主要有三大核心部分:Channel(通道),Buffer(緩沖),Selector(選擇區)。傳統IO基于位元組流進行操作,而NIO基于Channel和Buffer進行操作,資料總是從Channel(通道)讀取到Buffer(緩沖區),或者從緩沖區寫入到通道中。Selector(選擇區)用于監聽多個通道的事件(比如:打開連接配接,資料到達)。是以,單個線程可以監聽多個資料通道。

NIO和傳統IO(以下簡稱IO)之間第一個最大的差別是,IO是面向流的,NIO是面向緩沖區的。Java IO面向流意味着每次從流中讀一個或多個位元組,直至讀取所有位元組,它們沒有被緩存在任何地方。此外,它不能前後移動流中的資料。如果需要前後移動流中的資料,需要先将它們緩存到一個緩沖區。NIO的緩沖流導向方法略有不同。資料讀取到一個它稍後處理的緩沖區,需要時可在緩沖區前後移動。這就增加了處理過程中的靈活性。但是,還需要檢查是否該緩沖區中包含所有您需要處理的資料。而且,需確定當更多的資料讀入緩沖區時,不要覆寫緩沖區裡尚未處理的資料。

IO的各種流都是阻塞的。這意味着,當一個線程調用 read() 或 write() 時,該線程被組撒,直到有一些資料被讀取,或者資料完全寫入。該線程在此期間不能再幹任何事情了。NIO的非阻塞模式,使一個線程從某通道請求讀取資料,但是它僅能得到目前可用的資料,如果目前沒有資料可用時,就什麼都不會擷取。而不是保持線程阻塞,是以直至資料變得可讀之前,該線程可以繼續做其他的事情。非阻塞寫也是如此。一個線程請求寫入一些資料到通道,但不需要等它完全寫入,這個線程同時可以去做别的事情。線程通常将非租塞IO的空閑時間用于在其他通道上執行IO操作,是以一個單獨的線程現在可以管理多個輸入和輸出通道(channel)。

1.1 Channel

首先說下Channel。Channel和IO中的Stream(流)是差不多一個等級的。隻不過Stream是單向的,譬如:InputStream、OutputStream。而Channel是雙向的,既可以用來進行讀操作,又可以用來進行寫操作。

NIO中的Channel的主要實作有:

  • FileChannel
  • DatagramChannel
  • SocketChannel
  • ServerSocketChannel

這裡看名字就可以猜出來:分别可以對應檔案IO、UDP、TCP(Client和Server)。下面示範的案例基本上就是圍繞這4個類型的Channel進行陳述的。

1.2 Buffer

NIO 中的關鍵Buffer實作有:ByteBuffer、CharBuffer、DoubleBuffer、FloatBuffer、IntBuffer、LongBuffer、ShortBuffer,分别對應基本資料類型:byte、char、double、float、int、long、short。當然NIO中還有 MappedByteBuffer、HeapByteBuffer、DirectByteBuffer等這裡先不進行陳述。

1.3 Selector

Selector 運作單線程處理多個Channel,如果你的應用打開了多個通道,但每個連接配接的流量都很低,使用Selector就會很友善。例如在一個聊天伺服器中。要使用Selector,得向Selector注冊Channel,然後調用它的select()方法。這個方法會一直阻塞到某個注冊的通道有事件就緒。一旦這個方法傳回,線程就可以處理這些事情,事件的例子有如新的連接配接進來、資料接收等。

2.FileChannel

看完上面的陳述,對于第一次接觸NIO的同學來說雲裡霧裡,隻說了一些概念,也沒記住什麼,更别說怎麼用了。這裡開始通過傳統IO以及更改後的NIO來做對比,以更形象地突出NIO的用法,進而使你對NIO有一點點了解。

2.1傳統IO VS NIO

首先,案例1是采用FileInputStream讀取檔案内容的:

private static void method2() {
        InputStream in = null;
        try{
            in = new BufferedInputStream(new FileInputStream("nomal_io.txt"));
            byte[] buf = new byte[1024];
            int bytesRead = -1;
            while ((bytesRead = in.read(buf)) != -1) {
                for(int i=0;i<bytesRead;i++) {
                    System.out.print((char)buf[i]);
                }
            }
        } catch (IOException e){
            e.printStackTrace();
        } finally {
            try{
                if (in!=null){
                    in.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
           

輸出結果:(略)

案例對應的NIO(這裡通過RandomAccessFile進行操作,當然也可以通過FileInputStream.getChannel()進行操作):

private static void method1() {
        RandomAccessFile aFile = null;
        try {
            aFile = new RandomAccessFile("nio.txt","rw");
            FileChannel fileChannel = aFile.getChannel();
            ByteBuffer buf = ByteBuffer.allocate(1024);
            int bytesRead = -1;
            while ( (bytesRead = fileChannel.read(buf)) != -1) {
                 buf.flip();
                 while (buf.hasRemaining()) {
                     System.out.print((char)buf.get());
                 }
                 buf.compact();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try{
                if (aFile!=null){
                    aFile.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
           

 輸出結果:(略)

通過仔細對比案例1 和案例2,應該能看出個大概,最起碼能發現NIO的實作方式比較複雜。有了一個大概的印象就可以進行下一步了。

2.2Buffer 的使用

從案例2中可以總結出使用Buffer一般遵循下面一個步驟:

  • 配置設定空間(ByteBuffer buf = ByteBuffer.allocate(1024);還有一種allocateDirector後面在描述)
  • 讀取資料到Buffer(bytesRead = fileChannel.read(buf) )
  • 調用 flip() 方法(buf.flip())
  • 從 Buffer 中讀取資料( System.out.print((char)buf.get()); )
  • 調用clear() 或 compact() 方法

Buffer顧名思義:緩沖區,實際上是一個容器,一個連續數組。從Channel提供檔案、網絡讀取資料的管道,但是讀寫的資料都必須經過Buffer。如下圖:

Java NIO?看這一篇就夠了!1.概述2.FileChannel3.SocketChannel4.TCP服務端的NIO寫法5.記憶體映射檔案6.其餘功能介紹

 向Buffer中寫資料:

  • 從Channel寫到Buffer ( fileChannel.read(bug) )
  • 通過 Buffer的 put()方法( buf.put(...) )

從Buffer中讀取資料:

  • 從 Buffer 讀取到 Channel ( channel.write( buf ) )
  • 使用 get() 方法 從 Buffer中讀取資料 (  buf.get()  )

可以把Buffer簡單地了解為一組基本資料類型的元素清單,它通過幾個變量來儲存這個資料的目前位置狀态:capacity、position、limit、mark。

  • capacity:緩沖區數組的總長度。
  • position:下一個要操作的資料元素的位置。
  • limit:緩沖區數組中不可操作的下一個元素的位置(limit < capacity )
  • mark:用于記錄目前 position 的前一個位置或者預設是-1
Java NIO?看這一篇就夠了!1.概述2.FileChannel3.SocketChannel4.TCP服務端的NIO寫法5.記憶體映射檔案6.其餘功能介紹

無圖無真相,舉例:我們通過ByteBuffer.allocate(11)方法建立了一個11byte的數組的緩沖區,初始狀态如上圖,position的位置為0,capacity和limit預設都是數組長度。當我們寫入5個位元組時,變化如下圖:

Java NIO?看這一篇就夠了!1.概述2.FileChannel3.SocketChannel4.TCP服務端的NIO寫法5.記憶體映射檔案6.其餘功能介紹

 這是我們需要将緩沖區中的5個位元組資料寫入Channel的通信信道,是以我們調用ByteBuffer.flip()方法,變化如下圖所示(position設回0,并将limit設成之前的position的值)

Java NIO?看這一篇就夠了!1.概述2.FileChannel3.SocketChannel4.TCP服務端的NIO寫法5.記憶體映射檔案6.其餘功能介紹

這時底層作業系統就可以從緩沖區中正确讀取5個位元組資料并發送出去了。在下一次寫資料之前,我們需要再調用clear()方法,緩沖區的索引位置又回到的了初始位置。

調用clear()方法: position 将被設回0,limit設定成capacity,換句話說,Buffer被清空了,其實Buffer中的資料并未被清楚,隻是這些标記告訴我們從哪裡開始往Buffer裡寫資料。如果Buffer中有一些未讀的資料,調用clear()方法,資料将被遺忘,意味着不再有任何标記會告訴你哪些資料被讀過,哪些還沒有。如果Buffer中仍有未被讀的資料,且後續還要處理這些資料,但是此時想要先寫些資料,那麼使用compact()方法。compact()方法将所有未讀的資料拷貝到Buffer的起始處。然後position設到最後一個未讀元素的後面。limit屬性依然像clear()方法一樣,設定成capacity。現在Buffer準備好寫資料了,但是不會覆寫未讀的資料。

通過調用 Buffer.mark() 方法,可以标記Buffer中的一個特定的position,之後可以通過調用Buffer.reset() 方法恢複到這個這個position。Buffer.rewind()方法将position設回0,是以你可以重讀Buffer中的所有資料。limit保持不變,仍然表示能從Buffer中讀取多少個資料。

3.SocketChannel

說完了FileChannel和Buffer,大家應該對Buffer的用法比較了解了。這裡使用SocketChannel來繼續探讨NIO。NIO的強大功能部分來自于Channel的非阻塞特性,套接字的某系操作可能會無限地阻塞。例如,對accept()方法的調用可能會因為等待一個用戶端的連接配接而阻塞;對read()方法調用可能會因為沒有資料可讀而阻塞,直到連接配接的另一端傳來新的資料。總的來說,建立/接收或讀寫資料等IO調用,都可能無限地阻塞等待,直到底層的網絡實作發生了什麼。慢速的,有損耗的網絡,或者僅僅是簡單的網絡故障都可能導緻任意時間的延遲。然後不幸的是,在調用一個方法之前無法知道其是否是阻塞。NIO的Channel抽象的一個重要特征就是可以通過配置它的阻塞行為,以實作非阻塞式的信道。

channel.configureBlocking(false);
           

在非阻塞式信道單調用一個方法總是會立即傳回。這種調用的傳回值訓示了所請求的操作完成的程度。例如,在一個非阻塞式的ServerSocketChannel上調用accept()方法,如果有連接配接請求來了,則會傳回用戶端的SocketChannel,傳回傳回null。

 用戶端代碼(案例3):

private static void client() {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        SocketChannel socketChannel = null;
        try {
            socketChannel = SocketChannel.open();
            socketChannel.configureBlocking(false);
            socketChannel.connect(new InetSocketAddress("127.0.0.1",8080));
            if (socketChannel.finishConnect()) {
                int i=0;
                while (true) {
                    TimeUnit.SECONDS.sleep(1);
                    String info = "我是第" + i++ + "個來自用戶端的資訊";
                    buffer.clear();
                    buffer.put(info.getBytes());
                    buffer.flip();
                    while (buffer.hasRemaining()) {
                        System.out.println(buffer);
                        socketChannel.write(buffer);
                    }
                }
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            try {
                if (socketChannel != null) {
                    socketChannel.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
           

服務端代碼(案例4):

private static void server() {
        ServerSocket serverSocket = null;
        InputStream in = null;
        try {
            serverSocket = new ServerSocket(8080);
            int recvMsgSize = 0;
            byte[] recvBuf = new byte[1024];
            while (true) {
                Socket client = serverSocket.accept();
                System.out.println("處理來自" + client.getRemoteSocketAddress() + "用戶端");
                in = client.getInputStream();
                while ((recvMsgSize = in.read(recvBuf)) != -1) {
                    byte[] temp = new byte[recvMsgSize];
                    System.arraycopy(recvBuf, 0, temp,0,recvMsgSize);
                    System.out.println(new String(temp));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (serverSocket!=null) {
                    serverSocket.close();
                }
                if (in!=null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
           

輸出結果:(略)

根據案例分析,總結一下SocketChannel的用法。

打開SocketChannel:

socketChannel = SocketChannel.open();
            socketChannel.connect(new InetSocketAddress("127.0.0.1",8080));
           

關閉:

socketChannel.close();
           

 讀取資料:

String info = "我是第" + i++ + "個來自用戶端的資訊";
                    buffer.clear();
                    buffer.put(info.getBytes());
                    buffer.flip();
                    while (buffer.hasRemaining()) {
                        System.out.println(buffer);
                        socketChannel.write(buffer);
                    }
           

注意SocketChannel.write()方法的調用是在一個while循環中。write方法無法保證能寫多少位元組到SocketChannel。是以,我們重複調用write()直到Buffer沒有要寫的位元組為止。

非阻塞模式下,read()方法在尚未讀取到任何資料時可能就傳回了。是以需要關注它的int傳回值,它會告訴你讀取了多少位元組。 

4.TCP服務端的NIO寫法

到目前為止,所舉的案例中都沒涉及到Selector。不要急,好東西要慢慢來。Selector類可以用于避免使用阻塞模式用戶端中很浪費資源的“忙等”方法。例如,考慮一個IM伺服器。像QQ或者旺旺這樣的,可能有幾萬甚至幾千萬個用戶端同時連接配接到了伺服器,但在任何時刻都隻是非常少量的消息。

需要讀取和分發。這就需要一種方法阻塞等待,直到有一個信道可以進行I/O操作,并指出是哪個通道。NIO的選擇器就實作了這樣的功能。一個Selector執行個體可以同時檢查一組信道的IO狀态。用專業術語來說,選擇器就是一個多路開關選擇器,因為一個選擇器能夠管理多個信道上的IO操作。然後如果用傳統的方式來處理這麼多用戶端,使用的方法是循環地一個個地去檢查所有用戶端是否有IO操作,如果目前用戶端有IO操作,則可能把目前用戶端扔給一個線程池去處理,如果沒有IO操作則進行下一個輪詢,當所有的用戶端都輪詢過了又接着從頭開始輪詢;這種方法是非常笨而且浪費資源,因為大部分用戶端是沒有IO操作,我們也要去檢查;而selector就不一樣了,它在内部可以同時管理多個IO,當一個信道有IO操作的時候,它會通知Selector,Selector就是記住這個信道有IO操作,并且知道是何種IO操作,是讀呢?是寫呢?還是接受新的連接配接;是以如果使用Selector,它傳回的結果隻有兩種結果,一種是0,即你在調用的時刻沒有任何用戶端IO操作,另一種結果是一組需要IO操作的用戶端,這時你就根本不需要再檢查了,因為它傳回給你的肯定是你想要的。這樣一種通知的方式比那種主動輪詢的方式要高效地多。

要适應選擇器Selector,需要建立一個Selector執行個體(使用靜态工廠方法open())并将其注冊(register)到想要監控的信道上(注意,這要通過channel的方法實作,而不是使用selector的方法)。最後,調用選擇器的select()方法。該方法會阻塞等待,直到有一個或更多的信道準備好了IO操作或者等待逾時。select()方法将傳回可進行IO操作的信道數量。現在在一個單獨的線程中,通過調用select()方法就能檢查多個信道是否準備好進行IO操作。如果經過一段時間後仍然沒有信道準備好,select()方法就會傳回0,并允許程式繼續執行其他任務。

下面将上面的TCP服務端代碼改寫成NIO方式(案例5):

public class ServerConnect {
    private static final int BUF_SIZE = 1024;
    private static final int PORT = 8080;
    private static final int TIMEOUT = 3000;

    public static void main(String[] args) {
        selector();
    }

    private static void selector() {
        Selector selector = null;
        ServerSocketChannel ssc = null;
        try {
            selector = Selector.open();
            ssc = ServerSocketChannel.open();
            ssc.socket().bind(new InetSocketAddress(PORT));
            ssc.configureBlocking(false);
            ssc.register(selector, SelectionKey.OP_ACCEPT);
            while (true) {
                if (selector.select(TIMEOUT) == 0) {
                    System.out.println("==");
                    continue;
                }
                Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
                while (iter.hasNext()) {
                    SelectionKey key = iter.next();
                    //服務端信道是否有連接配接進來
                    if (key.isAcceptable()) {
                        handleAccept(key);
                    }
                    if (key.isReadable()) {
                        handleRead(key);
                    }
                    if (key.isWritable() && key.isValid()){
                        handleWrite(key);
                    }
                    if (key.isConnectable()) {
                        System.out.println("isConnectable = true");
                    }
                    iter.remove();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (selector != null) {
                    selector.close();
                }
                if (ssc != null) {
                    ssc.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void handleAccept(SelectionKey key) throws IOException {
        ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
        SocketChannel sc = ssc.accept();
        sc.configureBlocking(false);
        sc.register(key.selector(), SelectionKey.OP_READ, ByteBuffer.allocateDirect(BUF_SIZE));
    }

    private static void handleRead(SelectionKey key) throws IOException {
        SocketChannel sc = (SocketChannel) key.channel();
        ByteBuffer buf = (ByteBuffer)key.attachment();
        int read = -1;
        while ((read = sc.read(buf)) != -1) {
            buf.flip();
            while (buf.hasRemaining()) {
                System.out.print((char)buf.get());
            }
        }
        sc.close();
    }

    private static void handleWrite(SelectionKey key) throws IOException {
        ByteBuffer buf = (ByteBuffer) key.attachment();
        buf.flip();
        SocketChannel sc = (SocketChannel) key.channel();
        while (buf.hasRemaining()) {
            sc.write(buf);
        }
        buf.compact();
    }
}
           

下面來慢慢講解這段代碼。

4.1 ServerSocketChannel

打開ServerSocketChannel:

ServerSocketChannel serverSocketChannel= ServerSocketChannel.open();
           

關閉ServerSocketChannel:

serverSocketChannel.close();
           

監聽新進來的連接配接:

while(true){
    SocketChannel socketChannel = serverSocketChannel.accept();
}
           

ServerSocketChannel可以設定成非阻塞模式。在非阻塞模式下,accept()方法會立即傳回,如果還沒有新進來的連接配接,傳回的将是null。

是以,需要檢查傳回的SocketChannel是否是null。如:

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(9999));
        serverSocketChannel.configureBlocking(false);
        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            if (socketChannel !=null) {
                //do something with the socketChannel...
            }
        }
           

4.2 Selector

Selector的建立:

Selector selector = Selector.open();
           

為了将Channel和Selector配合使用,必須将Channel注冊到Selector上,通過SelectableChannel.register()方法來實作,沿用案例五種的部分代碼:

ssc = ServerSocketChannel.open();
            ssc.socket().bind(new InetSocketAddress(PORT));
            ssc.configureBlocking(false);
            ssc.register(selector, SelectionKey.OP_ACCEPT);
           

與Selector一起使用時,Channel必須處于非阻塞模式下。這意味着不能将FileChannel與Selector一起使用,因為FileChannel不能切換到非阻塞模式。而套接字通道可以。

注意register()方法的第二個參數。這是一個“interest集合”,意思是在通過Selector監聽Channel時對什麼事件感興趣。可以監聽四種不同的類型:

  • Connect
  • Accept
  • Read
  • Write

通道觸發了一個事件意思是該事件已經就緒。是以,某個Channel成功連接配接到另一個伺服器稱為“連接配接就緒”。一個Server Socket Channel 準備好接受新進入的連接配接稱為“接受就緒”。一個資料可讀的通道可以說是“讀就緒”。等待寫資料的通道可以說是“寫就緒”。

這四種事件用 SelectionKey的四個常量來表示:

  • SelectionKey.OP_CONNECT
  • SelectionKey.OP_ACCEPT
  • SelectionKey.OP_READ
  • SelectionKey.OP_WRITE

4.3 SelectionKey

當向Selector注冊Channel時,register()方法會傳回一個SelectionKey對象。這個對象包含了一些你感興趣的屬性:

  • interest集合
  • ready集合
  • Channel
  • Selector
  • 附加的對象(可選)

interest集合:就像向Selector注冊通道一節中所描述的,interest集合是你所選擇感興趣的事件集合。可以通過SelectionKey讀寫interest集合。

ready集合是通道一節準備就緒的操作的集合。在一次選擇(Selection)之後,你會首先通路這個 ready set。Selection将在下一小節進行解釋。可以這樣通路ready集合:

int readySet = selectionKey.readyOps();
           

可以用像檢測interest集合那樣的方法,來檢查Channel中什麼事件或操作已經就緒。但是,也可以使用以下四個方法,他們都會傳回一個布爾類型:

selectionKey.isAcceptable();
selectionKey.isConnectable();
selectionKey.isReadable();
selectionKey.isWriteable();

           

可以将一個對象或者更多資訊附着到SelectionKey上,這樣就能友善的識别某個給定的通道。例如,可以附加與通道一起使用的Buffer,或者是包含聚集資料的某個對象。使用方法如下:

selectionKey.attach(thObj);
Object obj = selectionKey.attachment();
           

 還可以使用register()方法向Selector注冊Channel的時候附加對象。如:

SelectionKey key = channel.register(selector,SelectionKey.OP_READ, theObj);
           

4.4 通過Selector選擇通道

一旦向Selector注冊了一個或多個通道,就可以調用幾個重載的select()方法。這些方法傳回你所感興趣的事件(如連接配接、接收、讀或寫),已經準備就緒的那些通道。換句話說,如果你對“讀就緒”的通道感興趣,select()方法會傳回讀事件已經就緒的那些通道。

下面是 select() 方法:

  • int select()
  • int select(long timeout)
  • int selectNow()

select()阻塞到至少有一個通道在你注冊的事件上就緒了。

select(long timeout)和select()一樣,除了最長會阻塞timeout(毫秒)。

selectNow()不會阻塞,不管什麼通道就緒都立刻傳回(注:此方法執行非阻塞選擇操作。如果自從前一次選擇操作後,沒有通道變成可選,則此方法傳回0)。

select()方法傳回的int值表示有多少通道已經就緒。即,自上次調用select()方法後有多少通道變成了就緒狀态。如果調用select()方法,因為有一個通道變成就緒狀态,傳回了1,若再次調用select()方法,如果另一個通道就緒了,它會再次傳回1。如果對第一個就緒的channel沒有做任何操作,現在就有兩個通道。

一旦調用了select()方法,并且傳回值表明有一個或更多個就緒了,然後通過調用selector的selectedKeys()方法,通路“已選中鍵集(selected key set)”中的就緒通道。如下所示:

Set selectedKeys = selector.selectedKeys();
           

當向Selector注冊Channel時,Channel.register()方法會傳回一個SelectionKey對象。這個對象代表了注冊到該Selector的通道。

注意每次疊代末尾的keyIterator.remove()調用。Selector不會自己從已選擇鍵集中移除SelectionKey執行個體。必須在處理完通道時自己移除。下次該通道變成就緒時,Selector會再次将其放入已選擇鍵集中。

 SelectionKey.channel()方法傳回的通道需要轉型成你要處理的類型,如ServerSocketChannel或SocketChannel等。

一個完整的使用Selector和ServerSocketChannel的案例可以參考案例5的selector()方法。

5.記憶體映射檔案

Java處理大檔案,一般使用BufferReader、BufferInputStream這類帶緩沖的IO類,不過檔案如果超大的話,更快的方式是采用MappedByteBuffer。

MappedByteBuffer是NIO引入的檔案記憶體映射方案,讀寫性能極高。NIO最主要的就是實作了對異步操作的支援。其中一種是通過把一個套接字通道(SocketChannel)注冊到一個選擇器(Selector)中,不時調用後者的選擇(select)方法就能傳回滿足的選擇鍵(SelectionKey),鍵中包含了SOCKET事件資訊。這就是select模型。

SocketChannel的讀寫是通過一個ByteBuffer來操作的。這個類本身的設計是不錯的,比直接操作byte[]友善多了。ByteBuffer有兩種模式:直接/間接。

間接模式最典型的就是HeapByteBuffer,即操作對記憶體(byte[])。但畢竟記憶體有限,如果要發送一個1G的檔案怎麼辦?不可能真的去配置設定1G的記憶體。這時就必須使用“直接”模式,及MappedByteBuffer,檔案映射。

先終端一下,談談作業系統的記憶體管理。一般作業系統的記憶體分兩部分:實體記憶體、虛拟記憶體。

虛拟記憶體一般使用的是頁面映像檔案,即硬碟中的某個特殊的檔案。作業系統負責頁面檔案内容的讀寫,這個過程叫“頁面中斷/切換”。MappedByteBuffer也是類似的,你可以把整個檔案(不管檔案多大)看出是一個ByteBuffer。MappedByteBuffer隻是一種特殊的ByteBuffer,即ByteBuffer的子類,如果檔案比較大的話可以分段進行映射,隻要指定檔案的那個部分就可以。

5.1 概念

FileChannel提供了map方法來把檔案映射為記憶體映像檔案:

MappedByteBuffer map(int mode,long position,long size);
           

可以把檔案的從 position 開始的 size 大小的區域映射為記憶體映像檔案。

mode 指出了可通路該記憶體映像檔案的方式:

  • READ_ONLY(隻讀):試圖修改得到的緩沖區将導緻抛出 ReadOnlyBufferException。
  • READ_WRITE(讀/寫):對得到的緩沖的更改最終将傳播到檔案;該更改對映射到同一檔案的其他程式是不可見的。
  • PRIVATE(專用):對得到的緩沖區的更改不會傳播到檔案,并且該更改對映射到同一檔案的其他程式也是不可見的;相反,會建立已修改部分的專用副本的緩沖區。

MappedByteBuffer是ByteBuffer的子類,其擴充了三個方法:

  • force():緩沖區是READ_WRITE模式下,此方法對緩沖區内容的修改強行寫入檔案。
  • load():将緩沖區的内容載入記憶體,并傳回該緩沖區的引用。
  • isLoaded():如果緩沖區的記憶體在實體記憶體中,則傳回真,否則傳回假。

5.2 案例對比

這裡通過采用ByteBuffer和MappedByteBuffer分别讀取大小約為9M的檔案來比較兩者之間的差別,method3()采用MappedByteBuffer讀取,method4()采用ByteBuffer讀取。

private static void method3() {
        RandomAccessFile file = null;
        FileChannel f = null;
        try {
            file = new RandomAccessFile("E:/test.zip","rw");
            f = file.getChannel();
            long begin = System.currentTimeMillis();
            MappedByteBuffer buff = f.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            long end = System.currentTimeMillis();
            System.out.println("read time:" + (end-begin) + " ms");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (file!=null){
                    file.close();
                }
                if (f != null) {
                    f.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void method4() {
        RandomAccessFile file = null;
        FileChannel f = null;
        try {
            file = new RandomAccessFile("E:/test.zip","rw");
            f = file.getChannel();
            long begin = System.currentTimeMillis();
            ByteBuffer buff = ByteBuffer.allocate((int) file.length());
            buff.clear();
            f.read(buff);
            long end = System.currentTimeMillis();
            System.out.println("read time:" + (end-begin) + " ms");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (file!=null){
                    file.close();
                }
                if (f != null) {
                    f.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
           

通過在入口函數main()中運作:

method3();
        System.out.println("==================");
        method4();
           

輸出結果:

read time:2 ms

==================

read time:8 ms

通過例子可以看出彼此的差别,一個例子也許是偶然,那麼下面把9M大小的檔案替換為600M檔案,輸出結果:

read time:2 ms

==================

read time:658 ms

可以看到差距拉大。

注:MappedByteBuffer有資源釋放的問題:被MappedByteBuffer打開的檔案隻有在垃圾收集時才會被關閉,而這個點是不正确的。在Javadoc中這裡描述:A mapped byte buffer and the file mapping that it represents remian valid until the buffer itself is garbage-collected。

6.其餘功能介紹

看完以上陳述,大家對NIO有了一定的了解,下面主要通過幾個案例,來說明NIO的其餘功能,下面代碼偏多,功能性講述少。

6.1 Scatter/Gather

分散(Scatter)從Channel中讀取是指在讀操作時将讀取的資料寫入多個buffer中。是以,Channel将從Channel中讀取的資料“分散(Scatter)”到多個Buffer中。

聚集(Gather)寫入Channel是指在寫操作時,将多個Buffer的資料同時寫入同一個Channel,是以Channel将多個Buffer中的資料“聚集(Gather)”後發送到Channel。

Scatter/Gather經常用于需要将傳輸的資料分開處理的場合,例如傳輸一個由消息頭和消息體組成的消息,你可能會将消息頭和消息體分散到不同的buffer中,這樣你可以友善的處理消息頭和消息體。

案例:

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ScattingAndGather {
    public static void main(String[] args) {
        gather();
    }

    private static void gather() {
        ByteBuffer header = ByteBuffer.allocate(10);
        ByteBuffer body = ByteBuffer.allocate(10);
        byte[] b1 = {'0','1'};
        byte[] b2 = {'2','3'};
        header.put(b1);
        body.put(b2);
        ByteBuffer [] buffers = {header,body};
        try{
            FileOutputStream os = new FileOutputStream("e:/test.txt");
            FileChannel channel = os.getChannel();
            channel.write(buffers);
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}
           

6.2 transferFrom & transferTo

FileChannel的transferFrom()方法可以将資料從源通道傳輸到FileChannel中。

private static void method1() {
        RandomAccessFile fromFile = null;
        RandomAccessFile toFile = null;
        try {
            fromFile = new RandomAccessFile("e:/test1.txt","rw");
            FileChannel fromChannel = fromFile.getChannel();
            toFile = new RandomAccessFile("e:/test2.txt","rw");
            FileChannel toChannel = toFile.getChannel();
            long position = 0;
            long count = fromChannel.size();
            System.out.println("count:"+count);
            toChannel.transferFrom(fromChannel, position, count);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fromFile!=null){
                    fromFile.close();
                }
                if (toFile !=null){
                    toFile.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
           

方法的輸入參數position表示從position出開始開始向目标檔案寫入資料,count表示最多傳輸的位元組數。如果源通道的剩餘空間小于count個位元組,則所傳輸的位元組數要小于請求的位元組數。此外要注意,在SocketChannel的實作中,SocketChannel隻會傳輸此刻準備好的資料(可能不足count位元組)。是以,SocketChannel可能不會将請求的所有資料(count個位元組)全部傳輸到FileChannel中。

transferTo()方法将資料從FileChannel傳輸到其他的Channel中。

private static void method2() {
        RandomAccessFile fromFile = null;
        RandomAccessFile toFile = null;
        try {
            fromFile = new RandomAccessFile("e:/test1.txt","rw");
            FileChannel fromChannel = fromFile.getChannel();
            toFile = new RandomAccessFile("e:/test2.txt","rw");
            FileChannel toChannel = toFile.getChannel();
            long position = 0;
            long count = fromChannel.size();
            System.out.println("count:"+count);
            fromChannel.transferTo(position, count, toChannel);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fromFile!=null){
                    fromFile.close();
                }
                if (toFile !=null){
                    toFile.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
           

上面所述的關于SocketChannel的問題在transferTo()方法中同樣存在。SocketChannel會一直傳輸資料知道目标buffer被填滿。

6.3 Pipe

JavaNIO管道是2個線程之間的單向資料連接配接。Pipe有一個source通道和一個sink通道。資料會被寫到sink通道,從source通道讀取。

private static void method1() {
        Pipe pipe = null;
        ExecutorService exec = Executors.newFixedThreadPool(2);
        try {
            pipe = Pipe.open();
            final Pipe tempPipe = pipe;
            exec.submit(new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    Pipe.SinkChannel sinkChannel = tempPipe.sink();//向通道中寫資料
                    while (true) {
                        TimeUnit.SECONDS.sleep(1);
                        String info = "Pipe test At time " + System.currentTimeMillis();
                        ByteBuffer buf = ByteBuffer.allocate(1024);
                        buf.clear();
                        buf.put(info.getBytes());
                        buf.flip();
                        while (buf.hasRemaining()) {
                            sinkChannel.write(buf);
                            System.out.println("發送:"+buf);
                        }
                    }
                }
            });

            exec.submit(new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    Pipe.SourceChannel sourceChannel = tempPipe.source();
                    while (true) {
                        TimeUnit.SECONDS.sleep(1);
                        ByteBuffer buf = ByteBuffer.allocate(1024);
                        buf.clear();
                        int readLen;
                        while ((readLen = sourceChannel.read(buf)) > 0) {
                            buf.flip();
                            byte[] b = new byte[readLen];
                            int i=0;
                            while (buf.hasRemaining()) {
                                b[i] = buf.get();
                                System.out.print((char)b[i]);
                                i++;
                            }
                            String s = new String(b);
                            System.out.println("==========================||"+s);
                        }
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            exec.shutdown();
        }
    }
           

6.4 DatagramChannel

Java NIO中的DatagramChannel是一個能收發UDP包的通道。因為UDP是無連接配接的網絡協定,是以不能像其他通道那樣讀取和寫入。它發送和接收的都是資料包。

private static void send() {
        DatagramChannel channel = null;
        try{
            channel = DatagramChannel.open();
            String info = "I'm sender!";
            ByteBuffer buf = ByteBuffer.allocate(1024);
            buf.clear();
            buf.put(info.getBytes());
            buf.flip();
            int byteSend = channel.send(buf, new InetSocketAddress("127.0.0.1",8888));
            System.out.print(byteSend);
        }catch (IOException e) {
            e.printStackTrace();
        } finally {
            try{
                if (channel!=null){
                    channel.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    private static void receive() {
        DatagramChannel channel = null;
        try{
            channel = DatagramChannel.open();
            channel.socket().bind(new InetSocketAddress(8888));
            ByteBuffer buf = ByteBuffer.allocate(1024);
            buf.clear();
            channel.receive(buf);
            buf.flip();
            while (buf.hasRemaining()) {
                System.out.print((char)buf.get());
            }
        }catch (IOException e) {
            e.printStackTrace();
        } finally {
            try{
                if (channel!=null){
                    channel.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }