Java NIO 學習總結

一.Java NIO 核心組成部分:

  • Channels
  • Buffers
  • Selectors
    所有的IO在NIO中都從一個Channel開始,Channel有點像流,數據可以從Channel讀取到Buffer中,也可以從Buffer寫到Channel中.
    Channel和Buffer有幾種類型:
  • FileChannel. 從文件中讀寫數據
  • DatagramChannel 能通過UDP讀寫網絡中的數據.
    • SocketChannel SocketChannel 能通過TCP讀寫網絡中的數據.
  • ServerSocketChannel 可以監聽新進來的TCP連接,像Web服務器那樣,對每一個新進來的連接都會創建一個SocketChannel.
    Java NIO的通道類似流,但又有些不同:
    既可以從通道中讀取數據,又可以寫數據到通道,但流的讀寫通常是單向的.
    通道可以異步地的讀寫.
    通道中的數據總是先讀到一個Buffer,或者總是要從一個Buffer中寫入.

二.Buffer

1.基本用法(四個步驟):

寫入數據到Buffer.
調用flip()方法.
從Buffer中讀取數據.
調用clear()方法或者compact()方法. 當向Buffer寫入數據時,Buffer會記錄寫了多少數據,一旦要讀取數據,* 需要通過flip()方法將Buffer從寫模式切換到讀模式,在讀模式下,可以讀取之前寫入到buffer的所有數據.
一旦讀完所有數據,就需要清空緩衝區,讓它可以再次被寫入,clear()方法會清空整個緩衝區,compact()方法只會清除已經讀過的數據,任何未讀的數據被移動到緩衝區的起始處,新寫入的數據放在其後.
Buffer的capacity,position和limit
緩衝區本質是一塊可以寫入數據,然後可以從中讀取數據的內存.
這塊內存被包裝成NIO Buffer對象,並提供了一組方法,用來方便的訪問該塊內存.
position和limit的含義取決於Buffer處在讀模式還是寫模式,不管Buffer處於什麼模式,capacity的含義總是一樣的.
capacity:Buffer的固定大小值.
position: 初始值爲0,當一個byte,long等數據寫到Buffer後,position向前移動到下一個可插入數據的Buffer單元,position最大可爲capacity-1;
當讀數據時,也是從某個特定位置讀,當將Buffer從寫模式切換到讀模式,position會被重置爲0,當從Buffer的position處讀取數據時,position向前移動到下一個可讀的位置.
limit:在寫模式下,Buffer的limit表示你最多能往Buffer裏寫多少數據,寫模式下,limit等於Buffer的capacity。  
當切換到Buffer的讀模式時,limit表示最多能讀多少數據,因此,limit會被設置成寫模式下的position值.

2.Buffer的分配

ByteBBuffer buf = ByteBuffer.allocate(48);
CharBuffer buf = CharBuffer.allocate(1024);

3.向Buffer中寫數據

寫數據到Buffer的兩種方式:
從Channel寫到Buffer。 
通過Buffer的put()方法寫到Buffer裏.
從Channel寫到Buffer的例子:

int bytesRead = inChannel.write(buf);

通過put方法寫Buffer的例子:

buf.put(127); 

4. flip()方法

flip方法將Buffer從寫模式切換到讀模式。調用flip()方法會將position設回0,並將limit設置成之前position的值。
換句話說,position現在用於標記讀的位置,limit表示之前寫進了多少個byte、char等 —— 現在能讀取多少個byte、char等。

5.從Buffer中讀取數據

從Buffer中讀取數據有兩種方式:
  • 從Buffer讀取數據到Channel。
    int byteRead = inChannel.write(buf).
  • 使用get()方法從Buffer中讀取數據。
    byte aByte = buf.get();

6. rewind()方法

Buffer.rewind()將position設回0,所以你可以重讀Buffer中的所有數據。limit保持不變,仍然表示能從Buffer中讀取多少個元素(byte、char等).

7.mark和reset()方法

通過調用Buffer.mark()方法,可以標記Buffer中的一個特定position。之後可以通過調用Buffer.reset()方法恢復到這個position。

buffer.mark();
//call buffer.get(); 
buffer.reset();  

8.equals()

當滿足下列條件時,表示兩個Buffer相等:
* 有相同的類型(byte、char、int等)。
* Buffer中剩餘的byte、char等的個數相等。
* Buffer中所有剩餘的byte、char等都相同。
如你所見,equals只是比較Buffer的一部分,不是每一個在它裏面的元素都比較。實際上,它只比較Buffer中的剩餘元素。

9.compareTo()方法

compareTo()方法比較兩個Buffer的剩餘元素(byte,char),如果滿足下列條件,則認爲一個Buffer小於另外一個Buffer
* 第一個不相等的元素小於另一個Buffer中對應的元素.
* 所有元素都相等,但第一個Buffer比另外一個先耗盡.

10. scatter/gather.

  • 分散(scatter)從Channel中讀取是指在讀操作時將讀取的數據寫入多個buffer,因此Channel將從Channel中讀取的數據”分散(scatter)”到多個Buffer中.
  • 聚集(gather)寫入Channel是指在寫操作時將多個buffer的數據寫入同一個Channel,因此,Channel將多個Buffer中的數據”聚集”後發送.
  • scatter / gather經常用於需要將傳輸的數據分開處理的場合,例如傳輸一個由消息頭和消息體組成的消息,你可能會將消息體和消息頭分散到不同的buffer中,這樣你可以方便的處理消息頭和消息體。

11.transferForm()

FileChannel的transferFrom()方法可以將數據從源通道傳輸到FileChannel中.

public class FileChannelTest {
    public static void main(String[] args) {
        try{
            RandomAccessFile fromFile = new RandomAccessFile("1.cpp","rw");
            FileChannel fromChannel = fromFile.getChannel();
            RandomAccessFile tofile = new RandomAccessFile("fei.cpp","rw");
            FileChannel toChannel = tofile.getChannel();

            long position = 0;
            long count = fromChannel.size();
            toChannel.transferFrom(fromChannel,position,count);

        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException t){
            t.printStackTrace();
        }
    }
}

Selector 選擇器 

Select(選擇器)是Java NIO 中能夠檢測一到多個NIO通道,並能夠知曉通道是否爲諸如讀寫事件做好準備的組件,這樣,一個單獨的線程就可以管理多個channel,從而管理多個網絡連接.

1.通過調用Selector.open()方法創建一個Selector,如下:

Selector selector = Selector.open();

2.向Selector註冊通道

channel.configureBlocking(false);
Selection key = channel.register(selector,Selectionkey.OP_READ);

與Selector一起使用時,Channel必須處於非阻塞模式下,這意味着不能將FileChannel與Selector一起使用,因爲FileChannel不能切換到非阻塞模式,而套接字通道都可以.
可以監聽四種不同類型的事件:
1. Connect
2. Accept
3. Read
4. Write
通道觸發了一個事件意思是該事件已經就緒。所以,某個channel成功連接到另一個服務器稱爲“連接就緒”。一個server socket channel準備好接收新進入的連接稱爲“接收就緒”。一個有數據可讀的通道可以說是“讀就緒”。等待寫數據的通道可以說是“寫就緒”。

3.SelectionKey

當向Selector註冊Channel時,register()方法會返回一個SelectionKey對象,這個對象包含一些你感興趣的屬性.
interest集合
ready集合
Channel
Selector

4.interest集合  

interest集合是你選擇的感興趣的事件集合,可以通過SelectionKey讀寫interest集合,像這樣:

int interestSet = selectionKey.interestOps(); 
boolean isInterestedInAccept  = (interestSet & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT;

boolean isInterestedInConnect = interestSet & SelectionKey.OP_CONNECT;

boolean isInterestedInRead    = interestSet & SelectionKey.OP_READ;

boolean isInterestedInWrite   = interestSet & SelectionKey.OP_WRITE;

可以看到,用”位與”操作interest集合和給定的SelectionKey常量,可以確定某個確定的事件是否在interest集合中.

5.ready集合  

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

int readySet = selectionKey.readOps();

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

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

Channel + Selector
從SelectionKey訪問Channel和Selector很簡單,如下:

Channel channel = selectionKey,channel();
Selector selector = selectionKey.selector(); 

6.通過Selector選擇通道

一旦向Selector註冊了一個或多個通道,就可以調用幾個重載的select()方法,這些方法返回你所感興趣的事件(如連接,接受,讀或寫)已經準備就緒的那些通道,換句話說,如果你對”讀就緒事件感興趣”,select()方法會返回讀事件已經就緒的那些通道.
1.int select() 阻塞到至少有一個通道在你註冊的事件上就緒了.
2.select(long timeout)在timeout之內,有事件發生,返回,超時無事件也返回.
3.selectNow()不會阻塞,不管什麼通道就緒立刻返回.沒有事件返回0.
4.select()方法返回的int值表示有多少通道已經就緒。亦即,自上次調用select()方法後有多少通道變成就緒狀態。如果調用select()方法,因爲有一個通道變成就緒狀態,返回了1,若再次調用select()方法,如果另一個通道就緒了,它會再次返回1。如果對第一個就緒的channel沒有做任何操作,現在就有兩個就緒的通道,但在每次select()方法調用之間,只有一個通道就緒了。
selectedKeys()
一旦調用了select()方法,並且返回值表明有一個或更多個通道就緒了,然後可以通過調用selector的selectedKeys()方法,訪問”已選擇鍵集”(selected key set )”中就緒通道.如下: 
遍歷這個已選擇的鍵集合來訪問就緒的通道:

Set selectedKeys = selector.selectedKeys();
Iterator keyIterator = selectedKeys.iterator();
while(keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
if(key.isAcceptable()) {
// a connection was accepted by a ServerSocketChannel.
} else if (key.isConnectable()) {
// a connection was established with a remote server.
} else if (key.isReadable()) {
// a channel is ready for reading
} else if (key.isWritable()) {
// a channel is ready for writing
}
 keyIterator.remove();
}

這個循環遍歷已選擇鍵集中的每個鍵,並檢測各個鍵多對應的通道的就緒事件.
注意每次迭代末尾的keyIterator.remove()調用。Selector不會自己從已選擇鍵集中移除SelectionKey實例。必須在處理完通道時自己移除。下次該通道變成就緒時,Selector會再次將其放入已選擇鍵集中。

SelectionKey.channel()方法返回的通道需要轉型成你要處理的類型,如ServerSocketChannel或SocketChannel等。
wakeup()
某個線程調用select()方法後阻塞了,即使沒有通道已經就緒,也有辦法讓其select()方法返回,只要讓其它線程在第一個線程調用select()方法的那個對象上調用Selector.wakeup()方法即可,阻塞在select()方法上的線程會立馬返回.
**如果有其它線程調用了wakeup()方法,但當前沒有線程阻塞在select()方法上,下一個調用select()方法的線程會立即”醒來”.
close()
用完Selector後調用其close()方法會關閉Selector,且使註冊到該Selector上的所有SelectionKey實例無效,通道本身不會關閉.

7.FileChannel.

FileChannel無法設置爲非阻塞模式,它總是運行在阻塞模式下.
1.打開FileChannel
在使用FileChannel之前,必須先打開它,但是,我們無法直接打開一個FileChannel,需要通過使用一個InputStream,OutputStream或RandomAccessFile來獲取一個FileChannel實例,

RandomAccessFile aFile = new RandomAccessFile("1.txt","rw");
FileChannel inChannel = aFile.getChannel();  

2.從FileChannel讀取數據
調用多個read()方法之一從FileChannel中讀取數據. 如:

ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = inChannel.read(buf); 

3.向FileChannel寫數據  
使用FileChannel.write()方法向FileChannel寫數據,該方法的參數是一個Buffer,如:

String newData = "New String to write to file..." + System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while(buf.hasRemaining()){
channel.write(buf);
}
注意FileChannel.write()是在while循環中調用的,因爲無法保證write()方法一次能向FileChannel寫入多少字節,因此需要重複調用write()方法,直到Buffer已經沒有尚未寫入通道的字節.  

4.關閉FileChannel

channel.close();
FileChannel的position方法.
有時可能需要在FileChannel的某個特定位置進行數據的讀/寫操作。可以通過調用position()方法獲取FileChannel的當前位置。
也可以通過調用position(long pos)方法設置FileChannel的當前位置。

例子如下:

long pos = channel.position();
channel.position(pos +123);

如果將位置設置在文件結束之後,然後試圖從文件中讀取數據,該方法將返回-1—文件結束標誌.
如果將位置設置在文件結束符之後,然後像通道中寫數據,文件將撐大到當前位置並寫入數據,這可能導致”文件空洞”,磁盤上物理文件中寫入的數據間有空隙。 
5.FileChannel的size()方法
FileChannel實例的size()方法返回的是該實例鎖關聯文件的大小.

long fileSize = channel.size();

6.FileChannel的truncate方法
可以使用FileChannel.truncate()方法截取一個文件,截取文件時,文件中指定長度後面的部分將被刪除.

channel.truncate(1024).

7.FileChannel的force方法
FileChannel.force()方法將通道里尚未寫入磁盤的數據強制寫到磁盤上。出於性能方面的考慮,操作系統會將數據緩存在內存中,所以無法保證寫入到FileChannel裏的數據一定會即時寫到磁盤上。要保證這一點,需要調用force()方法。
force()方法有一個boolean類型的參數,指明是否同時將文件元數據(權限信息等)寫到磁盤上。

channel.force(true); 

8.SocketChannel

SocketChannel 是一個連接到TCP網絡套接字的通道.可以通過以下兩種方式創建SocketChannel:
* 打開一個SocketChannel並連接到互聯網上的某臺服務器.
* 一個新連接到達ServerSocketChannel,會創建一個SocketChannel.
打開SocketChannel
1.打開SocketChannel

SocketChannel socketChannel =SocketChannel.open();
SocketChannel.connect(new InetSocketAddress(IP,80));

2.關閉SocketChannel
當用完SocketChannel之後調用SocketChannel.close()關閉SocketChannel:

socketChannel.close();

3.從SocketChannel讀取數據  
要從SocketChannel中讀取數據,調用一個read()的方法之一,以下是例子:
ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = socketChannel.read(buf);
首先,分配一個Buffer。從SocketChannel讀取到的數據將會放到這個Buffer中。
然後,調用SocketChannel.read()。該方法將數據從SocketChannel 讀到Buffer中。read()方法返回的int值表示讀了多少字節進Buffer裏。如果返回的是-1,表示已經讀到了流的末尾(連接關閉了)。
4.從SocketChannel寫數據

String newData = "New String to write to file..." + System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while(buf.hasRemaining()){
channel.write(buf);

注意SocketChannel.write()方法的調用是在一個while循環中的。Write()方法無法保證能寫多少字節到SocketChannel。所以,我們重複調用write()直到Buffer沒有要寫的字節爲止。
5.非阻塞模式  
可以設置SocketChannel爲非阻塞模式,設置之後,就可以在異步模式下調用connect(),read()和write()。
connect()
如果SocketChannel在非阻塞模式下,此時調用connect(),該方法可能在連接建立之前就返回了,爲了確定連接是否建立,可以調用finishConnect()的方法.像這樣: 

socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress(IP,port));
while(!socketChannel.finishConnect()){
//wait,or do something else ... 
}

write()
非阻塞模式下,write()方法在尚未寫出任何內容時可能就返回了。所以需要在循環中調用write()。
read()
非阻塞模式下,read()方法在尚未讀取到任何數據時可能就返回了。所以需要關注它的int返回值,它會告訴你讀取了多少字節。

9.ServerSocketChannel

ServerSocketChannel 是一個可以監聽進來的TCP連接的通道,就像標準IO中的ServerSocket一樣,ServerSocketChannel.
例子:

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); 
serverSocketChannel.socket().bind(new InetSocketAddress(9999));
while(true){
SocketChannel socketChannel = serverSocketChannel.accept();  
}

1.打開ServerSocketChannel
通過調用ServerSocketChannel.open()方法來打開ServerSocketChannel;

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); 

2.關閉ServerSocketChannel
通過調用ServerSocketChannel.close來關閉ServerSocketChannel.  
3.監聽新進來的連接 
通過ServerSocketChannel.accept()方法監聽新進來的連接,當accept()返回時,包含一個新進來的連接的SocketChannel.因此.accept()方法會一直阻塞到有新連接到達.
4.非阻塞模式  
ServerSocketChannel可以設置成非阻塞模式。在非阻塞模式下,accept() 方法會立刻返回,如果還沒有新進來的連接,返回的將是null。 因此,需要檢查返回的SocketChannel是否是null.如:

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

10 Pipe

管道是2個線程之間的單向數據連接,Pipe有一個source通道和一個sink通道,數據會被寫到sink通道,從source通道讀取.
1.創建管道  
通過Pipe.open()方法打開管道.

Pipe pipe = Pipe.open(); 

2.向管道中寫數據

Pipe.SinkChannel sinkChannel = pipe.sink(); 

通過調用SinkChannel的Write()方法,將數據寫入到SinkChannel,像這樣:
String

String newData = "New String to write to file..." + System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while(buf.hasRemaining()){
channel.write(buf);

3.從管道中讀取數據

Pipe.SourceChannel sourceChannel = pipe.source();
ByteBuffer buf =ByteBuffer.allocate(48);
int bytesRead = sourceChannel.read(buf); 
read()方法的返回值int會告訴我們多少自己被讀進了緩衝區.  
發佈了109 篇原創文章 · 獲贊 238 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章