NIO是Java 1.4開始引入的,目的是替代標(biāo)準(zhǔn)IO,它采用了與標(biāo)準(zhǔn)IO完全不同的設(shè)計(jì)模式和工作方式,這里就來總結(jié)一下。
1.Buffer
正如他的名字,就是一個(gè)緩存,實(shí)際上是內(nèi)存的一塊區(qū)域,它是NIO體系的重要組成部分,主要和通道進(jìn)行交互。 Buffer本身是一個(gè)抽象類,它有以下幾個(gè)子類:
ByteBuffer
CharBuffer
DoubleBuffer
FloatBuffer
IntBuffer
LongBuffer
ShortBuffer
根據(jù)緩存的數(shù)據(jù)類型不同創(chuàng)建的不同子類,大致功能都類似,我們不一個(gè)一個(gè)介紹,只介紹Buffer的關(guān)鍵點(diǎn)。
Buffer的使用一般如下:將數(shù)據(jù)寫入buffer,準(zhǔn)備從buffer中讀數(shù)據(jù),讀取數(shù)據(jù),清空buffer。下面先簡(jiǎn)單演示一下然后再解釋:
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的子類也都是抽象類,不能直接實(shí)例化,都需要調(diào)用靜態(tài)方法生成,可以看源碼,調(diào)用不同的靜態(tài)方法實(shí)例化不同的實(shí)現(xiàn)類。以ByteBuffer為例,可以這樣實(shí)例化:
allocate(int capacity) //分配一個(gè)大小為capacity的字節(jié)數(shù)組作為緩沖,但是在堆中
allocateDirect(int capacity) //和上面類似,不過直接借助系統(tǒng)在內(nèi)存中創(chuàng)建,速度較快,但消耗性能
wrap(byte[] array) //直接從外部指定一個(gè)數(shù)組,不適用默認(rèn)創(chuàng)建的,但是雙方一方改動(dòng)就會(huì)影響另一方
wrap(byte[] array, int offset, int length) //和上面一個(gè)一樣,但是能指定偏移量和長(zhǎng)度
我們獲得一個(gè)Buffer實(shí)例后就可以使用,首先向buffer中寫東西需要channel配合,調(diào)用read方法即可。之后在讀之前需要準(zhǔn)備一下,從代碼看就是調(diào)用flip方法,這個(gè)方法有什么作用呢?從文檔上看,就是將limit設(shè)置為position,然后將position 置零。這樣有什么用呢?下面就來介紹一下buffer的幾個(gè)成員變量:capacity,limit,position。
capacity就是一個(gè)buffer的固定大小,表示他的容量
position表示當(dāng)前指針的位置,也就是當(dāng)前讀或?qū)懙降奈恢?br>
limit這個(gè)值在寫模式下表示最多能寫多少,寫模式下等于capacity。讀模式下表示能讀到多少,調(diào)用flip將limit等于position,表示最多能讀到之前寫入的所有內(nèi)容
看flip的實(shí)現(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方法。可以看一下他們的實(shí)現(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方法很清晰,就是將幾個(gè)游標(biāo)歸為為原始狀態(tài)。compact也是設(shè)置幾個(gè)游標(biāo)位置,不過有點(diǎn)特殊,remaining方法是獲取剩余數(shù)據(jù)數(shù)量就是limit - position,然后將該值賦給position,然后將capacity賦值給limit。他和clear的區(qū)別就是在position上的處理。但是,如果我們已經(jīng)將buffer內(nèi)容讀完,這時(shí)limit = position,那么 position(remaining())的效果就是position = limit - position = 0.這時(shí)compact方法效果等于clear。否則雖然也可以繼續(xù)寫內(nèi)容進(jìn)去,但容量減少,但好處是未讀的數(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)記一個(gè)位置,reset使游標(biāo)回到這個(gè)位置。mark成員變量初始為-1.所以不要沒有調(diào)用mark方法就去調(diào)用reset。
可以看到buffer的主要操作就是針對(duì)幾個(gè)指針的,畢竟他是依賴于數(shù)組實(shí)現(xiàn)的。
2.Channel
Channel用來實(shí)現(xiàn)通道的概念,他類似于流,但是不能直接操作數(shù)據(jù),需要借助于Buffer,它本身是一個(gè)接口,一般有以下幾個(gè)重要實(shí)現(xiàn):
FileChannel
DatagramChannel
SocketChannel
ServerSocketChannel
2.1 FileChannel
FileChannel是一個(gè)用于讀寫操作文件的channel。首先看怎么獲得實(shí)例化對(duì)象,F(xiàn)ileChannel也是一個(gè)抽象類,所以不能通過構(gòu)造獲得。一般獲得的途徑有,RandomAccessFile、FileOutputStream、FileInputStream等一些類的getChannel()方法,如上文中示例。
另外在Java 1.7 中提供了幾個(gè)靜態(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();
}
}
有一點(diǎn)需要注意的是,有時(shí)并不能保證把整個(gè)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都是把一個(gè)channel的內(nèi)容傳輸?shù)搅硪粋€(gè),但是注意兩個(gè)方法的區(qū)別,即方向性。
上面示例用到了size()方法,是用來獲取所關(guān)聯(lián)文件的大小。
position()和position(long)方法用來獲取指針位置和設(shè)置指針位置。設(shè)置position是可以將指針設(shè)置到文件結(jié)束符之后的,但是中間會(huì)有空洞。
truncate(long)方法可一截取一個(gè)文件并返回FileChannel,從文件開始截取到指定位置。
2.2 DatagramChannel
DatagramChannel是Java UDP通信中傳輸數(shù)據(jù)的通道。關(guān)于Java中傳統(tǒng)UDP的實(shí)現(xiàn)見這里,下面簡(jiǎn)單用DatagramChannel實(shí)現(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";
}
}
一般獲得一個(gè)DatagramChannel 需要使用靜態(tài)方法open,DatagramChannel 也是配合buffer使用的。之后服務(wù)端需要綁定一個(gè)地址和端口。接下來就可以收發(fā)數(shù)據(jù)了,收用receive方法,返回發(fā)送方的地址信息,發(fā)生使用send方法,返回成功發(fā)生的字節(jié)數(shù)。
有一點(diǎn)特別重要,由于是配合buffer操作,無論是客戶端還是服務(wù)端,在發(fā)送前都需要調(diào)用flip方法,否則發(fā)送的都是空數(shù)據(jù)(因?yàn)槎际菑膒osition到limit,flip之前position是當(dāng)前寫的位置,limit為capacity)。還有一點(diǎn),在接受時(shí),由于不知道buffer中有效字節(jié)數(shù),所以limit為capacity,直觀的看就是轉(zhuǎn)為字符串時(shí)末尾有大量空內(nèi)容,需要trim一下。
默認(rèn)情況下是阻塞的,也可以設(shè)置為非阻塞的,channel.configureBlocking(true);,此時(shí)receive方法會(huì)立刻返回,可能為null。channel也有connect方法,但是UDP是非連接的,所以只是綁定一個(gè)遠(yuǎn)端地址,收發(fā)智能從指定地址來。connect之后就可以用read或者write收發(fā)數(shù)據(jù)。
2.3 SocketChannel 與 ServerSocketChannel
這兩類是和Socket與 ServerSocket對(duì)應(yīng)的兩個(gè)雷,也是專為TCP通信設(shè)計(jì)的,ServerSocketChannel代表服務(wù)端,SocketChannel 代表一個(gè)連接。關(guān)于Java的傳統(tǒng)TCP實(shí)現(xiàn)見這里,下面簡(jiǎn)單實(shí)現(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獲取一個(gè)示例,服務(wù)端要綁定地址和端口,然后監(jiān)聽連接,客戶端只需去連接服務(wù)端即可。同樣的都可以設(shè)置為非阻塞的,收發(fā)數(shù)據(jù)使用read和write方法。需要注意的還是buffer操作問題以及即使關(guān)流或者做控制就行。
3.Selector
Selector 可以同時(shí)監(jiān)控多個(gè)Channel 的 IO 狀況,也就是說,利用 Selector可使一個(gè)單獨(dú)的線程管理多個(gè) Channel,selector 是非阻塞 IO 的核心。簡(jiǎn)單示例
客戶端
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()方法獲取一個(gè)Selector ,設(shè)置ServerSocketChannel 為非阻塞的,之后注冊(cè)事件,一般有以下幾種
SelectionKey.OP_CONNECT
SelectionKey.OP_ACCEPT
SelectionKey.OP_READ
SelectionKey.OP_WRITE
之后調(diào)用select()返回就緒通道數(shù),然后根據(jù)時(shí)間類型執(zhí)行具體操作即可。
4.Pipe
傳統(tǒng)IO為我們提供了線程間通信的類,PipedInputStream與PipedOutputStream。NIO作為IO的替代者,自然也有線程通信的方法,就是Pipe,使用起來很簡(jiǎn)單,如下:
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只是一個(gè)管理者,收發(fā)數(shù)據(jù)還是通過兩個(gè)通道:SinkChannel 和SourceChannel 。都是單項(xiàng)的,SinkChannel 負(fù)責(zé)寫數(shù)據(jù),SourceChannel 負(fù)責(zé)收數(shù)據(jù)。