Java NIO(New IO)是從Java 1.4版本開始引入的一個新的IO API,可以替代標(biāo)準(zhǔn)的Java IO API。
Java NIO提供了與標(biāo)準(zhǔn)IO不同的IO工作方式:
- Channels and Buffers(通道和緩沖區(qū)):標(biāo)準(zhǔn)的IO基于字節(jié)流和字符流進(jìn)行操作的眯亦,而NIO是基于通道(Channel)和緩沖區(qū)(Buffer)進(jìn)行操作,數(shù)據(jù)總是從通道讀取到緩沖區(qū)中姿搜,或者從緩沖區(qū)寫入到通道中笤昨。
- Asynchronous IO(異步IO):Java NIO可以讓你異步的使用IO,例如:當(dāng)線程從通道讀取數(shù)據(jù)到緩沖區(qū)時停士,線程還是可以進(jìn)行其他事情挖帘。當(dāng)數(shù)據(jù)被寫入到緩沖區(qū)時完丽,線程可以繼續(xù)處理它。從緩沖區(qū)寫入通道也類似拇舀。
- Selectors(選擇器):Java NIO引入了選擇器的概念逻族,選擇器用于監(jiān)聽多個通道的事件(比如:連接打開,數(shù)據(jù)到達(dá))骄崩。因此聘鳞,單個的線程可以監(jiān)聽多個數(shù)據(jù)通道。
下面就來詳細(xì)介紹Java NIO的相關(guān)知識要拂。
1抠璃、Java NIO 概述
Java NIO 由以下幾個核心部分組成:
- Channels
- Buffers
- Selectors
雖然 Java NIO 中除此之外還有很多類和組件,但在我看來脱惰,Channel搏嗡,Buffer 和 Selector 構(gòu)成了核心的 API。其它組件枪芒,如 Pipe 和 FileLock彻况,只不過是與三個核心組件共同使用的工具類。因此舅踪,在概述中我將集中在這三個組件上纽甘。其它組件會在單獨(dú)的章節(jié)中講到。
Channel 和 Buffer
基本上抽碌,所有的 IO 在NIO 中都從一個 Channel 開始悍赢。Channel 有點(diǎn)象流。 數(shù)據(jù)可以從 Channel 讀到 Buffer 中货徙,也可以從 Buffer 寫到 Channel 中左权。這里有個圖示:
<figure style="margin: 1em 0px; color: rgb(26, 26, 26); font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"></figure>
Channel 和 Buffer 有好幾種類型。下面是 JAVA NIO 中的一些主要 Channel 的實(shí)現(xiàn):
- FileChannel
- DatagramChannel
- SocketChannel
- ServerSocketChannel
正如你所看到的痴颊,這些通道涵蓋了 UDP 和 TCP 網(wǎng)絡(luò) IO赏迟,以及文件 IO。
與這些類一起的有一些有趣的接口蠢棱,但為簡單起見锌杀,我盡量在概述中不提到它們。本教程其它章節(jié)與它們相關(guān)的地方我會進(jìn)行解釋泻仙。
以下是 Java NIO 里關(guān)鍵的 Buffer 實(shí)現(xiàn):
- ByteBuffer
- CharBuffer
- DoubleBuffer
- FloatBuffer
- IntBuffer
- LongBuffer
- ShortBuffer
這些 Buffer 覆蓋了你能通過 IO 發(fā)送的基本數(shù)據(jù)類型:byte, short, int, long, float, double 和 char糕再。
Java NIO 還有個 MappedByteBuffer,用于表示內(nèi)存映射文件玉转, 我也不打算在概述中說明突想。
Selector
Selector 允許單線程處理多個 Channel。如果你的應(yīng)用打開了多個連接(通道),但每個連接的流量都很低猾担,使用 Selector 就會很方便袭灯。例如,在一個聊天服務(wù)器中绑嘹。
這是在一個單線程中使用一個 Selector 處理3個 Channel 的圖示:
<figure style="margin: 1em 0px; color: rgb(26, 26, 26); font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"></figure>
要使用 Selector妓蛮,得向 Selector 注冊 Channel,然后調(diào)用它的 select() 方法圾叼。這個方法會一直阻塞到某個注冊的通道有事件就緒。一旦這個方法返回捺癞,線程就可以處理這些事件夷蚊,事件的例子有如新連接進(jìn)來,數(shù)據(jù)接收等髓介。
2惕鼓、Channel
Java NIO 的通道類似流,但又有些不同:
- 既可以從通道中讀取數(shù)據(jù)唐础,又可以寫數(shù)據(jù)到通道箱歧。但流的讀寫通常是單向的。
- 通道可以異步地讀寫一膨。
- 通道中的數(shù)據(jù)總是要先讀到一個 Buffer呀邢,或者總是要從一個 Buffer 中寫入。
正如上面所說豹绪,從通道讀取數(shù)據(jù)到緩沖區(qū)价淌,從緩沖區(qū)寫入數(shù)據(jù)到通道。如下圖所示:
<figure style="margin: 1em 0px; color: rgb(26, 26, 26); font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"></figure>
Channel 的實(shí)現(xiàn)
這些是 Java NIO 中最重要的通道的實(shí)現(xiàn):
- FileChannel
- DatagramChannel
- SocketChannel
- ServerSocketChannel
- FileChannel 從文件中讀寫數(shù)據(jù)瞒津。
DatagramChannel 能通過 UDP 讀寫網(wǎng)絡(luò)中的數(shù)據(jù)蝉衣。
SocketChannel 能通過 TCP 讀寫網(wǎng)絡(luò)中的數(shù)據(jù)。
ServerSocketChannel 可以監(jiān)聽新進(jìn)來的 TCP 連接巷蚪,像 Web 服務(wù)器那樣病毡。對每一個新進(jìn)來的連接都會創(chuàng)建一個 SocketChannel。
基本的 Channel 示例
下面是一個使用 FileChannel 讀取數(shù)據(jù)到 Buffer 中的示例:
RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel inChannel = aFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = inChannel.read(buf);while (bytesRead != -1) {
System.out.println("Read " + bytesRead);
buf.flip();while(buf.hasRemaining()){
System.out.print((char) buf.get());}
buf.clear();bytesRead = inChannel.read(buf);
}
aFile.close();
注意 buf.flip() 的調(diào)用屁柏,首先讀取數(shù)據(jù)到Buffer啦膜,然后反轉(zhuǎn)Buffer,接著再從Buffer中讀取數(shù)據(jù)。下一節(jié)會深入講解Buffer的更多細(xì)節(jié)前联。
3功戚、Buffer
Java NIO 中的 Buffer 用于和 NIO 通道進(jìn)行交互。如你所知似嗤,數(shù)據(jù)是從通道讀入緩沖區(qū)啸臀,從緩沖區(qū)寫入到通道中的。
緩沖區(qū)本質(zhì)上是一塊可以寫入數(shù)據(jù),然后可以從中讀取數(shù)據(jù)的內(nèi)存乘粒。這塊內(nèi)存被包裝成 NIO Buffer 對象豌注,并提供了一組方法,用來方便的訪問該塊內(nèi)存灯萍。
Buffer 的基本用法
使用 Buffer 讀寫數(shù)據(jù)一般遵循以下四個步驟:
- 寫入數(shù)據(jù)到 Buffer
- 調(diào)用 flip() 方法
- 從 Buffer 中讀取數(shù)據(jù)
- 調(diào)用 clear() 方法或者 compact() 方法
當(dāng)向 buffer 寫入數(shù)據(jù)時轧铁,buffer 會記錄下寫了多少數(shù)據(jù)。一旦要讀取數(shù)據(jù)旦棉,需要先通過 flip() 方法將 Buffer 從寫模式切換到讀模式齿风。在讀模式下,可以讀取之前寫入到 buffer 的所有數(shù)據(jù)绑洛。
一旦讀完了所有的數(shù)據(jù)救斑,就需要清空緩沖區(qū),讓它可以再次被寫入真屯。有兩種方式能清空緩沖區(qū):調(diào)用 clear() 或 compact() 方法脸候。clear() 方法會清空整個緩沖區(qū)。compact() 方法只會清除已經(jīng)讀過的數(shù)據(jù)绑蔫,任何未讀的數(shù)據(jù)都被移到緩沖區(qū)的起始處运沦,新寫入的數(shù)據(jù)將放到緩沖區(qū)未讀數(shù)據(jù)的后面。
下面是一個使用Buffer的例子:
RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel inChannel = aFile.getChannel();//create buffer with capacity of 48 bytes
ByteBuffer buf = ByteBuffer.allocate(48);int bytesRead = inChannel.read(buf);
//read into buffer.
while (bytesRead != -1) {
buf.flip(); //make buffer ready for read
while(buf.hasRemaining()){
System.out.print((char) buf.get()); // read 1 byte at a time
}
buf.clear(); //make buffer ready for writing
bytesRead = inChannel.read(buf);
}
aFile.close();
Buffer 的 capacity配深、 position 和 limit
緩沖區(qū)本質(zhì)上是一塊可以寫入數(shù)據(jù)携添,然后可以從中讀取數(shù)據(jù)的內(nèi)存。這塊內(nèi)存被包裝成 NIO Buffer 對象凉馆,并提供了一組方法薪寓,用來方便的訪問該塊內(nèi)存。
為了理解 Buffer 的工作原理澜共,需要熟悉它的三個屬性:
- capacity
- position
- limit
position 和 limit 的含義取決于 Buffer 處在讀模式還是寫模式向叉。不管 Buffer 處在什么模式,capacity 的含義總是一樣的嗦董。
這里有一個關(guān)于 capacity母谎,position 和 limit 在讀寫模式中的說明,詳細(xì)的解釋在插圖后面。
<figure style="margin: 1em 0px; color: rgb(26, 26, 26); font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"></figure>
capacity
作為一個內(nèi)存塊,Buffer 有一個固定的大小值簇宽,也叫“capacity”.你只能往里寫 capacity 個byte、long咬扇,char 等類型。一旦 Buffer 滿了廊勃,需要將其清空(通過讀數(shù)據(jù)或者清除數(shù)據(jù))才能繼續(xù)寫數(shù)據(jù)往里寫數(shù)據(jù)懈贺。
position
當(dāng)你寫數(shù)據(jù)到 Buffer 中時经窖,position 表示當(dāng)前的位置。初始的 position 值為 0梭灿。當(dāng)一個 byte画侣、long 等數(shù)據(jù)寫到 Buffer 后, position 會向前移動到下一個可插入數(shù)據(jù)的 Buffer 單元堡妒。position 最大可為 capacity – 1.
當(dāng)讀取數(shù)據(jù)時配乱,也是從某個特定位置讀。當(dāng)將 Buffer 從寫模式切換到讀模式皮迟,position 會被重置為 0. 當(dāng)從 Buffer 的 position 處讀取數(shù)據(jù)時搬泥,position 向前移動到下一個可讀的位置。
limit
在寫模式下伏尼,Buffer 的 limit 表示你最多能往 Buffer 里寫多少數(shù)據(jù)佑钾。 寫模式下,limit 等于Buffer 的 capacity烦粒。
當(dāng)切換 Buffer 到讀模式時, limit 表示你最多能讀到多少數(shù)據(jù)代赁。因此扰她,當(dāng)切換 Buffer 到讀模式時,limit 會被設(shè)置成寫模式下的 position 值芭碍。換句話說徒役,你能讀到之前寫入的所有數(shù)據(jù)(limit被設(shè)置成已寫數(shù)據(jù)的數(shù)量,這個值在寫模式下就是 position)
Buffer的類型
Java NIO 有以下Buffer類型
- ByteBuffer
- MappedByteBuffer
- CharBuffer
- DoubleBuffer
- FloatBuffer
- IntBuffer
- LongBuffer
- ShortBuffer
如你所見窖壕,這些Buffer類型代表了不同的數(shù)據(jù)類型忧勿。換句話說,就是可以通過char瞻讽,short鸳吸,int,long速勇,float 或 double類型來操作緩沖區(qū)中的字節(jié)晌砾。
MappedByteBuffer 有些特別,在涉及它的專門章節(jié)中再講烦磁。
Buffer 的分配
要想獲得一個 Buffer 對象首先要進(jìn)行分配养匈。 每一個 Buffer 類都有一個 allocate 方法。下面是一個分配48字節(jié) capacity 的 ByteBuffer 的例子都伪。
ByteBuffer buf = ByteBuffer.allocate(48);
這是分配一個可存儲1024個字符的 CharBuffer:
CharBuffer buf = CharBuffer.allocate(1024);
向 Buffer 中寫數(shù)據(jù)
寫數(shù)據(jù)到 Buffer 有兩種方式:
- 從 Channel 寫到 Buffer呕乎。
- 通過 Buffer 的 put() 方法寫到 Buffer 里。
從 Channel 寫到 Buffer 的例子:
int bytesRead = inChannel.read(buf); //read into buffer.
通過put方法寫B(tài)uffer的例子:
buf.put(127);
put 方法有很多版本陨晶,允許你以不同的方式把數(shù)據(jù)寫入到 Buffer 中猬仁。例如, 寫到一個指定的位置,或者把一個字節(jié)數(shù)組寫入到 Buffer逐虚。 更多Buffer實(shí)現(xiàn)的細(xì)節(jié)參考JavaDoc聋溜。
flip() 方法
flip() 方法將 Buffer 從寫模式切換到讀模式。調(diào)用 flip() 方法會將 position 設(shè)回 0叭爱,并將 limit 設(shè)置成之前 position 的值撮躁。
換句話說,position 現(xiàn)在用于標(biāo)記讀的位置买雾,limit 表示之前寫進(jìn)了多少個 byte把曼、char等 —— 現(xiàn)在能讀取多少個byte、char等漓穿。
從Buffer中讀取數(shù)據(jù)
從Buffer中讀取數(shù)據(jù)有兩種方式:
- 從Buffer讀取數(shù)據(jù)到Channel嗤军。
- 使用get()方法從Buffer中讀取數(shù)據(jù)。
從Buffer讀取數(shù)據(jù)到Channel的例子:
//read from buffer into channel.int bytesWritten = inChannel.write(buf);
byte aByte = buf.get();
使用get()方法從Buffer中讀取數(shù)據(jù)的例子
get方法有很多版本晃危,允許你以不同的方式從Buffer中讀取數(shù)據(jù)叙赚。例如,從指定position讀取僚饭,或者從Buffer中讀取數(shù)據(jù)到字節(jié)數(shù)組震叮。更多Buffer實(shí)現(xiàn)的細(xì)節(jié)參考JavaDoc。
rewind()方法
Buffer.rewind()將position設(shè)回0鳍鸵,所以你可以重讀Buffer中的所有數(shù)據(jù)苇瓣。limit保持不變,仍然表示能從Buffer中讀取多少個元素(byte偿乖、char等)击罪。
clear()與compact()方法
一旦讀完Buffer中的數(shù)據(jù),需要讓Buffer準(zhǔn)備好再次被寫入贪薪∠苯可以通過clear()或compact()方法來完成。
如果調(diào)用的是clear()方法画切,position將被設(shè)回0损话,limit被設(shè)置成 capacity的值。換句話說槽唾,Buffer 被清空了丧枪。Buffer中的數(shù)據(jù)并未清除,只是這些標(biāo)記告訴我們可以從哪里開始往Buffer里寫數(shù)據(jù)庞萍。
如果Buffer中有一些未讀的數(shù)據(jù)拧烦,調(diào)用clear()方法,數(shù)據(jù)將“被遺忘”钝计,意味著不再有任何標(biāo)記會告訴你哪些數(shù)據(jù)被讀過恋博,哪些還沒有齐佳。
如果Buffer中仍有未讀的數(shù)據(jù),且后續(xù)還需要這些數(shù)據(jù)债沮,但是此時想要先先寫些數(shù)據(jù)炼吴,那么使用compact()方法。
compact()方法將所有未讀的數(shù)據(jù)拷貝到Buffer起始處疫衩。然后將position設(shè)到最后一個未讀元素正后面硅蹦。limit屬性依然像clear()方法一樣,設(shè)置成capacity∶泼海現(xiàn)在Buffer準(zhǔn)備好寫數(shù)據(jù)了童芹,但是不會覆蓋未讀的數(shù)據(jù)。
mark()與reset()方法
通過調(diào)用Buffer.mark()方法鲤拿,可以標(biāo)記Buffer中的一個特定position假褪。之后可以通過調(diào)用Buffer.reset()方法恢復(fù)到這個position。例如:
buffer.mark();
//call buffer.get() a couple of times, e.g. during parsing.
buffer.reset(); //set position back to mark.
equals()與compareTo()方法
可以使用equals()和compareTo()方法兩個Buffer近顷。
equals()
當(dāng)滿足下列條件時生音,表示兩個Buffer相等:
- 有相同的類型(byte、char窒升、int等)久锥。
- Buffer中剩余的byte、char等的個數(shù)相等异剥。
- Buffer中所有剩余的byte、char等都相同絮重。
如你所見冤寿,equals只是比較Buffer的一部分,不是每一個在它里面的元素都比較青伤。實(shí)際上督怜,它只比較Buffer中的剩余元素。
compareTo()方法
compareTo()方法比較兩個Buffer的剩余元素(byte狠角、char等)号杠, 如果滿足下列條件,則認(rèn)為一個Buffer“小于”另一個Buffer:
- 第一個不相等的元素小于另一個Buffer中對應(yīng)的元素 丰歌。
- 所有元素都相等姨蟋,但第一個Buffer比另一個先耗盡(第一個Buffer的元素個數(shù)比另一個少)。
(譯注:剩余元素是從 position到limit之間的元素)
4立帖、Scatter/Gather
Java NIO 開始支持 scatter/gather眼溶,scatter/gather 用于描述從 Channel(譯者注:Channel 在中文經(jīng)常翻譯為通道)中讀取或者寫入到 Channel 的操作。
分散(scatter)從 Channel 中讀取是指在讀操作時將讀取的數(shù)據(jù)寫入多個 buffer 中晓勇。因此堂飞,Channel 將從 Channel 中讀取的數(shù)據(jù)“分散(scatter)”到多個Buffer中灌旧。
聚集(gather)寫入Channel是指在寫操作時將多個buffer的數(shù)據(jù)寫入同一個Channel,因此绰筛,Channel 將多個Buffer中的數(shù)據(jù)“聚集(gather)”后發(fā)送到Channel枢泰。
scatter / gather經(jīng)常用于需要將傳輸?shù)臄?shù)據(jù)分開處理的場合,例如傳輸一個由消息頭和消息體組成的消息铝噩,你可能會將消息體和消息頭分散到不同的buffer中衡蚂,這樣你可以方便的處理消息頭和消息體。
Scattering Reads
Scattering Reads是指數(shù)據(jù)從一個channel讀取到多個buffer中薄榛。如下圖描述:
<figure style="margin: 1em 0px; color: rgb(26, 26, 26); font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"></figure>
代碼示例如下:
ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body = ByteBuffer.allocate(1024);
ByteBuffer[] bufferArray = { header, body };
channel.read(bufferArray);
注意buffer首先被插入到數(shù)組讳窟,然后再將數(shù)組作為channel.read() 的輸入?yún)?shù)。read()方法按照buffer在數(shù)組中的順序?qū)腸hannel中讀取的數(shù)據(jù)寫入到buffer敞恋,當(dāng)一個buffer被寫滿后丽啡,channel緊接著向另一個buffer中寫。
Scattering Reads在移動下一個buffer前硬猫,必須填滿當(dāng)前的buffer补箍,這也意味著它不適用于動態(tài)消息(譯者注:消息大小不固定)。換句話說啸蜜,如果存在消息頭和消息體坑雅,消息頭必須完成填充(例如 128byte),Scattering Reads才能正常工作衬横。
Gathering Writes
Gathering Writes是指數(shù)據(jù)從多個buffer寫入到同一個channel裹粤。如下圖描述:
<figure style="margin: 1em 0px; color: rgb(26, 26, 26); font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"></figure>
代碼示例如下:
ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body = ByteBuffer.allocate(1024);//write data into buffers
ByteBuffer[] bufferArray = { header, body };
channel.write(bufferArray);
buffers數(shù)組是write()方法的入?yún)ⅲ瑆rite()方法會按照buffer在數(shù)組中的順序蜂林,將數(shù)據(jù)寫入到channel遥诉,注意只有position和limit之間的數(shù)據(jù)才會被寫入。因此噪叙,如果一個buffer的容量為128byte矮锈,但是僅僅包含58byte的數(shù)據(jù),那么這58byte的數(shù)據(jù)將被寫入到channel中睁蕾。因此與Scattering Reads相反苞笨,Gathering Writes能較好的處理動態(tài)消息。
5子眶、通道之間的數(shù)據(jù)傳輸
在Java NIO中瀑凝,如果兩個通道中有一個是FileChannel,那你可以直接將數(shù)據(jù)從一個channel(譯者注:channel中文常譯作通道)傳輸?shù)搅硗庖粋€channel臭杰。
transferFrom()
FileChannel的transferFrom()方法可以將數(shù)據(jù)從源通道傳輸?shù)紽ileChannel中(譯者注:這個方法在JDK文檔中的解釋為將字節(jié)從給定的可讀取字節(jié)通道傳輸?shù)酱送ǖ赖奈募校?/p>
下面是一個簡單的例子:
RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");
FileChannel fromChannel = fromFile.getChannel();
RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
FileChannel toChannel = toFile.getChannel();
long position = 0;
long count = fromChannel.size();
toChannel.transferFrom(position, count, fromChannel);
方法的輸入?yún)?shù)position表示從position處開始向目標(biāo)文件寫入數(shù)據(jù)猜丹,count表示最多傳輸?shù)淖止?jié)數(shù)。如果源通道的剩余空間小于 count 個字節(jié)硅卢,則所傳輸?shù)淖止?jié)數(shù)要小于請求的字節(jié)數(shù)射窒。
此外要注意藏杖,在SoketChannel的實(shí)現(xiàn)中,SocketChannel只會傳輸此刻準(zhǔn)備好的數(shù)據(jù)(可能不足count字節(jié))脉顿。因此蝌麸,SocketChannel可能不會將請求的所有數(shù)據(jù)(count個字節(jié))全部傳輸?shù)紽ileChannel中。
transferTo()
transferTo()方法將數(shù)據(jù)從FileChannel傳輸?shù)狡渌腸hannel中艾疟。
下面是一個簡單的例子:
RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");
FileChannel fromChannel = fromFile.getChannel();
RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
FileChannel toChannel = toFile.getChannel();
long position = 0;
long count = fromChannel.size();
fromChannel.transferTo(position, count, toChannel);
是不是發(fā)現(xiàn)這個例子和前面那個例子特別相似来吩?除了調(diào)用方法的FileChannel對象不一樣外,其他的都一樣蔽莱。
上面所說的關(guān)于SocketChannel的問題在transferTo()方法中同樣存在弟疆。SocketChannel會一直傳輸數(shù)據(jù)直到目標(biāo)buffer被填滿。
6盗冷、Selector
Selector(選擇器)是Java NIO中能夠檢測一到多個NIO通道怠苔,并能夠知曉通道是否為諸如讀寫事件做好準(zhǔn)備的組件。這樣仪糖,一個單獨(dú)的線程可以管理多個channel柑司,從而管理多個網(wǎng)絡(luò)連接。
為什么使用Selector?
僅用單個線程來處理多個Channels的好處是锅劝,只需要更少的線程來處理通道攒驰。事實(shí)上,可以只用一個線程處理所有的通道故爵。對于操作系統(tǒng)來說玻粪,線程之間上下文切換的開銷很大,而且每個線程都要占用系統(tǒng)的一些資源(如內(nèi)存)诬垂。因此劲室,使用的線程越少越好。
但是剥纷,需要記住,現(xiàn)代的操作系統(tǒng)和CPU在多任務(wù)方面表現(xiàn)的越來越好呢铆,所以多線程的開銷隨著時間的推移晦鞋,變得越來越小了。實(shí)際上棺克,如果一個CPU有多個內(nèi)核悠垛,不使用多任務(wù)可能是在浪費(fèi)CPU能力。不管怎么說娜谊,關(guān)于那種設(shè)計(jì)的討論應(yīng)該放在另一篇不同的文章中确买。在這里,只要知道使用Selector能夠處理多個通道就足夠了纱皆。
下面是單線程使用一個Selector處理3個channel的示例圖:
<figure style="margin: 1em 0px; color: rgb(26, 26, 26); font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"></figure>
Selector的創(chuàng)建
通過調(diào)用Selector.open()方法創(chuàng)建一個Selector湾趾,如下:
Selector selector = Selector.open();
向Selector注冊通道
為了將Channel和Selector配合使用芭商,必須將channel注冊到selector上。通過SelectableChannel.register()方法來實(shí)現(xiàn)搀缠,如下:
channel.configureBlocking(false);SelectionKey key = channel.register(selector,
Selectionkey.OP_READ);
與Selector一起使用時铛楣,Channel必須處于非阻塞模式下。這意味著不能將FileChannel與Selector一起使用艺普,因?yàn)镕ileChannel不能切換到非阻塞模式簸州。而套接字通道都可以。
注意register()方法的第二個參數(shù)歧譬。這是一個“interest集合”岸浑,意思是在通過Selector監(jiān)聽Channel時對什么事件感興趣」宀剑可以監(jiān)聽四種不同類型的事件:
- Connect
- Accept
- Read
- Write
通道觸發(fā)了一個事件意思是該事件已經(jīng)就緒矢洲。所以,某個channel成功連接到另一個服務(wù)器稱為“連接就緒”面氓。一個server socket channel準(zhǔn)備好接收新進(jìn)入的連接稱為“接收就緒”兵钮。一個有數(shù)據(jù)可讀的通道可以說是“讀就緒”。等待寫數(shù)據(jù)的通道可以說是“寫就緒”舌界。
這四種事件用SelectionKey的四個常量來表示:
- SelectionKey.OP_CONNECT
- SelectionKey.OP_ACCEPT
- SelectionKey.OP_READ
- SelectionKey.OP_WRITE
如果你對不止一種事件感興趣掘譬,那么可以用“位或”操作符將常量連接起來,如下:
int interestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;
在下面還會繼續(xù)提到interest集合呻拌。
SelectionKey
在上一小節(jié)中葱轩,當(dāng)向Selector注冊Channel時,register()方法會返回一個SelectionKey對象藐握。這個對象包含了一些你感興趣的屬性:
- interest集合
- ready集合
- Channel
- Selector
附加的對象(可選)
下面我會描述這些屬性靴拱。
interest集合
就像向Selector注冊通道一節(jié)中所描述的,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 集合中溜在。
ready集合
ready 集合是通道已經(jīng)準(zhǔn)備就緒的操作的集合陌知。在一次選擇(Selection)之后,你會首先訪問這個ready set掖肋。Selection將在下一小節(jié)進(jìn)行解釋仆葡。可以這樣訪問ready集合:
int readySet = selectionKey.readyOps();
可以用像檢測interest集合那樣的方法志笼,來檢測channel中什么事件或操作已經(jīng)就緒沿盅。但是把篓,也可以使用以下四個方法,它們都會返回一個布爾類型:
selectionKey.isAcceptable();selectionKey.isConnectable();
selectionKey.isReadable();selectionKey.isWritable();
Channel + Selector
從SelectionKey訪問Channel和Selector很簡單嗡呼。如下:
Channel channel = selectionKey.channel();Selector selector = selectionKey.selector();
附加的對象
可以將一個對象或者更多信息附著到SelectionKey上纸俭,這樣就能方便的識別某個給定的通道。例如南窗,可以附加 與通道一起使用的Buffer揍很,或是包含聚集數(shù)據(jù)的某個對象。使用方法如下:
selectionKey.attach(theObject);Object attachedObj = selectionKey.attachment();
還可以在用register()方法向Selector注冊Channel的時候附加對象万伤。如:
SelectionKey key = channel.register(selector, SelectionKey.OP_READ, theObject);
通過Selector選擇通道
一旦向Selector注冊了一或多個通道窒悔,就可以調(diào)用幾個重載的select()方法。這些方法返回你所感興趣的事件(如連接敌买、接受简珠、讀或?qū)懀┮呀?jīng)準(zhǔn)備就緒的那些通道。換句話說虹钮,如果你對“讀就緒”的通道感興趣聋庵,select()方法會返回讀事件已經(jīng)就緒的那些通道。
下面是select()方法:
- int select()
- int select(long timeout)
- int selectNow()
select()阻塞到至少有一個通道在你注冊的事件上就緒了芙粱。
select(long timeout)和select()一樣祭玉,除了最長會阻塞timeout毫秒(參數(shù))。
selectNow()不會阻塞春畔,不管什么通道就緒都立刻返回(譯者注:此方法執(zhí)行非阻塞的選擇操作脱货。如果自從前一次選擇操作后,沒有通道變成可選擇的律姨,則此方法直接返回零振峻。)。
select()方法返回的int值表示有多少通道已經(jīng)就緒择份。亦即扣孟,自上次調(diào)用select()方法后有多少通道變成就緒狀態(tài)。如果調(diào)用select()方法荣赶,因?yàn)橛幸粋€通道變成就緒狀態(tài)凤价,返回了1,若再次調(diào)用select()方法讯壶,如果另一個通道就緒了料仗,它會再次返回1湾盗。如果對第一個就緒的channel沒有做任何操作伏蚊,現(xiàn)在就有兩個就緒的通道,但在每次select()方法調(diào)用之間格粪,只有一個通道就緒了躏吊。
selectedKeys()
一旦調(diào)用了select()方法氛改,并且返回值表明有一個或更多個通道就緒了,然后可以通過調(diào)用selector的selectedKeys()方法比伏,訪問“已選擇鍵集(selected key set)”中的就緒通道胜卤。如下所示:
當(dāng)像Selector注冊Channel時,Channel.register()方法會返回一個SelectionKey 對象赁项。這個對象代表了注冊到該Selector的通道葛躏。可以通過SelectionKey的selectedKeySet()方法訪問這些對象悠菜。
可以遍歷這個已選擇的鍵集合來訪問就緒的通道舰攒。如下:
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();
}
這個循環(huán)遍歷已選擇鍵集中的每個鍵,并檢測各個鍵所對應(yīng)的通道的就緒事件悔醋。
注意每次迭代末尾的keyIterator.remove()調(diào)用摩窃。Selector不會自己從已選擇鍵集中移除SelectionKey實(shí)例。必須在處理完通道時自己移除芬骄。下次該通道變成就緒時猾愿,Selector會再次將其放入已選擇鍵集中。
SelectionKey.channel()方法返回的通道需要轉(zhuǎn)型成你要處理的類型账阻,如ServerSocketChannel或SocketChannel等蒂秘。
wakeUp()
某個線程調(diào)用select()方法后阻塞了,即使沒有通道已經(jīng)就緒宰僧,也有辦法讓其從select()方法返回材彪。只要讓其它線程在第一個線程調(diào)用select()方法的那個對象上調(diào)用Selector.wakeup()方法即可。阻塞在select()方法上的線程會立馬返回琴儿。
如果有其它線程調(diào)用了wakeup()方法段化,但當(dāng)前沒有線程阻塞在select()方法上,下個調(diào)用select()方法的線程會立即“醒來(wake up)”造成。
close()
用完Selector后調(diào)用其close()方法會關(guān)閉該Selector显熏,且使注冊到該Selector上的所有SelectionKey實(shí)例無效。通道本身并不會關(guān)閉晒屎。
完整的示例
這里有一個完整的示例喘蟆,打開一個Selector,注冊一個通道注冊到這個Selector上(通道的初始化過程略去),然后持續(xù)監(jiān)控這個Selector的四種事件(接受鼓鲁,連接蕴轨,讀,寫)是否就緒骇吭。
Selector selector = Selector.open();
channel.configureBlocking(false);
SelectionKey key = channel.register(selector, SelectionKey.OP_READ);while(true) { int readyChannels = selector.select(); if(readyChannels == 0) continue;
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();
}
}
7橙弱、FileChannel
Java NIO中的FileChannel是一個連接到文件的通道。可以通過文件通道讀寫文件棘脐。
FileChannel無法設(shè)置為非阻塞模式斜筐,它總是運(yùn)行在阻塞模式下。
打開FileChannel
在使用FileChannel之前蛀缝,必須先打開它顷链。但是,我們無法直接打開一個FileChannel屈梁,需要通過使用一個InputStream嗤练、OutputStream或RandomAccessFile來獲取一個FileChannel實(shí)例。下面是通過RandomAccessFile打開FileChannel的示例:
RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel inChannel = aFile.getChannel();
從FileChannel讀取數(shù)據(jù)
調(diào)用多個read()方法之一從FileChannel中讀取數(shù)據(jù)在讶。如:
ByteBuffer buf = ByteBuffer.allocate(48);int bytesRead = inChannel.read(buf);
首先潭苞,分配一個Buffer。從FileChannel中讀取的數(shù)據(jù)將被讀到Buffer中真朗。
然后此疹,調(diào)用FileChannel.read()方法。該方法將數(shù)據(jù)從FileChannel讀取到Buffer中遮婶。read()方法返回的int值表示了有多少字節(jié)被讀到了Buffer中蝗碎。如果返回-1,表示到了文件末尾旗扑。
向FileChannel寫數(shù)據(jù)
使用FileChannel.write()方法向FileChannel寫數(shù)據(jù)蹦骑,該方法的參數(shù)是一個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循環(huán)中調(diào)用的臀防。因?yàn)闊o法保證write()方法一次能向FileChannel寫入多少字節(jié)眠菇,因此需要重復(fù)調(diào)用write()方法,直到Buffer中已經(jīng)沒有尚未寫入通道的字節(jié)袱衷。
關(guān)閉FileChannel
用完FileChannel后必須將其關(guān)閉捎废。如:
channel.close();
FileChannel的position方法
有時可能需要在FileChannel的某個特定位置進(jìn)行數(shù)據(jù)的讀/寫操作≈略铮可以通過調(diào)用position()方法獲取FileChannel的當(dāng)前位置登疗。
也可以通過調(diào)用position(long pos)方法設(shè)置FileChannel的當(dāng)前位置。
這里有兩個例子:
long pos = channel.position();
channel.position(pos +123);
如果將位置設(shè)置在文件結(jié)束符之后嫌蚤,然后試圖從文件通道中讀取數(shù)據(jù)辐益,讀方法將返回-1 —— 文件結(jié)束標(biāo)志。
如果將位置設(shè)置在文件結(jié)束符之后脱吱,然后向通道中寫數(shù)據(jù)智政,文件將撐大到當(dāng)前位置并寫入數(shù)據(jù)。這可能導(dǎo)致“文件空洞”箱蝠,磁盤上物理文件中寫入的數(shù)據(jù)間有空隙续捂。
FileChannel的size方法
FileChannel實(shí)例的size()方法將返回該實(shí)例所關(guān)聯(lián)文件的大小猜年。如:
long fileSize = channel.size();
FileChannel的truncate方法
可以使用FileChannel.truncate()方法截取一個文件。截取文件時疾忍,文件將中指定長度后面的部分將被刪除。如:
channel.truncate(1024);
這個例子截取文件的前1024個字節(jié)床三。
FileChannel的force方法
FileChannel.force()方法將通道里尚未寫入磁盤的數(shù)據(jù)強(qiáng)制寫到磁盤上一罩。出于性能方面的考慮,操作系統(tǒng)會將數(shù)據(jù)緩存在內(nèi)存中撇簿,所以無法保證寫入到FileChannel里的數(shù)據(jù)一定會即時寫到磁盤上。要保證這一點(diǎn),需要調(diào)用force()方法忍抽。
force()方法有一個boolean類型的參數(shù)因妇,指明是否同時將文件元數(shù)據(jù)(權(quán)限信息等)寫到磁盤上。
下面的例子同時將文件數(shù)據(jù)和元數(shù)據(jù)強(qiáng)制寫到磁盤上:
查看源代碼打印幫助
channel.force(true);
8找蜜、SocketChannel
Java NIO中的SocketChannel是一個連接到TCP網(wǎng)絡(luò)套接字的通道饼暑。可以通過以下2種方式創(chuàng)建SocketChannel:
打開一個SocketChannel并連接到互聯(lián)網(wǎng)上的某臺服務(wù)器洗做。
一個新連接到達(dá)ServerSocketChannel時弓叛,會創(chuàng)建一個SocketChannel。
打開 SocketChannel
下面是SocketChannel的打開方式:
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("http://jenkov.com", 80));
關(guān)閉 SocketChannel
當(dāng)用完SocketChannel之后調(diào)用SocketChannel.close()關(guān)閉SocketChannel:
socketChannel.close();
從 SocketChannel 讀取數(shù)據(jù)
要從SocketChannel中讀取數(shù)據(jù)诚纸,調(diào)用一個read()的方法之一撰筷。以下是例子:
ByteBuffer buf = ByteBuffer.allocate(48);int bytesRead = socketChannel.read(buf);
首先,分配一個Buffer畦徘。從SocketChannel讀取到的數(shù)據(jù)將會放到這個Buffer中毕籽。
然后,調(diào)用SocketChannel.read()井辆。該方法將數(shù)據(jù)從SocketChannel 讀到Buffer中关筒。read()方法返回的int值表示讀了多少字節(jié)進(jìn)Buffer里。如果返回的是-1杯缺,表示已經(jīng)讀到了流的末尾(連接關(guān)閉了)平委。
寫入 SocketChannel
寫數(shù)據(jù)到SocketChannel用的是SocketChannel.write()方法,該方法以一個Buffer作為參數(shù)夺谁。示例如下:
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()方法的調(diào)用是在一個while循環(huán)中的廉赔。Write()方法無法保證能寫多少字節(jié)到SocketChannel。所以匾鸥,我們重復(fù)調(diào)用write()直到Buffer沒有要寫的字節(jié)為止蜡塌。
非阻塞模式
可以設(shè)置 SocketChannel 為非阻塞模式(non-blocking mode).設(shè)置之后,就可以在異步模式下調(diào)用connect(), read() 和write()了勿负。
connect()
如果SocketChannel在非阻塞模式下馏艾,此時調(diào)用connect()劳曹,該方法可能在連接建立之前就返回了。為了確定連接是否建立琅摩,可以調(diào)用finishConnect()的方法铁孵。像這樣:
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("http://jenkov.com", 80));while(! socketChannel.finishConnect() ){ //wait, or do something else...
}
write()
非阻塞模式下,write()方法在尚未寫出任何內(nèi)容時可能就返回了房资。所以需要在循環(huán)中調(diào)用write()蜕劝。前面已經(jīng)有例子了,這里就不贅述了轰异。
read()
非阻塞模式下,read()方法在尚未讀取到任何數(shù)據(jù)時可能就返回了岖沛。所以需要關(guān)注它的int返回值,它會告訴你讀取了多少字節(jié)搭独。
非阻塞模式與選擇器
非阻塞模式與選擇器搭配會工作的更好婴削,通過將一或多個SocketChannel注冊到Selector,可以詢問選擇器哪個通道已經(jīng)準(zhǔn)備好了讀取牙肝,寫入等唉俗。Selector與SocketChannel的搭配使用會在后面詳講。
9配椭、ServerSocketChannel
Java NIO中的 ServerSocketChannel 是一個可以監(jiān)聽新進(jìn)來的TCP連接的通道, 就像標(biāo)準(zhǔn)IO中的ServerSocket一樣互躬。ServerSocketChannel類在 java.nio.channels包中。
這里有個例子:
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(9999));while(true){
SocketChannel socketChannel =
serverSocketChannel.accept(); //do something with socketChannel...}
關(guān)閉 ServerSocketChannel
通過調(diào)用ServerSocketChannel.close() 方法來關(guān)閉ServerSocketChannel. 如:
serverSocketChannel.close();
監(jiān)聽新進(jìn)來的連接
通過 ServerSocketChannel.accept() 方法監(jiān)聽新進(jìn)來的連接颂郎。當(dāng) accept()方法返回的時候,它返回一個包含新進(jìn)來的連接的 SocketChannel吼渡。因此, accept()方法會一直阻塞到有新連接到達(dá)。
通常不會僅僅只監(jiān)聽一個連接,在while循環(huán)中調(diào)用 accept()方法. 如下面的例子:
while(true){
SocketChannel socketChannel =
serverSocketChannel.accept(); //do something with socketChannel...
}
ServerSocketChannel可以設(shè)置成非阻塞模式乓序。在非阻塞模式下寺酪,accept() 方法會立刻返回,如果還沒有新進(jìn)來的連接,返回的將是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 socketChannel...
}
}
10、Java NIO DatagramChannel
Java NIO中的DatagramChannel是一個能收發(fā)UDP包的通道陨献。因?yàn)閁DP是無連接的網(wǎng)絡(luò)協(xié)議盒犹,所以不能像其它通道那樣讀取和寫入。它發(fā)送和接收的是數(shù)據(jù)包眨业。
打開 DatagramChannel
下面是 DatagramChannel 的打開方式:
DatagramChannel channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(9999));
這個例子打開的 DatagramChannel可以在UDP端口9999上接收數(shù)據(jù)包急膀。
接收數(shù)據(jù)
通過receive()方法從DatagramChannel接收數(shù)據(jù),如:
ByteBuffer buf = ByteBuffer.allocate(48);buf.clear();channel.receive(buf);
receive()方法會將接收到的數(shù)據(jù)包內(nèi)容復(fù)制到指定的Buffer. 如果Buffer容不下收到的數(shù)據(jù)龄捡,多出的數(shù)據(jù)將被丟棄卓嫂。
發(fā)送數(shù)據(jù)
通過send()方法從DatagramChannel發(fā)送數(shù)據(jù),如:
String newData = "New String to write to file..." + System.currentTimeMillis();ByteBuffer buf = ByteBuffer.allocate(48);buf.clear();buf.put(newData.getBytes());buf.flip();int bytesSent = channel.send(buf, new InetSocketAddress("jenkov.com", 80));
這個例子發(fā)送一串字符到”jenkov.com”服務(wù)器的UDP端口80聘殖。 因?yàn)榉?wù)端并沒有監(jiān)控這個端口晨雳,所以什么也不會發(fā)生行瑞。也不會通知你發(fā)出的數(shù)據(jù)包是否已收到,因?yàn)閁DP在數(shù)據(jù)傳送方面沒有任何保證餐禁。
連接到特定的地址
可以將DatagramChannel“連接”到網(wǎng)絡(luò)中的特定地址的血久。由于UDP是無連接的,連接到特定地址并不會像TCP通道那樣創(chuàng)建一個真正的連接帮非。而是鎖住DatagramChannel 氧吐,讓其只能從特定地址收發(fā)數(shù)據(jù)。
這里有個例子:
channel.connect(new InetSocketAddress("jenkov.com", 80));
當(dāng)連接后喜鼓,也可以使用read()和write()方法,就像在用傳統(tǒng)的通道一樣衔肢。只是在數(shù)據(jù)傳送方面沒有任何保證庄岖。這里有幾個例子:
int bytesRead = channel.read(buf);int bytesWritten = channel.write(but);
11、Pipe
Java NIO 管道是2個線程之間的單向數(shù)據(jù)連接角骤。Pipe有一個source通道和一個sink通道隅忿。數(shù)據(jù)會被寫到sink通道,從source通道讀取邦尊。
這里是Pipe原理的圖示:
<figure style="margin: 1em 0px; color: rgb(26, 26, 26); font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"></figure>
創(chuàng)建管道
通過Pipe.open()方法打開管道背桐。例如:
Pipe pipe = Pipe.open();
向管道寫數(shù)據(jù)
要向管道寫數(shù)據(jù),需要訪問sink通道蝉揍。像這樣:
Pipe.SinkChannel sinkChannel = pipe.sink();
通過調(diào)用SinkChannel的write()方法链峭,將數(shù)據(jù)寫入SinkChannel,像這樣:
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()) {
sinkChannel.write(buf);}
從管道讀取數(shù)據(jù)
從讀取管道的數(shù)據(jù),需要訪問source通道又沾,像這樣:
Pipe.SourceChannel sourceChannel = pipe.source();
調(diào)用source通道的read()方法來讀取數(shù)據(jù)弊仪,像這樣:
ByteBuffer buf = ByteBuffer.allocate(48);int bytesRead = sourceChannel.read(buf);
12、Java NIO與IO的對比
當(dāng)學(xué)習(xí)了Java NIO和IO的API后杖刷,一個問題馬上涌入腦海:
我應(yīng)該何時使用IO励饵,何時使用NIO呢?在本文中滑燃,我會盡量清晰地解析Java NIO和IO的差異役听、它們的使用場景,以及它們?nèi)绾斡绊懩拇a設(shè)計(jì)表窘。
Java NIO和IO的主要區(qū)別
下表總結(jié)了Java NIO和IO之間的主要差別典予,我會更詳細(xì)地描述表中每部分的差異。
IONIOStream orientedBuffer orientedBlocking IONon blocking IO無Selectors
面向流與面向緩沖
Java NIO和IO之間第一個最大的區(qū)別是乐严,IO是面向流的熙参,NIO是面向緩沖區(qū)的。
Java IO面向流意味著每次從流中讀一個或多個字節(jié)麦备,直至讀取所有字節(jié)孽椰,它們沒有被緩存在任何地方昭娩。此外,它不能前后移動流中的數(shù)據(jù)黍匾。如果需要前后移動從流中讀取的數(shù)據(jù)栏渺,需要先將它緩存到一個緩沖區(qū)。
Java NIO的緩沖導(dǎo)向方法略有不同锐涯。數(shù)據(jù)讀取到一個它稍后處理的緩沖區(qū)磕诊,需要時可在緩沖區(qū)中前后移動。這就增加了處理過程中的靈活性纹腌。但是霎终,還需要檢查是否該緩沖區(qū)中包含所有您需要處理的數(shù)據(jù)。而且升薯,需確保當(dāng)更多的數(shù)據(jù)讀入緩沖區(qū)時莱褒,不要覆蓋緩沖區(qū)里尚未處理的數(shù)據(jù)。
阻塞與非阻塞IO
Java IO的各種流是阻塞的涎劈。這意味著广凸,當(dāng)一個線程調(diào)用read() 或 write()時,該線程被阻塞蛛枚,直到有一些數(shù)據(jù)被讀取谅海,或數(shù)據(jù)完全寫入。該線程在此期間不能再干任何事情了蹦浦。
Java NIO的非阻塞模式扭吁,使一個線程從某通道發(fā)送請求讀取數(shù)據(jù),但是它僅能得到目前可用的數(shù)據(jù)盲镶,如果目前沒有數(shù)據(jù)可用時智末,就什么都不會獲取。而不是保持線程阻塞徒河,所以直至數(shù)據(jù)變的可以讀取之前系馆,該線程可以繼續(xù)做其他的事情。 非阻塞寫也是如此顽照。一個線程請求寫入一些數(shù)據(jù)到某通道由蘑,但不需要等待它完全寫入,這個線程同時可以去做別的事情代兵。 線程通常將非阻塞IO的空閑時間用于在其它通道上執(zhí)行IO操作尼酿,所以一個單獨(dú)的線程現(xiàn)在可以管理多個輸入和輸出通道(channel)。
選擇器(Selectors)
Java NIO的選擇器允許一個單獨(dú)的線程來監(jiān)視多個輸入通道植影,你可以注冊多個通道使用一個選擇器裳擎,然后使用一個單獨(dú)的線程來“選擇”通道:這些通道里已經(jīng)有可以處理的輸入,或者選擇已準(zhǔn)備寫入的通道思币。這種選擇機(jī)制鹿响,使得一個單獨(dú)的線程很容易來管理多個通道羡微。
NIO和IO如何影響應(yīng)用程序的設(shè)計(jì)
無論您選擇IO或NIO工具箱,可能會影響您應(yīng)用程序設(shè)計(jì)的以下幾個方面:
- 對NIO或IO類的API調(diào)用惶我。
- 數(shù)據(jù)處理妈倔。
- 用來處理數(shù)據(jù)的線程數(shù)。
API調(diào)用
當(dāng)然绸贡,使用NIO的API調(diào)用時看起來與使用IO時有所不同盯蝴,但這并不意外,因?yàn)椴⒉皇莾H從一個InputStream逐字節(jié)讀取听怕,而是數(shù)據(jù)必須先讀入緩沖區(qū)再處理捧挺。
數(shù)據(jù)處理
使用純粹的NIO設(shè)計(jì)相較IO設(shè)計(jì),數(shù)據(jù)處理也受到影響尿瞭。
在IO設(shè)計(jì)中闽烙,我們從InputStream或 Reader逐字節(jié)讀取數(shù)據(jù)。假設(shè)你正在處理一基于行的文本數(shù)據(jù)流筷厘,例如:
Name: AnnaAge: 25Email: anna@mailserver.comPhone: 1234567890
該文本行的流可以這樣處理:
InputStream input = … ; // get the InputStream from the client socket BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String nameLine = reader.readLine(); String ageLine = reader.readLine(); String emailLine = reader.readLine(); String phoneLine = reader.readLine();
請注意處理狀態(tài)由程序執(zhí)行多久決定鸣峭。換句話說宏所,一旦reader.readLine()方法返回酥艳,你就知道肯定文本行就已讀完, readline()阻塞直到整行讀完爬骤,這就是原因充石。你也知道此行包含名稱;同樣霞玄,第二個readline()調(diào)用返回的時候骤铃,你知道這行包含年齡等。 正如你可以看到坷剧,該處理程序僅在有新數(shù)據(jù)讀入時運(yùn)行惰爬,并知道每步的數(shù)據(jù)是什么。一旦正在運(yùn)行的線程已處理過讀入的某些數(shù)據(jù)惫企,該線程不會再回退數(shù)據(jù)(大多如此)撕瞧。下圖也說明了這條原則:
<figure style="margin: 1em 0px; color: rgb(26, 26, 26); font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"></figure>
上圖:從一個阻塞的流中讀數(shù)據(jù)
而一個NIO的實(shí)現(xiàn)會有所不同,下面是一個簡單的例子:
ByteBuffer buffer = ByteBuffer.allocate(48);int bytesRead = inChannel.read(buffer);
注意第二行狞尔,從通道讀取字節(jié)到ByteBuffer丛版。當(dāng)這個方法調(diào)用返回時,你不知道你所需的所有數(shù)據(jù)是否在緩沖區(qū)內(nèi)偏序。你所知道的是页畦,該緩沖區(qū)包含一些字節(jié),這使得處理有點(diǎn)困難研儒。
假設(shè)第一次 read(buffer)調(diào)用后豫缨,讀入緩沖區(qū)的數(shù)據(jù)只有半行独令,例如,“Name:An”州胳,你能處理數(shù)據(jù)嗎记焊?顯然不能,需要等待栓撞,直到整行數(shù)據(jù)讀入緩存遍膜,在此之前,對數(shù)據(jù)的任何處理毫無意義瓤湘。
所以瓢颅,你怎么知道是否該緩沖區(qū)包含足夠的數(shù)據(jù)可以處理呢?好了弛说,你不知道挽懦。發(fā)現(xiàn)的方法只能查看緩沖區(qū)中的數(shù)據(jù)。其結(jié)果是木人,在你知道所有數(shù)據(jù)都在緩沖區(qū)里之前信柿,你必須檢查幾次緩沖區(qū)的數(shù)據(jù)。這不僅效率低下醒第,而且可以使程序設(shè)計(jì)方案雜亂不堪渔嚷。例如:
ByteBuffer buffer = ByteBuffer.allocate(48); int bytesRead = inChannel.read(buffer); while(! bufferFull(bytesRead) ) {
bytesRead = inChannel.read(buffer);
}
bufferFull()方法必須跟蹤有多少數(shù)據(jù)讀入緩沖區(qū),并返回真或假稠曼,這取決于緩沖區(qū)是否已滿形病。換句話說,如果緩沖區(qū)準(zhǔn)備好被處理霞幅,那么表示緩沖區(qū)滿了漠吻。
bufferFull()方法掃描緩沖區(qū),但必須保持在bufferFull()方法被調(diào)用之前狀態(tài)相同司恳。如果沒有途乃,下一個讀入緩沖區(qū)的數(shù)據(jù)可能無法讀到正確的位置。這是不可能的扔傅,但卻是需要注意的又一問題耍共。
如果緩沖區(qū)已滿,它可以被處理铅鲤。如果它不滿划提,并且在你的實(shí)際案例中有意義,你或許能處理其中的部分?jǐn)?shù)據(jù)邢享。但是許多情況下并非如此鹏往。下圖展示了“緩沖區(qū)數(shù)據(jù)循環(huán)就緒”:
<figure style="margin: 1em 0px; color: rgb(26, 26, 26); font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"></figure>
上圖:從一個通道里讀數(shù)據(jù),直到所有的數(shù)據(jù)都讀到緩沖區(qū)里
總結(jié)
NIO可讓您只使用一個(或幾個)單線程管理多個通道(網(wǎng)絡(luò)連接或文件),但付出的代價(jià)是解析數(shù)據(jù)可能會比從一個阻塞流中讀取數(shù)據(jù)更復(fù)雜伊履。
如果需要管理同時打開的成千上萬個連接韩容,這些連接每次只是發(fā)送少量的數(shù)據(jù),例如聊天服務(wù)器唐瀑,實(shí)現(xiàn)NIO的服務(wù)器可能是一個優(yōu)勢群凶。同樣,如果你需要維持許多打開的連接到其他計(jì)算機(jī)上哄辣,如P2P網(wǎng)絡(luò)中请梢,使用一個單獨(dú)的線程來管理你所有出站連接,可能是一個優(yōu)勢力穗。一個線程多個連接的設(shè)計(jì)方案如下圖所示:
<figure style="margin: 1em 0px; color: rgb(26, 26, 26); font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"></figure>
上圖:單線程管理多個連接
如果你有少量的連接使用非常高的帶寬毅弧,一次發(fā)送大量的數(shù)據(jù),也許典型的IO服務(wù)器實(shí)現(xiàn)可能非常契合当窗。下圖說明了一個典型的IO服務(wù)器設(shè)計(jì):
<figure style="margin: 1em 0px; color: rgb(26, 26, 26); font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", sans-serif; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"></figure>
上圖:一個典型的IO服務(wù)器設(shè)計(jì):一個連接通過一個線程處理
更多好文章請關(guān)注公號“編碼前線”