如今使用NIO的場景愈來愈多,不少網上的技術框架或多或少的使用NIO技術,譬如Tomcat,Jetty。學習和掌握NIO技術已經不是一個JAVA攻城獅的加分技能,而是一個必備技能。再者,如今互聯網的面試中上點level的都會涉及一下NIO或者AIO的問題(AIO下次再講述,本篇主要講述NIO),掌握好NIO也能幫助你得到一份較好的offer。 驅使博主寫這篇文章的關鍵是網上關於NIO的文章並非不少,並且案例較少,針對這個特性,本文主要經過實際案例主要講述NIO的用法,每一個案例都通過實際檢驗。博主經過本身的理解以及一些案例但願能給各位在學習NIO之時多一份參考。博主能力有限,文中有不足之處歡迎之處。面試
NIO主要有三大核心部分:Channel(通道),Buffer(緩衝區), Selector。傳統IO基於字節流和字符流進行操做,而NIO基於Channel和Buffer(緩衝區)進行操做,數據老是從通道讀取到緩衝區中,或者從緩衝區寫入到通道中。Selector(選擇區)用於監聽多個通道的事件(好比:鏈接打開,數據到達)。所以,單個線程能夠監聽多個數據通道。數組
NIO和傳統IO(一下簡稱IO)之間第一個最大的區別是,IO是面向流的,NIO是面向緩衝區的。 Java IO面向流意味着每次從流中讀一個或多個字節,直至讀取全部字節,它們沒有被緩存在任何地方。此外,它不能先後移動流中的數據。若是須要先後移動從流中讀取的數據,須要先將它緩存到一個緩衝區。NIO的緩衝導向方法略有不一樣。數據讀取到一個它稍後處理的緩衝區,須要時可在緩衝區中先後移動。這就增長了處理過程當中的靈活性。可是,還須要檢查是否該緩衝區中包含全部您須要處理的數據。並且,需確保當更多的數據讀入緩衝區時,不要覆蓋緩衝區裏還沒有處理的數據。緩存
IO的各類流是阻塞的。這意味着,當一個線程調用read() 或 write()時,該線程被阻塞,直到有一些數據被讀取,或數據徹底寫入。該線程在此期間不能再幹任何事情了。 NIO的非阻塞模式,使一個線程從某通道發送請求讀取數據,可是它僅能獲得目前可用的數據,若是目前沒有數據可用時,就什麼都不會獲取。而不是保持線程阻塞,因此直至數據變的能夠讀取以前,該線程能夠繼續作其餘的事情。 非阻塞寫也是如此。一個線程請求寫入一些數據到某通道,但不須要等待它徹底寫入,這個線程同時能夠去作別的事情。 線程一般將非阻塞IO的空閒時間用於在其它通道上執行IO操做,因此一個單獨的線程如今能夠管理多個輸入和輸出通道(channel)。服務器
首先說一下Channel,國內大多翻譯成「通道」。Channel和IO中的Stream(流)是差很少一個等級的。只不過Stream是單向的,譬如:InputStream, OutputStream.而Channel是雙向的,既能夠用來進行讀操做,又能夠用來進行寫操做。
NIO中的Channel的主要實現有:markdown
這裏看名字就能夠猜出個因此然來:分別能夠對應文件IO、UDP和TCP(Server和Client)。下面演示的案例基本上就是圍繞這4個類型的Channel進行陳述的。網絡
NIO中的關鍵Buffer實現有:ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer,分別對應基本數據類型: byte, char, double, float, int, long, short。固然NIO中還有MappedByteBuffer, HeapByteBuffer, DirectByteBuffer等這裏先不進行陳述。併發
Selector運行單線程處理多個Channel,若是你的應用打開了多個通道,但每一個鏈接的流量都很低,使用Selector就會很方便。例如在一個聊天服務器中。要使用Selector, 得向Selector註冊Channel,而後調用它的select()方法。這個方法會一直阻塞到某個註冊的通道有事件就緒。一旦這個方法返回,線程就能夠處理這些事件,事件的例子有如新的鏈接進來、數據接收等。app
看完上面的陳述,對於第一次接觸NIO的同窗來講雲裏霧裏,只說了一些概念,也沒記住什麼,更別說怎麼用了。這裏開始經過傳統IO以及更改後的NIO來作對比,以更形象的突出NIO的用法,進而使你對NIO有一點點的瞭解。框架
首先,案例1是採用FileInputStream讀取文件內容的:
public static void method2(){
InputStream in = null;
try{
in = new BufferedInputStream(new FileInputStream("src/nomal_io.txt"));
byte [] buf = new byte[1024];
int bytesRead = in.read(buf);
while(bytesRead != -1)
{
for(int i=0;i<bytesRead;i++)
System.out.print((char)buf[i]);
bytesRead = in.read(buf);
}
}catch (IOException e)
{
e.printStackTrace();
}finally{
try{
if(in != null){
in.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
1234567891011121314151617181920212223242526
複製代碼
輸出結果:(略)
案例是對應的NIO(這裏經過RandomAccessFile進行操做,固然也能夠經過FileInputStream.getChannel()進行操做):
public static void method1(){
RandomAccessFile aFile = null;
try{
aFile = new RandomAccessFile("src/nio.txt","rw");
FileChannel fileChannel = aFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
int bytesRead = fileChannel.read(buf);
System.out.println(bytesRead);
while(bytesRead != -1)
{
buf.flip();
while(buf.hasRemaining())
{
System.out.print((char)buf.get());
}
buf.compact();
bytesRead = fileChannel.read(buf);
}
}catch (IOException e){
e.printStackTrace();
}finally{
try{
if(aFile != null){
aFile.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
123456789101112131415161718192021222324252627282930313233
複製代碼
輸出結果:(略)
經過仔細對比案例1和案例2,應該能看出個大概,最起碼能發現NIO的實現方式比叫複雜。有了一個大概的印象能夠進入下一步了。
從案例2中能夠總結出使用Buffer通常遵循下面幾個步驟:
Buffer顧名思義:緩衝區,其實是一個容器,一個連續數組。Channel提供從文件、網絡讀取數據的渠道,可是讀寫的數據都必須通過Buffer。以下圖:
向Buffer中寫數據:
從Buffer中讀取數據:
能夠把Buffer簡單地理解爲一組基本數據類型的元素列表,它經過幾個變量來保存這個數據的當前位置狀態:capacity, position, limit, mark:
索引
說明
capacity
緩衝區數組的總長度
position
下一個要操做的數據元素的位置
limit
緩衝區數組中不可操做的下一個元素的位置:limit<=capacity
mark
用於記錄當前position的前一個位置或者默認是-1
無圖無真相,舉例:咱們經過ByteBuffer.allocate(11)方法建立了一個11個byte的數組的緩衝區,初始狀態如上圖,position的位置爲0,capacity和limit默認都是數組長度。當咱們寫入5個字節時,變化以下圖:
這時咱們須要將緩衝區中的5個字節數據寫入Channel的通訊信道,因此咱們調用ByteBuffer.flip()方法,變化以下圖所示(position設回0,並將limit設成以前的position的值):
這時底層操做系統就能夠從緩衝區中正確讀取這個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中讀取多少個元素。
說完了FileChannel和Buffer, 你們應該對Buffer的用法比較瞭解了,這裏使用SocketChannel來繼續探討NIO。NIO的強大功能部分來自於Channel的非阻塞特性,套接字的某些操做可能會無限期地阻塞。例如,對accept()方法的調用可能會由於等待一個客戶端鏈接而阻塞;對read()方法的調用可能會由於沒有數據可讀而阻塞,直到鏈接的另外一端傳來新的數據。總的來講,建立/接收鏈接或讀寫數據等I/O調用,均可能無限期地阻塞等待,直到底層的網絡實現發生了什麼。慢速的,有損耗的網絡,或僅僅是簡單的網絡故障均可能致使任意時間的延遲。然而不幸的是,在調用一個方法以前沒法知道其是否阻塞。NIO的channel抽象的一個重要特徵就是能夠經過配置它的阻塞行爲,以實現非阻塞式的信道。
channel.configureBlocking(false)
1
複製代碼
在非阻塞式信道上調用一個方法老是會當即返回。這種調用的返回值指示了所請求的操做完成的程度。例如,在一個非阻塞式ServerSocketChannel上調用accept()方法,若是有鏈接請求來了,則返回客戶端SocketChannel,不然返回null。
這裏先舉一個TCP應用案例,客戶端採用NIO實現,而服務端依舊使用IO實現。
客戶端代碼(案例3):
public static void client(){
ByteBuffer buffer = ByteBuffer.allocate(1024);
SocketChannel socketChannel = null;
try
{
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("10.10.195.115",8080));
if(socketChannel.finishConnect())
{
int i=0;
while(true)
{
TimeUnit.SECONDS.sleep(1);
String info = "I'm "+i+++"-th information from client";
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();
}
}
}
12345678910111213141516171819202122232425262728293031323334353637383940
複製代碼
服務端代碼(案例4):
public 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 clntSocket = serverSocket.accept();
SocketAddress clientAddress = clntSocket.getRemoteSocketAddress();
System.out.println("Handling client at "+clientAddress);
in = clntSocket.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();
}
}
}
12345678910111213141516171819202122232425262728293031323334353637
複製代碼
輸出結果:(略)
根據案例分析,總結一下SocketChannel的用法。
打開SocketChannel:
socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("10.10.195.115",8080));
12
複製代碼
關閉:
socketChannel.close();
1
複製代碼
讀取數據:
String info = "I'm "+i+++"-th information from client";
buffer.clear();
buffer.put(info.getBytes());
buffer.flip();
while(buffer.hasRemaining()){
System.out.println(buffer);
socketChannel.write(buffer);
}
12345678
複製代碼
注意SocketChannel.write()方法的調用是在一個while循環中的。Write()方法沒法保證能寫多少字節到SocketChannel。因此,咱們重複調用write()直到Buffer沒有要寫的字節爲止。
非阻塞模式下,read()方法在還沒有讀取到任何數據時可能就返回了。因此須要關注它的int返回值,它會告訴你讀取了多少字節。
到目前爲止,所舉的案例中都沒有涉及Selector。不要急,好東西要慢慢來。Selector類能夠用於避免使用阻塞式客戶端中很浪費資源的「忙等」方法。例如,考慮一個IM服務器。像QQ或者旺旺這樣的,可能有幾萬甚至幾千萬個客戶端同時鏈接到了服務器,但在任什麼時候刻都只是很是少許的消息。
須要讀取和分發。這就須要一種方法阻塞等待,直到至少有一個信道能夠進行I/O操做,並指出是哪一個信道。NIO的選擇器就實現了這樣的功能。一個Selector實例能夠同時檢查一組信道的I/O狀態。用專業術語來講,選擇器就是一個多路開關選擇器,由於一個選擇器可以管理多個信道上的I/O操做。然而若是用傳統的方式來處理這麼多客戶端,使用的方法是循環地一個一個地去檢查全部的客戶端是否有I/O操做,若是當前客戶端有I/O操做,則可能把當前客戶端扔給一個線程池去處理,若是沒有I/O操做則進行下一個輪詢,當全部的客戶端都輪詢過了又接着從頭開始輪詢;這種方法是很是笨並且也很是浪費資源,由於大部分客戶端是沒有I/O操做,咱們也要去檢查;而Selector就不同了,它在內部能夠同時管理多個I/O,當一個信道有I/O操做的時候,他會通知Selector,Selector就是記住這個信道有I/O操做,而且知道是何種I/O操做,是讀呢?是寫呢?仍是接受新的鏈接;因此若是使用Selector,它返回的結果只有兩種結果,一種是0,即在你調用的時刻沒有任何客戶端須要I/O操做,另外一種結果是一組須要I/O操做的客戶端,這時你就根本不須要再檢查了,由於它返回給你的確定是你想要的。這樣一種通知的方式比那種主動輪詢的方式要高效得多!
要使用選擇器(Selector),須要建立一個Selector實例(使用靜態工廠方法open())並將其註冊(register)到想要監控的信道上(注意,這要經過channel的方法實現,而不是使用selector的方法)。最後,調用選擇器的select()方法。該方法會阻塞等待,直到有一個或更多的信道準備好了I/O操做或等待超時。select()方法將返回可進行I/O操做的信道數量。如今,在一個單獨的線程中,經過調用select()方法就能檢查多個信道是否準備好進行I/O操做。若是通過一段時間後仍然沒有信道準備好,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();
}
public static void handleAccept(SelectionKey key) throws IOException{
ServerSocketChannel ssChannel = (ServerSocketChannel)key.channel();
SocketChannel sc = ssChannel.accept();
sc.configureBlocking(false);
sc.register(key.selector(), SelectionKey.OP_READ,ByteBuffer.allocateDirect(BUF_SIZE));
}
public static void handleRead(SelectionKey key) throws IOException{
SocketChannel sc = (SocketChannel)key.channel();
ByteBuffer buf = (ByteBuffer)key.attachment();
long bytesRead = sc.read(buf);
while(bytesRead>0){
buf.flip();
while(buf.hasRemaining()){
System.out.print((char)buf.get());
}
System.out.println();
buf.clear();
bytesRead = sc.read(buf);
}
if(bytesRead == -1){
sc.close();
}
}
public 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();
}
public 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();
}
}
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
複製代碼
下面來慢慢講解這段代碼。
打開ServerSocketChannel:
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
1
複製代碼
關閉ServerSocketChannel:
serverSocketChannel.close();
1
複製代碼
監聽新進來的鏈接:
while(true){
SocketChannel socketChannel = serverSocketChannel.accept();
}
123
複製代碼
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 socketChannel...
}
}
1234567891011
複製代碼
Selector的建立:Selector selector = Selector.open();
爲了將Channel和Selector配合使用,必須將Channel註冊到Selector上,經過SelectableChannel.register()方法來實現,沿用案例5中的部分代碼:
ssc= ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(PORT));
ssc.configureBlocking(false);
ssc.register(selector, SelectionKey.OP_ACCEPT);
1234
複製代碼
與Selector一塊兒使用時,Channel必須處於非阻塞模式下。這意味着不能將FileChannel與Selector一塊兒使用,由於FileChannel不能切換到非阻塞模式。而套接字通道均可以。
注意register()方法的第二個參數。這是一個「interest集合」,意思是在經過Selector監聽Channel時對什麼事件感興趣。能夠監聽四種不一樣類型的事件:
1. Connect
2. Accept
3. Read
4. Write
1234
複製代碼
通道觸發了一個事件意思是該事件已經就緒。因此,某個channel成功鏈接到另外一個服務器稱爲「鏈接就緒」。一個server socket channel準備好接收新進入的鏈接稱爲「接收就緒」。一個有數據可讀的通道能夠說是「讀就緒」。等待寫數據的通道能夠說是「寫就緒」。
這四種事件用SelectionKey的四個常量來表示:
1. SelectionKey.OP_CONNECT
2. SelectionKey.OP_ACCEPT
3. SelectionKey.OP_READ
4. SelectionKey.OP_WRITE
1234
複製代碼
當向Selector註冊Channel時,register()方法會返回一個SelectionKey對象。這個對象包含了一些你感興趣的屬性:
interest集合:就像向Selector註冊通道一節中所描述的,interest集合是你所選擇的感興趣的事件集合。能夠經過SelectionKey讀寫interest集合。
ready 集合是通道已經準備就緒的操做的集合。在一次選擇(Selection)以後,你會首先訪問這個ready set。Selection將在下一小節進行解釋。能夠這樣訪問ready集合:
int readySet = selectionKey.readyOps();
1
複製代碼
能夠用像檢測interest集合那樣的方法,來檢測channel中什麼事件或操做已經就緒。可是,也可使用如下四個方法,它們都會返回一個布爾類型:
selectionKey.isAcceptable();
selectionKey.isConnectable();
selectionKey.isReadable();
selectionKey.isWritable();
1234
複製代碼
從SelectionKey訪問Channel和Selector很簡單。以下:
Channel channel = selectionKey.channel();
Selector selector = selectionKey.selector();
12
複製代碼
能夠將一個對象或者更多信息附着到SelectionKey上,這樣就能方便的識別某個給定的通道。例如,能夠附加 與通道一塊兒使用的Buffer,或是包含彙集數據的某個對象。使用方法以下:
selectionKey.attach(theObject);
Object attachedObj = selectionKey.attachment();
12
複製代碼
還能夠在用register()方法向Selector註冊Channel的時候附加對象。如:
SelectionKey key = channel.register(selector, SelectionKey.OP_READ, theObject);
1
複製代碼
一旦向Selector註冊了一或多個通道,就能夠調用幾個重載的select()方法。這些方法返回你所感興趣的事件(如鏈接、接受、讀或寫)已經準備就緒的那些通道。換句話說,若是你對「讀就緒」的通道感興趣,select()方法會返回讀事件已經就緒的那些通道。
下面是select()方法:
select()阻塞到至少有一個通道在你註冊的事件上就緒了。
select(long timeout)和select()同樣,除了最長會阻塞timeout毫秒(參數)。
selectNow()不會阻塞,無論什麼通道就緒都馬上返回(譯者注:此方法執行非阻塞的選擇操做。若是自從前一次選擇操做後,沒有通道變成可選擇的,則此方法直接返回零。)。
select()方法返回的int值表示有多少通道已經就緒。亦即,自上次調用select()方法後有多少通道變成就緒狀態。若是調用select()方法,由於有一個通道變成就緒狀態,返回了1,若再次調用select()方法,若是另外一個通道就緒了,它會再次返回1。若是對第一個就緒的channel沒有作任何操做,如今就有兩個就緒的通道,但在每次select()方法調用之間,只有一個通道就緒了。
一旦調用了select()方法,而且返回值代表有一個或更多個通道就緒了,而後能夠經過調用selector的selectedKeys()方法,訪問「已選擇鍵集(selected key set)」中的就緒通道。以下所示:
Set selectedKeys = selector.selectedKeys();
1
複製代碼
當向Selector註冊Channel時,Channel.register()方法會返回一個SelectionKey 對象。這個對象表明了註冊到該Selector的通道。
注意每次迭代末尾的keyIterator.remove()調用。Selector不會本身從已選擇鍵集中移除SelectionKey實例。必須在處理完通道時本身移除。下次該通道變成就緒時,Selector會再次將其放入已選擇鍵集中。
SelectionKey.channel()方法返回的通道須要轉型成你要處理的類型,如ServerSocketChannel或SocketChannel等。
一個完整的使用Selector和ServerSocketChannel的案例能夠參考案例5的selector()方法。
JAVA處理大文件,通常用BufferedReader,BufferedInputStream這類帶緩衝的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的子類。 MappedByteBuffer 將文件直接映射到內存(這裏的內存指的是虛擬內存,並非物理內存)。一般,能夠映射整個文件,若是文件比較大的話能夠分段進行映射,只要指定文件的那個部分就能夠。
FileChannel提供了map方法來把文件影射爲內存映像文件: MappedByteBuffer map(int mode,long position,long size); 能夠把文件的從position開始的size大小的區域映射爲內存映像文件,mode指出了 可訪問該內存映像文件的方式:
MappedByteBuffer是ByteBuffer的子類,其擴充了三個方法:
這裏經過採用ByteBuffer和MappedByteBuffer分別讀取大小約爲5M的文件"src/1.ppt"來比較二者之間的區別,method3()是採用MappedByteBuffer讀取的,method4()對應的是ByteBuffer。
public static void method4(){
RandomAccessFile aFile = null;
FileChannel fc = null;
try{
aFile = new RandomAccessFile("src/1.ppt","rw");
fc = aFile.getChannel();
long timeBegin = System.currentTimeMillis();
ByteBuffer buff = ByteBuffer.allocate((int) aFile.length());
buff.clear();
fc.read(buff);
//System.out.println((char)buff.get((int)(aFile.length()/2-1)));
//System.out.println((char)buff.get((int)(aFile.length()/2)));
//System.out.println((char)buff.get((int)(aFile.length()/2)+1));
long timeEnd = System.currentTimeMillis();
System.out.println("Read time: "+(timeEnd-timeBegin)+"ms");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(aFile!=null){
aFile.close();
}
if(fc!=null){
fc.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
public static void method3(){
RandomAccessFile aFile = null;
FileChannel fc = null;
try{
aFile = new RandomAccessFile("src/1.ppt","rw");
fc = aFile.getChannel();
long timeBegin = System.currentTimeMillis();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, aFile.length());
// System.out.println((char)mbb.get((int)(aFile.length()/2-1)));
// System.out.println((char)mbb.get((int)(aFile.length()/2)));
//System.out.println((char)mbb.get((int)(aFile.length()/2)+1));
long timeEnd = System.currentTimeMillis();
System.out.println("Read time: "+(timeEnd-timeBegin)+"ms");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(aFile!=null){
aFile.close();
}
if(fc!=null){
fc.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
複製代碼
經過在入口函數main()中運行:
method3();
System.out.println("=============");
method4();
123
複製代碼
輸出結果(運行在普通PC機上):
Read time: 2ms
=============
Read time: 12ms
123
複製代碼
經過輸出結果能夠看出彼此的差異,一個例子也許是偶然,那麼下面把5M大小的文件替換爲200M的文件,輸出結果:
Read time: 1ms
=============
Read time: 407ms
123
複製代碼
能夠看到差距拉大。
注:MappedByteBuffer有資源釋放的問題:被MappedByteBuffer打開的文件只有在垃圾收集時纔會被關閉,而這個點是不肯定的。在Javadoc中這裏描述:A mapped byte buffer and the file mapping that it represents remian valid until the buffer itself is garbage-collected。詳細能夠翻閱參考資料5和6.
看完以上陳述,詳細你們對NIO有了必定的瞭解,下面主要經過幾個案例,來講明NIO的其他功能,下面代碼量偏多,功能性講述偏少。
分散(scatter)從Channel中讀取是指在讀操做時將讀取的數據寫入多個buffer中。所以,Channel將從Channel中讀取的數據「分散(scatter)」到多個Buffer中。
彙集(gather)寫入Channel是指在寫操做時將多個buffer的數據寫入同一個Channel,所以,Channel 將多個Buffer中的數據「彙集(gather)」後發送到Channel。
scatter / gather常常用於須要將傳輸的數據分開處理的場合,例如傳輸一個由消息頭和消息體組成的消息,你可能會將消息體和消息頭分散到不一樣的buffer中,這樣你能夠方便的處理消息頭和消息體。
案例:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
public class ScattingAndGather
{
public static void main(String args[]){
gather();
}
public 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 [] buffs = {header, body};
try
{
FileOutputStream os = new FileOutputStream("src/scattingAndGather.txt");
FileChannel channel = os.getChannel();
channel.write(buffs);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
123456789101112131415161718192021222324252627282930313233343536373839
複製代碼
FileChannel的transferFrom()方法能夠將數據從源通道傳輸到FileChannel中。
public static void method1(){
RandomAccessFile fromFile = null;
RandomAccessFile toFile = null;
try
{
fromFile = new RandomAccessFile("src/fromFile.xml","rw");
FileChannel fromChannel = fromFile.getChannel();
toFile = new RandomAccessFile("src/toFile.txt","rw");
FileChannel toChannel = toFile.getChannel();
long position = 0;
long count = fromChannel.size();
System.out.println(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();
}
}
}
12345678910111213141516171819202122232425262728293031323334
複製代碼
方法的輸入參數position表示從position處開始向目標文件寫入數據,count表示最多傳輸的字節數。若是源通道的剩餘空間小於 count 個字節,則所傳輸的字節數要小於請求的字節數。此外要注意,在SoketChannel的實現中,SocketChannel只會傳輸此刻準備好的數據(可能不足count字節)。所以,SocketChannel可能不會將請求的全部數據(count個字節)所有傳輸到FileChannel中。
transferTo()方法將數據從FileChannel傳輸到其餘的channel中。
public static void method2()
{
RandomAccessFile fromFile = null;
RandomAccessFile toFile = null;
try
{
fromFile = new RandomAccessFile("src/fromFile.txt","rw");
FileChannel fromChannel = fromFile.getChannel();
toFile = new RandomAccessFile("src/toFile.txt","rw");
FileChannel toChannel = toFile.getChannel();
long position = 0;
long count = fromChannel.size();
System.out.println(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();
}
}
}
123456789101112131415161718192021222324252627282930313233343536
複製代碼
上面所說的關於SocketChannel的問題在transferTo()方法中一樣存在。SocketChannel會一直傳輸數據直到目標buffer被填滿。
Java NIO 管道是2個線程之間的單向數據鏈接。Pipe有一個source通道和一個sink通道。數據會被寫到sink通道,從source通道讀取。
public static void method1(){
Pipe pipe = null;
ExecutorService exec = Executors.newFixedThreadPool(2);
try{
pipe = Pipe.open();
final Pipe pipeTemp = pipe;
exec.submit(new Callable<Object>(){
@Override
public Object call() throws Exception
{
Pipe.SinkChannel sinkChannel = pipeTemp.sink();//向通道中寫數據
while(true){
TimeUnit.SECONDS.sleep(1);
String newData = "Pipe Test At Time "+System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while(buf.hasRemaining()){
System.out.println(buf);
sinkChannel.write(buf);
}
}
}
});
exec.submit(new Callable<Object>(){
@Override
public Object call() throws Exception
{
Pipe.SourceChannel sourceChannel = pipeTemp.source();//向通道中讀數據
while(true){
TimeUnit.SECONDS.sleep(1);
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.clear();
int bytesRead = sourceChannel.read(buf);
System.out.println("bytesRead="+bytesRead);
while(bytesRead >0 ){
buf.flip();
byte b[] = new byte[bytesRead];
int i=0;
while(buf.hasRemaining()){
b[i]=buf.get();
System.out.printf("%X",b[i]);
i++;
}
String s = new String(b);
System.out.println("=================||"+s);
bytesRead = sourceChannel.read(buf);
}
}
}
});
}catch(IOException e){
e.printStackTrace();
}finally{
exec.shutdown();
}
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
複製代碼
Java NIO中的DatagramChannel是一個能收發UDP包的通道。由於UDP是無鏈接的網絡協議,因此不能像其它通道那樣讀取和寫入。它發送和接收的是數據包。
public static void reveive(){
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());
}
System.out.println();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(channel!=null){
channel.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
public static void send(){
DatagramChannel channel = null;
try{
channel = DatagramChannel.open();
String info = "I'm the Sender!";
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.clear();
buf.put(info.getBytes());
buf.flip();
int bytesSent = channel.send(buf, new InetSocketAddress("10.10.195.115",8888));
System.out.println(bytesSent);
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(channel!=null){
channel.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
複製代碼