寫入數據到Buffer.
調用flip()方法.
從Buffer中讀取數據.
調用clear()方法或compact()方法. 當向Buffer寫入數據時,Buffer會記錄寫了多少數據,1旦要讀取數據,* 需要通過flip()方法將Buffer從寫模式切換到讀模式,在讀模式下,可以讀取之前寫入到buffer的所有數據.
1旦讀完所有數據,就需要清空緩沖區,讓它可以再次被寫入,clear()方法會清空全部緩沖區,compact()方法只會清除已讀過的數據,任何未讀的數據被移動到緩沖區的起始處,新寫入的數據放在其后.
Buffer的capacity,position和limit
緩沖區本質是1塊可以寫入數據,然后可以從中讀取數據的內存.
這塊內存被包裝成NIO Buffer對象,并提供了1組方法,用來方便的訪問該塊內存.
position和limit的含義取決于Buffer處在讀模式還是寫模式,不管Buffer處于甚么模式,capacity的含義總是1樣的.
capacity:Buffer的固定大小值.
position: 初始值為0,當1個byte,long等數據寫到Buffer后,position向前移動到下1個可插入數據的Buffer單元,position最大可為capacity⑴;
當讀數據時,也是從某個特定位置讀,當將Buffer從寫模式切換到讀模式,position會被重置為0,當從Buffer的position處讀取數據時,position向前移動到下1個可讀的位置.
limit:在寫模式下,Buffer的limit表示你最多能往Buffer里寫多少數據,寫模式下,limit等于Buffer的capacity。
當切換到Buffer的讀模式時,limit表示最多能讀多少數據,因此,limit會被設置成寫模式下的position值.
ByteBBuffer buf = ByteBuffer.allocate(48);
CharBuffer buf = CharBuffer.allocate(1024);
寫數據到Buffer的兩種方式:
從Channel寫到Buffer。
通過Buffer的put()方法寫到Buffer里.
從Channel寫到Buffer的例子:
int bytesRead = inChannel.write(buf);
通過put方法寫Buffer的例子:
buf.put(127);
flip方法將Buffer從寫模式切換到讀模式。調用flip()方法會將position設回0,并將limit設置成之前position的值。
換句話說,position現在用于標記讀的位置,limit表示之前寫進了多少個byte、char等 —— 現在能讀取多少個byte、char等。
Buffer.rewind()將position設回0,所以你可以重讀Buffer中的所有數據。limit保持不變,依然表示能從Buffer中讀取多少個元素(byte、char等).
通過調用Buffer.mark()方法,可以標記Buffer中的1個特定position。以后可以通過調用Buffer.reset()方法恢復到這個position。
buffer.mark();
//call buffer.get();
buffer.reset();
當滿足以下條件時,表示兩個Buffer相等:
* 有相同的類型(byte、char、int等)。
* Buffer中剩余的byte、char等的個數相等。
* Buffer中所有剩余的byte、char等都相同。
如你所見,equals只是比較Buffer的1部份,不是每個在它里面的元素都比較。實際上,它只比較Buffer中的剩余元素。
compareTo()方法比較兩個Buffer的剩余元素(byte,char),如果滿足以下條件,則認為1個Buffer小于另外1個Buffer
* 第1個不相等的元素小于另外一個Buffer中對應的元素.
* 所有元素都相等,但第1個Buffer比另外1個先耗盡.
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();
}
}
}
Select(選擇器)是Java NIO 中能夠檢測1到多個NIO通道,并能夠知曉通道是不是為諸如讀寫事件做好準備的組件,這樣,1個單獨的線程就能夠管理多個channel,從而管理多個網絡連接.
Selector selector = Selector.open();
channel.configureBlocking(false);
Selection key = channel.register(selector,Selectionkey.OP_READ);
與Selector1起使用時,Channel必須處于非阻塞模式下,這意味著不能將FileChannel與Selector1起使用,由于FileChannel不能切換到非阻塞模式,而套接字通道都可以.
可以監聽4種不同類型的事件:
1. Connect
2. Accept
3. Read
4. Write
通道觸發了1個事件意思是該事件已就緒。所以,某個channel成功連接到另外一個服務器稱為“連接就緒”。1個server socket channel準備好接收新進入的連接稱為“接收就緒”。1個有數據可讀的通道可以說是“讀就緒”。等待寫數據的通道可以說是“寫就緒”。
當向Selector注冊Channel時,register()方法會返回1個SelectionKey對象,這個對象包括1些你感興趣的屬性.
interest集合
ready集合
Channel
Selector
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 集合是通道已準備就緒的操作的集合。在1次選擇(Selection)以后,你會首先訪問這個ready set。Selection將在下1小節進行解釋。可以這樣訪問ready集合:
int readySet = selectionKey.readOps();
可以像檢測interest集合那樣,來檢測channel中甚么事件或操作已就緒,但是,也能夠使用以下4個方法,它們都會返回1個布爾類型:
selectionKey.isAcceptable();
selectionKey.isConnectable();
selectionKey.isReadable();
selectionKey.isWritable();
Channel + Selector
從SelectionKey訪問Channel和Selector很簡單,以下:
Channel channel = selectionKey,channel();
Selector selector = selectionKey.selector();
1旦向Selector注冊了1個或多個通道,就能夠調用幾個重載的select()方法,這些方法返回你所感興趣的事件(如連接,接受,讀或寫)已準備就緒的那些通道,換句話說,如果你對”讀就緒事件感興趣”,select()方法會返回讀事件已就緒的那些通道.
1.int select() 阻塞到最少有1個通道在你注冊的事件上就緒了.
2.select(long timeout)在timeout以內,有事件產生,返回,超時無事件也返回.
3.selectNow()不會阻塞,不管甚么通道就緒立刻返回.沒有事件返回0.
4.select()方法返回的int值表示有多少通道已就緒。亦即,自上次調用select()方法后有多少通道變成績緒狀態。如果調用select()方法,由于有1個通道變成績緒狀態,返回了1,若再次調用select()方法,如果另外一個通道就緒了,它會再次返回1。如果對第1個就緒的channel沒有做任何操作,現在就有兩個就緒的通道,但在每次select()方法調用之間,只有1個通道就緒了。
selectedKeys()
1旦調用了select()方法,并且返回值表明有1個或更多個通道就緒了,然后可以通過調用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()方法返回,只要讓其它線程在第1個線程調用select()方法的那個對象上調用Selector.wakeup()方法便可,阻塞在select()方法上的線程會立馬返回.
**如果有其它線程調用了wakeup()方法,但當前沒有線程阻塞在select()方法上,下1個調用select()方法的線程會立即”醒來”.
close()
用完Selector后調用其close()方法會關閉Selector,且使注冊到該Selector上的所有SelectionKey實例無效,通道本身不會關閉.
FileChannel沒法設置為非阻塞模式,它總是運行在阻塞模式下.
1.打開FileChannel
在使用FileChannel之前,必須先打開它,但是,我們沒法直接打開1個FileChannel,需要通過使用1個InputStream,OutputStream或RandomAccessFile來獲得1個FileChannel實例,
RandomAccessFile aFile = new RandomAccessFile("1.txt","rw");
FileChannel inChannel = aFile.getChannel();
2.從FileChannel讀取數據
調用多個read()方法之1從FileChannel中讀取數據. 如:
ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = inChannel.read(buf);
3.向FileChannel寫數據
使用FileChannel.write()方法向FileChannel寫數據,該方法的參數是1個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()方法1次能向FileChannel寫入多少字節,因此需要重復調用write()方法,直到Buffer已沒有還沒有寫入通道的字節.
4.關閉FileChannel
channel.close();
FileChannel的position方法.
有時可能需要在FileChannel的某個特定位置進行數據的讀/寫操作。可以通過調用position()方法獲得FileChannel確當前位置。
也能夠通過調用position(long pos)方法設置FileChannel確當前位置。
例子以下:
long pos = channel.position();
channel.position(pos +123);
如果將位置設置在文件結束以后,然后試圖從文件中讀取數據,該方法將返回⑴—文件結束標志.
如果將位置設置在文件結束符以后,然后像通道中寫數據,文件將撐大到當前位置并寫入數據,這可能致使”文件空洞”,磁盤上物理文件中寫入的數據間有空隙。
5.FileChannel的size()方法
FileChannel實例的size()方法返回的是該實例鎖關聯文件的大小.
long fileSize = channel.size();
6.FileChannel的truncate方法
可使用FileChannel.truncate()方法截取1個文件,截取文件時,文件中指定長度后面的部份將被刪除.
channel.truncate(1024).
7.FileChannel的force方法
FileChannel.force()方法將通道里還沒有寫入磁盤的數據強迫寫到磁盤上。出于性能方面的斟酌,操作系統會將數據緩存在內存中,所以沒法保證寫入到FileChannel里的數據1定會即時寫到磁盤上。要保證這1點,需要調用force()方法。
force()方法有1個boolean類型的參數,指明是不是同時將文件元數據(權限信息等)寫到磁盤上。
channel.force(true);
SocketChannel 是1個連接到TCP網絡套接字的通道.可以通過以下兩種方式創建SocketChannel:
* 打開1個SocketChannel并連接到互聯網上的某臺服務器.
* 1個新連接到達ServerSocketChannel,會創建1個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中讀取數據,調用1個read()的方法之1,以下是例子:
ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = socketChannel.read(buf);
首先,分配1個Buffer。從SocketChannel讀取到的數據將會放到這個Buffer中。
然后,調用SocketChannel.read()。該方法將數據從SocketChannel 讀到Buffer中。read()方法返回的int值表示讀了多少字節進Buffer里。如果返回的是⑴,表示已讀到了流的末尾(連接關閉了)。
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()方法的調用是在1個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返回值,它會告知你讀取了多少字節。
ServerSocketChannel 是1個可以監聽進來的TCP連接的通道,就像標準IO中的ServerSocket1樣,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()返回時,包括1個新進來的連接的SocketChannel.因此.accept()方法會1直阻塞到有新連接到達.
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.
}
}
管道是2個線程之間的單向數據連接,Pipe有1個source通道和1個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會告知我們多少自己被讀進了緩沖區.