NIO是Java 1.4開始引入的侣肄,目的是替代標(biāo)準(zhǔn)IO蚜印,它采用了與標(biāo)準(zhǔn)IO完全不同的設(shè)計模式和工作方式,這里就來總結(jié)一下兴想。
1.Buffer
正如他的名字幢哨,就是一個緩存,實際上是內(nèi)存的一塊區(qū)域嫂便,它是NIO體系的重要組成部分捞镰,主要和通道進行交互。 Buffer本身是一個抽象類,它有以下幾個子類:
ByteBuffer
CharBuffer
DoubleBuffer
FloatBuffer
IntBuffer
LongBuffer
ShortBuffer
根據(jù)緩存的數(shù)據(jù)類型不同創(chuàng)建的不同子類岸售,大致功能都類似践樱,我們不一個一個介紹,只介紹Buffer的關(guān)鍵點凸丸。
Buffer的使用一般如下:將數(shù)據(jù)寫入buffer阳堕,準(zhǔn)備從buffer中讀數(shù)據(jù)弧可,讀取數(shù)據(jù)悼嫉,清空buffer萎馅。下面先簡單演示一下然后再解釋:
public static void main(String[] args) throws IOException {
try (RandomAccessFile file = new RandomAccessFile("file/test.txt","rw");
FileChannel channel = file.getChannel()){
ByteBuffer buffer = ByteBuffer.allocate(5);
int len;
while((len = channel.read(buffer))!=-1){
buffer.flip();
while (buffer.hasRemaining())
System.out.println((char)buffer.get());
buffer.compact();
}
}catch (Exception e){
e.printStackTrace();
}
}
先看構(gòu)造,Buffer的子類也都是抽象類抛人,不能直接實例化,都需要調(diào)用靜態(tài)方法生成脐瑰,可以看源碼妖枚,調(diào)用不同的靜態(tài)方法實例化不同的實現(xiàn)類。以ByteBuffer為例苍在,可以這樣實例化:
allocate(int capacity) //分配一個大小為capacity的字節(jié)數(shù)組作為緩沖绝页,但是在堆中
allocateDirect(int capacity) //和上面類似,不過直接借助系統(tǒng)在內(nèi)存中創(chuàng)建寂恬,速度較快续誉,但消耗性能
wrap(byte[] array) //直接從外部指定一個數(shù)組,不適用默認(rèn)創(chuàng)建的初肉,但是雙方一方改動就會影響另一方
wrap(byte[] array, int offset, int length) //和上面一個一樣酷鸦,但是能指定偏移量和長度
我們獲得一個Buffer實例后就可以使用,首先向buffer中寫東西需要channel配合牙咏,調(diào)用read方法即可臼隔。之后在讀之前需要準(zhǔn)備一下,從代碼看就是調(diào)用flip方法妄壶,這個方法有什么作用呢摔握?從文檔上看,就是將limit設(shè)置為position丁寄,然后將position 置零氨淌。這樣有什么用呢?下面就來介紹一下buffer的幾個成員變量:capacity伊磺,limit盛正,position。
capacity就是一個buffer的固定大小奢浑,表示他的容量
position表示當(dāng)前指針的位置蛮艰,也就是當(dāng)前讀或?qū)懙降奈恢?br>
limit這個值在寫模式下表示最多能寫多少,寫模式下等于capacity。讀模式下表示能讀到多少壤蚜,調(diào)用flip將limit等于position即寡,表示最多能讀到之前寫入的所有內(nèi)容
看flip的實現(xiàn)也很好理解,如下:
public final Buffer flip() {
limit = position;
position = 0;
mark = -1;
return this;
}
剛才是準(zhǔn)備讀數(shù)據(jù)袜刷,下面就是從中讀了聪富,buffer有一系列g(shù)et方法,和流的read方法類似著蟹。既然有g(shù)et就有put方法墩蔓,除了上面和channel配合寫入東西,還可以用一系列put方法寫入萧豆。讀之前可以判斷一下是否還有數(shù)據(jù):hasRemaining()奸披;讀完之后為了使下次還能用,需要清空buffer涮雷,可以用clear方法或者compact方法阵面。可以看一下他們的實現(xiàn):
public final Buffer clear() {
position = 0;
limit = capacity;
mark = -1;
return this;
}
public ByteBuffer compact() {
System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
position(remaining());
limit(capacity());
discardMark();
return this;
}
clear方法很清晰洪鸭,就是將幾個游標(biāo)歸為為原始狀態(tài)样刷。compact也是設(shè)置幾個游標(biāo)位置,不過有點特殊览爵,remaining方法是獲取剩余數(shù)據(jù)數(shù)量就是limit - position置鼻,然后將該值賦給position,然后將capacity賦值給limit蜓竹。他和clear的區(qū)別就是在position上的處理箕母。但是,如果我們已經(jīng)將buffer內(nèi)容讀完梅肤,這時limit = position司蔬,那么 position(remaining())的效果就是position = limit - position = 0.這時compact方法效果等于clear。否則雖然也可以繼續(xù)寫內(nèi)容進去姨蝴,但容量減少俊啼,但好處是未讀的數(shù)據(jù)以后可以繼續(xù)讀。
再來看其他方法:
public final Buffer rewind() {
position = 0;
mark = -1;
return this;
}
rewind是將position 置零左医,也就是buffer中內(nèi)容可以重新讀取授帕。
public final Buffer mark() {
mark = position;
return this;
}
public final Buffer reset() {
int m = mark;
if (m < 0)
throw new InvalidMarkException();
position = m;
return this;
}
mark和reset是配合使用的。mark標(biāo)記一個位置浮梢,reset使游標(biāo)回到這個位置跛十。mark成員變量初始為-1.所以不要沒有調(diào)用mark方法就去調(diào)用reset。
可以看到buffer的主要操作就是針對幾個指針的秕硝,畢竟他是依賴于數(shù)組實現(xiàn)的芥映。
2.Channel
Channel用來實現(xiàn)通道的概念,他類似于流,但是不能直接操作數(shù)據(jù)奈偏,需要借助于Buffer坞嘀,它本身是一個接口,一般有以下幾個重要實現(xiàn):
FileChannel
DatagramChannel
SocketChannel
ServerSocketChannel
2.1 FileChannel
FileChannel是一個用于讀寫操作文件的channel惊来。首先看怎么獲得實例化對象丽涩,F(xiàn)ileChannel也是一個抽象類,所以不能通過構(gòu)造獲得裁蚁。一般獲得的途徑有矢渊,RandomAccessFile、FileOutputStream枉证、FileInputStream等一些類的getChannel()方法,如上文中示例矮男。
另外在Java 1.7 中提供了幾個靜態(tài)的open方法用來直接打開或創(chuàng)建文件獲取Channel:
public static void main(String[] args) throws IOException {
try (FileChannel channel = FileChannel.open(Paths.get("file/test.txt"), StandardOpenOption.READ)){
ByteBuffer buffer = ByteBuffer.allocate(5);
int len;
while((len = channel.read(buffer))!=-1){
buffer.flip();
while (buffer.hasRemaining())
System.out.println((char)buffer.get());
buffer.compact();
}
}catch (Exception e){
e.printStackTrace();
}
}
Channel是不能直接讀數(shù)據(jù)的,需要借助于buffer刽严,同樣寫內(nèi)容也是要借助于buffer昂灵,下面演示一下傳統(tǒng)的復(fù)制文件。
public static void main(String[] args) throws IOException {
try (FileChannel readChannel = FileChannel.open(Paths.get("file/test.png"), StandardOpenOption.READ)){
FileChannel writeChannel = FileChannel.open(Paths.get("file/copy.png"), StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE);
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (readChannel.read(buffer)!=-1){
buffer.flip();
while (buffer.hasRemaining())
writeChannel.write(buffer);
buffer.clear();
}
}catch (Exception e){
e.printStackTrace();
}
}
有一點需要注意的是舞萄,有時并不能保證把整個buffer的內(nèi)容寫入,為了嚴(yán)謹(jǐn)起見管削,需要循環(huán)判斷buffer中是否有內(nèi)容未寫入倒脓。
除了上面?zhèn)鹘y(tǒng)的寫法,channel還有自己特有的傳輸方法:
try (FileChannel readChannel = FileChannel.open(Paths.get("file/test.png"), StandardOpenOption.READ)){
FileChannel writeChannel = FileChannel.open(Paths.get("file/copy.png"), StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE);
//以下兩句話效果一樣
//writeChannel.transferFrom(readChannel,0,readChannel.size());
readChannel.transferTo(0,readChannel.size(),writeChannel);
}catch (Exception e){
e.printStackTrace();
}
transferFrom和transferTo都是把一個channel的內(nèi)容傳輸?shù)搅硪粋€含思,但是注意兩個方法的區(qū)別崎弃,即方向性。
上面示例用到了size()方法含潘,是用來獲取所關(guān)聯(lián)文件的大小饲做。
position()和position(long)方法用來獲取指針位置和設(shè)置指針位置。設(shè)置position是可以將指針設(shè)置到文件結(jié)束符之后的遏弱,但是中間會有空洞盆均。
truncate(long)方法可一截取一個文件并返回FileChannel,從文件開始截取到指定位置漱逸。
2.2 DatagramChannel
DatagramChannel是Java UDP通信中傳輸數(shù)據(jù)的通道泪姨。關(guān)于Java中傳統(tǒng)UDP的實現(xiàn)見這里,下面簡單用DatagramChannel實現(xiàn)一下UDP通信
服務(wù)端
public class UDPService {
public static final String SERVICE_IP = "127.0.0.1";
public static final int SERVICE_PORT = 10101;
public static void main(String[] args) {
UDPService service = new UDPService();
service.startService(SERVICE_IP,SERVICE_PORT);
}
private void startService(String ip, int port){
try (DatagramChannel channel = DatagramChannel.open()){
channel.bind(new InetSocketAddress(ip,port));
while (true){
ByteBuffer buffer = ByteBuffer.allocate(1024);
SocketAddress socketAddress = channel.receive(buffer);
String receive = new String(buffer.array(),"UTF-8").trim();
System.out.println("address: " + socketAddress.toString()+ " msg: "+ receive);
buffer.clear();
buffer.put((receive + "hello world").getBytes());
buffer.flip();
channel.send(buffer,socketAddress);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
客戶端
public class UDPClient {
public static void main(String[] args){
UDPClient client = new UDPClient();
Scanner scanner = new Scanner(System.in);
while(true){
String msg = scanner.nextLine();
if("##".equals(msg))
break;
System.out.println(client.sendAndReceive(UDPService.SERVICE_IP,UDPService.SERVICE_PORT,msg));
}
}
private String sendAndReceive(String serviceIp, int servicePort, String msg) {
try (DatagramChannel channel = DatagramChannel.open()){
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put(msg.getBytes());
System.out.println(buffer.position());
buffer.flip();
SocketAddress address = new InetSocketAddress(serviceIp,servicePort);
System.out.println( channel.send(buffer,address));
buffer.clear();
SocketAddress socketAddress = channel.receive(buffer);
return "address: " + socketAddress.toString()+ " msg: "+ new String(buffer.array(),"UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return "null";
}
}
一般獲得一個DatagramChannel 需要使用靜態(tài)方法open饰抒,DatagramChannel 也是配合buffer使用的肮砾。之后服務(wù)端需要綁定一個地址和端口。接下來就可以收發(fā)數(shù)據(jù)了袋坑,收用receive方法仗处,返回發(fā)送方的地址信息,發(fā)生使用send方法,返回成功發(fā)生的字節(jié)數(shù)婆誓。
有一點特別重要吃环,由于是配合buffer操作,無論是客戶端還是服務(wù)端旷档,在發(fā)送前都需要調(diào)用flip方法模叙,否則發(fā)送的都是空數(shù)據(jù)(因為都是從position到limit,flip之前position是當(dāng)前寫的位置鞋屈,limit為capacity)范咨。還有一點,在接受時厂庇,由于不知道buffer中有效字節(jié)數(shù)渠啊,所以limit為capacity,直觀的看就是轉(zhuǎn)為字符串時末尾有大量空內(nèi)容权旷,需要trim一下替蛉。
默認(rèn)情況下是阻塞的,也可以設(shè)置為非阻塞的拄氯,channel.configureBlocking(true);躲查,此時receive方法會立刻返回,可能為null译柏。channel也有connect方法镣煮,但是UDP是非連接的,所以只是綁定一個遠(yuǎn)端地址鄙麦,收發(fā)智能從指定地址來典唇。connect之后就可以用read或者write收發(fā)數(shù)據(jù)。
2.3 SocketChannel 與 ServerSocketChannel
這兩類是和Socket與 ServerSocket對應(yīng)的兩個雷胯府,也是專為TCP通信設(shè)計的介衔,ServerSocketChannel代表服務(wù)端,SocketChannel 代表一個連接骂因。關(guān)于Java的傳統(tǒng)TCP實現(xiàn)見這里炎咖,下面簡單實現(xiàn)一下TCP通信
服務(wù)端:
public class TCPService {
public static final String SERVICE_IP = "127.0.0.1";
public static final int SERVICE_PORT = 10101;
public static void main(String[] args) {
TCPService service = new TCPService();
service.startService();
}
private void startService(){
try (ServerSocketChannel service = ServerSocketChannel.open()){
service.bind(new InetSocketAddress(SERVICE_IP,SERVICE_PORT));
while (true){
SocketChannel channel = service.accept();
ByteBuffer buffer = ByteBuffer.allocate(1024);
StringBuilder msg = new StringBuilder();
while ((len = channel.read(buffer)) > 0) {
receive.append(new String(buffer.array(), 0, len));
buffer.clear();
}
System.out.println("address: " + channel.getRemoteAddress().toString() + " msg: " + msg.toString());
buffer.clear();
buffer.put((msg + "hello world").getBytes());
buffer.flip();
while (buffer.hasRemaining())
channel.write(buffer);
channel.shutdownOutput();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
客戶端
public class TCPClient {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
TCPClient client = new TCPClient();
while(true){
System.out.println(client.sendAndReceive(TCPService.SERVICE_IP,TCPService.SERVICE_PORT,scanner.nextLine()));
}
}
private String sendAndReceive(String address,int port,String msg){
try (SocketChannel channel = SocketChannel.open()){
channel.connect(new InetSocketAddress(address,port));
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put(msg.getBytes());
buffer.flip();
while (buffer.hasRemaining())
channel.write(buffer);
channel.shutdownOutput();
buffer.clear();
StringBuilder receive = new StringBuilder();
int len = 0;
while ((len = channel.read(buffer)) > 0) {
receive.append(new String(buffer.array(), 0, len));
buffer.clear();
}
return "address: " + channel.getRemoteAddress().toString() + " msg: " + receive.toString();
}catch (Exception e){
e.printStackTrace();
}
return "null";
}
}
同樣都是利用open獲取一個示例,服務(wù)端要綁定地址和端口侣签,然后監(jiān)聽連接塘装,客戶端只需去連接服務(wù)端即可。同樣的都可以設(shè)置為非阻塞的影所,收發(fā)數(shù)據(jù)使用read和write方法蹦肴。需要注意的還是buffer操作問題以及即使關(guān)流或者做控制就行。
3.Selector
Selector 可以同時監(jiān)控多個Channel 的 IO 狀況猴娩,也就是說阴幌,利用 Selector可使一個單獨的線程管理多個 Channel勺阐,selector 是非阻塞 IO 的核心。簡單示例
客戶端
public class TCPClient {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
TCPClient client = new TCPClient();
while(true){
client.sendAndReceive(TCPService.SERVICE_IP,TCPService.SERVICE_PORT,scanner.nextLine());
}
}
private void sendAndReceive(String address,int port,String msg){
try (SocketChannel channel = SocketChannel.open()){
channel.connect(new InetSocketAddress(address, port));
ByteBuffer buf = ByteBuffer.allocate(1024);
channel.configureBlocking(false);
buf.put((new Date() + ":" + msg).getBytes());
buf.flip();
channel.write(buf);
buf.clear();
channel.shutdownOutput();
}catch (Exception e){
e.printStackTrace();
}
}
}
服務(wù)端
public class TCPService {
public static final String SERVICE_IP = "127.0.0.1";
public static final int SERVICE_PORT = 10101;
private String msg;
public static void main(String[] args) {
TCPService service = new TCPService();
service.startService();
}
private void startService(){
try (ServerSocketChannel service = ServerSocketChannel.open()){
service.bind(new InetSocketAddress(SERVICE_IP,SERVICE_PORT));
service.configureBlocking(false);
Selector selector = Selector.open();
service.register(selector, SelectionKey.OP_ACCEPT);
while (selector.select() > 0) {
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
if (key.isAcceptable()) {
System.out.println("isAcceptable");
SocketChannel sc = service.accept();
sc.configureBlocking(false);
sc.register(selector, SelectionKey.OP_READ );
} else if (key.isReadable()) {
System.out.println("isReadable");
SocketChannel channel = (SocketChannel) key.channel();
ByteBuffer buf = ByteBuffer.allocate(1024);
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = channel.read(buf)) > 0) {
sb.append(new String(buf.array(), 0, len));
buf.clear();
}
System.out.println(sb.toString());
channel.close();
}
it.remove();
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
主要是服務(wù)端的應(yīng)用矛双,基本流程就是先利用open()方法獲取一個Selector 渊抽,設(shè)置ServerSocketChannel 為非阻塞的,之后注冊事件议忽,一般有以下幾種
SelectionKey.OP_CONNECT
SelectionKey.OP_ACCEPT
SelectionKey.OP_READ
SelectionKey.OP_WRITE
之后調(diào)用select()返回就緒通道數(shù)懒闷,然后根據(jù)時間類型執(zhí)行具體操作即可。
4.Pipe
傳統(tǒng)IO為我們提供了線程間通信的類栈幸,PipedInputStream與PipedOutputStream愤估。NIO作為IO的替代者,自然也有線程通信的方法速址,就是Pipe玩焰,使用起來很簡單,如下:
public class Receiver extends Thread{
private Pipe pipe;
public void setPipe(Pipe pipe){
this.pipe = pipe;
}
@Override
public void run() {
super.run();
try (Pipe.SourceChannel channel = pipe.source()){
ByteBuffer buf = ByteBuffer.allocate(1024);
int len = 0;
StringBuilder sb = new StringBuilder();
while((len = channel.read(buf))!=-1){
sb.append(new String(buf.array(),0,len));
buf.clear();
}
System.out.println(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Sender extends Thread{
private Pipe pipe;
public void setPipe(Pipe pipe){
this.pipe = pipe;
}
@Override
public void run() {
super.run();
try (Pipe.SinkChannel channel = pipe.sink()){
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put("hello world".getBytes());
buffer.flip();
while (buffer.hasRemaining())
channel.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
Receiver receiver = new Receiver();
Sender sender = new Sender();
Pipe pipe = Pipe.open();
receiver.setPipe(pipe);
sender.setPipe(pipe);
receiver.start();
sender.start();
}
Pipe只是一個管理者芍锚,收發(fā)數(shù)據(jù)還是通過兩個通道:SinkChannel 和SourceChannel 昔园。都是單項的,SinkChannel 負(fù)責(zé)寫數(shù)據(jù)并炮,SourceChannel 負(fù)責(zé)收數(shù)據(jù)默刚。