Java-NIO
什麼是NIO
Java NIO(New IO)是從Java 1.4版本開始引入的一個新的IO API,可以替代標準的Java IO API。NIO與原來的IO有同樣的作用和目的,但是使用的方式完全不同,NIO支援面向緩衝區的、基於通道的IO操作。NIO將以更加高效的方式進行文件的讀寫操作。
IO | NIO |
---|---|
面向流(Stream Oriented) | 面向緩衝區(Buffer Oriented) |
阻塞IO(Blocking IO) | 非阻塞IO(NonBlocking IO) |
選擇器(Selectors) |
底層原理可見:作業系統-文件IO
緩衝區(Buffer)
緩衝區類型
Buffer 就像一個數組,可以保存多個相同類型的數據。根據數據類型不同(boolean 除外) ,有以下Buffer 常用子類
- ByteBuffer
- CharBuffer
- ShortBuffer
- IntBuffer
- LongBuffer
- FloatBuffer
- DoubleBuffer
各種類型的緩衝區中,都有一個對應類型的數組,如
ByteBuffer
final byte[] hb; // Non-null only for heap buffersCopy
IntBuffer
final int[] hb; // Non-null only for heap buffers
獲取緩衝區
通過allocate方法可以獲取一個對應緩衝區的對象,它是緩衝區類的一個靜態方法
例
// 獲取一個容量大小為1024位元組的位元組緩衝區
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
核心屬性
緩衝區的父類Buffer中有幾個核心屬性,如下
// Invariants: mark <= position <= limit <= capacity
private int mark = -1;
private int position = 0;
private int limit;
private int capacity;Copy
- capacity:緩衝區的容量。通過構造函數賦予,一旦設置,無法更改
- limit:緩衝區的界限。位於limit 後的數據不可讀寫。緩衝區的限制不能為負,並且不能大於其容量
- position:下一個讀寫位置的索引(類似PC)。緩衝區的位置不能為負,並且不能大於limit
- mark:記錄當前position的值。position被改變後,可以通過調用reset() 方法恢復到mark的位置。
以上四個屬性必須滿足以下要求
mark <= position <= limit <= capacity
核心方法
put()方法
- put()方法可以將一個數據放入到緩衝區中。
- 進行該操作後,postition的值會+1,指向下一個可以放入的位置。capacity = limit ,為緩衝區容量的值。
flip()方法
- flip()方法會切換對緩衝區的操作模式,由寫->讀 / 讀->寫
- 進行該操作後
- 如果是寫模式->讀模式,position = 0 , limit 指向最後一個元素的下一個位置,capacity不變
- 如果是讀->寫,則恢復為put()方法中的值
get()方法
- get()方法會讀取緩衝區中的一個值
- 進行該操作後,position會+1,如果超過了limit則會拋出異常
rewind()方法
- 該方法只能在讀模式下使用
- rewind()方法後,會恢復position、limit和capacity的值,變為進行get()前的值
clean()方法
- clean()方法會將緩衝區中的各個屬性恢復為最初的狀態,position = 0, capacity = limit
- 此時緩衝區的數據依然存在,處於「被遺忘」狀態,下次進行寫操作時會覆蓋這些數據
mark()和reset()方法
- mark()方法會將postion的值保存到mark屬性中
- reset()方法會將position的值改為mark中保存的值
使用展示
import java.nio.ByteBuffer;
public class demo1 {
public static void main(String[] args) {
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
System.out.println("放入前參數");
System.out.println("position " + byteBuffer.position());
System.out.println("limit " + byteBuffer.limit());
System.out.println("capacity " + byteBuffer.capacity());
System.out.println();
System.out.println("------put()------");
System.out.println("放入3個數據");
byte bt = 1;
byteBuffer.put(bt);
byteBuffer.put(bt);
byteBuffer.put(bt);
System.out.println("放入後參數");
System.out.println("position " + byteBuffer.position());
System.out.println("limit " + byteBuffer.limit());
System.out.println("capacity " + byteBuffer.capacity());
System.out.println();
System.out.println("------flip()-get()------");
System.out.println("讀取一個數據");
// 切換模式
byteBuffer.flip();
byteBuffer.get();
System.out.println("讀取後參數");
System.out.println("position " + byteBuffer.position());
System.out.println("limit " + byteBuffer.limit());
System.out.println("capacity " + byteBuffer.capacity());
System.out.println();
System.out.println("------rewind()------");
byteBuffer.rewind();
System.out.println("恢復後參數");
System.out.println("position " + byteBuffer.position());
System.out.println("limit " + byteBuffer.limit());
System.out.println("capacity " + byteBuffer.capacity());
System.out.println();
System.out.println("------clear()------");
// 清空緩衝區,這裡只是恢復了各個屬性的值,但是緩衝區里的數據依然存在
// 但是下次寫入的時候會覆蓋緩衝區中之前的數據
byteBuffer.clear();
System.out.println("清空後參數");
System.out.println("position " + byteBuffer.position());
System.out.println("limit " + byteBuffer.limit());
System.out.println("capacity " + byteBuffer.capacity());
System.out.println();
System.out.println("清空後獲得數據");
System.out.println(byteBuffer.get());
}
}
放入前參數
position 0
limit 1024
capacity 1024
------put()------
放入3個數據
放入後參數
position 3
limit 1024
capacity 1024
------flip()-get()------
讀取一個數據
讀取後參數
position 1
limit 3
capacity 1024
------rewind()------
恢復後參數
position 0
limit 3
capacity 1024
------clear()------
清空後參數
position 0
limit 1024
capacity 1024
清空後獲得數據
1
Process finished with exit code 0
非直接緩衝區和直接緩衝區
非直接緩衝區
通過allocate()方法獲取的緩衝區都是非直接緩衝區。這些緩衝區是建立在JVM堆記憶體之中的。
public static ByteBuffer allocate(int capacity) {
if (capacity < 0)
throw new IllegalArgumentException();
// 在堆記憶體中開闢空間
return new HeapByteBuffer(capacity, capacity);
}
HeapByteBuffer(int cap, int lim) { // package-private
// new byte[cap] 創建數組,在堆記憶體中開闢空間
super(-1, 0, lim, cap, new byte[cap], 0);
/*
hb = new byte[cap];
offset = 0;
*/
}
通過非直接緩衝區,想要將數據寫入到物理磁碟中,或者是從物理磁碟讀取數據。都需要經過JVM和作業系統,數據在兩個地址空間中傳輸時,會copy一份保存在對方的空間中。所以費直接緩衝區的讀取效率較低.。
直接緩衝區
只有ByteBuffer可以獲得直接緩衝區,通過allocateDirect()獲取的緩衝區為直接緩衝區,這些緩衝區是建立在物理記憶體之中的。
public static ByteBuffer allocateDirect(int capacity) {
return new DirectByteBuffer(capacity);
}
DirectByteBuffer(int cap) { // package-private
...
// 申請物理記憶體
boolean pa = VM.isDirectMemoryPageAligned();
...
}
直接緩衝區通過在作業系統和JVM之間創建物理記憶體映射文件加快緩衝區數據讀/寫入物理磁碟的速度。放到物理記憶體映射文件中的數據就不歸應用程式控制了,作業系統會自動將物理記憶體映射文件中的數據寫入到物理記憶體中。
通道(Channel)
Channel由java.nio.channels 包定義的。Channel 表示IO 源與目標打開的連接。Channel 類似於傳統的「流」。只不過Channel 本身不能直接訪問數據,Channel 只能與Buffer 進行交互 。
應用程式進行讀寫操作調用函數時,底層調用的作業系統提供給用戶的讀寫API,調用這些API時會生成對應的指令,CPU則會執行這些指令。在電腦剛出現的那段時間,所有讀寫請求的指令都有CPU去執行,過多的讀寫請求會導致CPU無法去執行其他命令,從而CPU的利用率降低。
後來,DMA(Direct Memory Access,直接存儲器訪問)出現了。當IO請求傳到電腦底層時,DMA會向CPU請求,讓DMA去處理這些IO操作,從而可以讓CPU去執行其他指令。DMA處理IO操作時,會請求獲取匯流排的使用權。當IO請求過多時,會導致大量匯流排用於處理IO請求,從而降低效率 。
於是便有了Channel(通道),Channel相當於一個專門用於IO操作的獨立處理器,它具有獨立處理IO請求的能力,當有IO請求時,它會自行處理這些IO請求 。
Java Channel
- 本地文件IO
- FileChannel
- 網路IO
- SocketChanel、ServerSocketChannel:用於TCP傳輸
- DatagramChannel:用於UDP傳輸
獲得通道的方法
對象調用getChannel() 方法
獲取通道的一種方式是對支援通道的對象調用getChannel() 方法。支援通道的類如下:
- FileInputStream
- FileOutputStream
- RandomAccessFile
- DatagramSocket
- Socket
- ServerSocket
例子:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.DatagramChannel;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
public class demo2 {
public static void main(String[] args) throws IOException {
// 本地通道
FileInputStream fileInputStream = new FileInputStream("zwt");
FileChannel channel1 = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream("zwt");
FileChannel channel2 = fileOutputStream.getChannel();
// 網路通道
Socket socket = new Socket();
SocketChannel channel3 = socket.getChannel();
ServerSocket serverSocket = new ServerSocket();
ServerSocketChannel channel4 = serverSocket.getChannel();
DatagramSocket datagramSocket = new DatagramSocket();
DatagramChannel channel5 = datagramSocket.getChannel();
// 最後要關閉通道
FileChannel open = FileChannel.open(Paths.get("zwt"));
SocketChannel open1 = SocketChannel.open();
}
}
getChannel()+非直接緩衝區
- getChannel()獲得通道
- allocate()獲得非直接緩衝區
通過非直接緩衝區讀寫數據,需要通過通道來傳輸緩衝區里的數據
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class demo4 {
public static void main(String[] args) {
FileInputStream is = null;
FileOutputStream os = null;
// 獲得通道
FileChannel inChannel = null;
FileChannel outChannel = null;
// 利用 try-catch-finally 保證關閉
try {
is = new FileInputStream("");
os = new FileOutputStream("");
// 獲得通道
inChannel = is.getChannel();
outChannel = os.getChannel();
// 獲得緩衝區,用於在通道中傳輸數據
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
// 循環將位元組數據放入到buffer中,然後寫入磁碟中
while (inChannel.read(byteBuffer) != -1) {
// 切換模式
byteBuffer.flip();
outChannel.write(byteBuffer);
byteBuffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inChannel != null) {
try {
inChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outChannel != null) {
try {
outChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
open()+直接緩衝區
- 通過open獲得通道
- 通過FileChannel.map()獲取直接緩衝區
使用直接緩衝區時,無需通過通道來傳輸數據,直接將數據放在緩衝區內即可
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class demo5 {
public static void main(String[] args) throws IOException {
// 通過open()方法來獲得通道
FileChannel inChannel = FileChannel.open(Paths.get(""), StandardOpenOption.READ);
// outChannel需要為 READ WRITE CREATE模式
// READ WRITE是因為後面獲取直接緩衝區時模式為READ_WRITE模式
// CREATE是因為要創建新的文件
FileChannel outChannel = FileChannel.open(Paths.get(""), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
// 獲得直接緩衝區
MappedByteBuffer inMapBuf = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer outMapBuf = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());
// 位元組數組
byte[] bytes = new byte[inMapBuf.limit()];
// 因為是直接緩衝區,可以直接將數據放入到記憶體映射文件,無需通過通道傳輸
inMapBuf.get(bytes);
outMapBuf.put(bytes);
// 關閉緩衝區,這裡沒有用try-catch-finally
inChannel.close();
outChannel.close();
}
}
通道間直接傳輸
public static void channelToChannel() throws IOException {
long start = System.currentTimeMillis();
// 通過open()方法來獲得通道
FileChannel inChannel = FileChannel.open(Paths.get(""), StandardOpenOption.READ);
// outChannel需要為 READ WRITE CREATE模式
// READ WRITE是因為後面獲取直接緩衝區時模式為READ_WRITE模式
// CREATE是因為要創建新的文件
FileChannel outChannel = FileChannel.open(Paths.get(""), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
// 通道間直接傳輸
inChannel.transferTo(0, inChannel.size(), outChannel);
// 對應的還有transferFrom
// outChannel.transferFrom(inChannel, 0, inChannel.size());
inChannel.close();
outChannel.close();
}
直接緩衝區VS非直接緩衝區
// getChannel() + 非直接緩衝區耗時
708
// open() + 直接緩衝區耗時
115
// channel transferTo channel耗時
47
直接緩衝區的讀寫速度雖然很快,但是會佔用很多很多記憶體空間。如果文件過大,會使得電腦運行速度變慢
分散和聚集
分散讀取
分散讀取(Scattering Reads)是指從Channel 中讀取的數據「分散」到多個Buffer 中。
注意:按照緩衝區的順序,從Channel 中讀取的數據依次將 Buffer 填滿。
聚集寫入
聚集寫入(Gathering Writes)是指將多個Buffer 中的數據「聚集」到Channel。
按照緩衝區的順序,寫入position 和limit 之間的數據到Channel。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class demo6 {
public static void main(String[] args) throws IOException {
FileInputStream is = new FileInputStream("");
FileOutputStream os = new FileOutputStream("");
FileChannel inChannel = is.getChannel();
FileChannel outChannel = os.getChannel();
// 獲得多個緩衝區,並且放入到緩衝區數組中
ByteBuffer byteBuffer1 = ByteBuffer.allocate(50);
ByteBuffer byteBuffer2 = ByteBuffer.allocate(1024);
ByteBuffer[] byteBuffers = {byteBuffer1, byteBuffer2};
// 分散讀取
inChannel.read(byteBuffers);
byteBuffer1.flip();
byteBuffer2.flip();
// 聚集寫入
outChannel.write(byteBuffers);
}
}
非阻塞式網路通訊
概念
底層原理可見:作業系統-文件IO
比喻:
舉個你去飯堂吃飯的例⼦,你好⽐⽤戶程式,飯堂好⽐作業系統。
阻塞 I/O 好⽐,
你去飯堂吃飯,但是飯堂的菜還沒做好,然後你就⼀直在那⾥等啊等,
等了好⻓⼀段時間終於等到飯堂阿姨把菜端了出來(數據準備的過程),
但是你還得繼續等阿姨把菜(內核空間)打到你的飯盒⾥(⽤戶空間),
經歷完這兩個過程,你才可以離開。
⾮阻塞 I/O 好⽐,
你去了飯堂,問阿姨菜做好了沒有,阿姨告訴你沒,
你就離開了,過⼏⼗分鐘,你⼜來,
飯堂問阿姨,阿姨說做好了,於是阿姨幫你把菜打到你的飯盒⾥,這個過程你是得等待的。
基於⾮阻塞的 I/O 多路復⽤好⽐,
你去飯堂吃飯,發現有⼀排窗⼝,飯堂阿姨告訴你這些窗⼝都還沒做好菜,
等做好了再通知你,於是等啊等( select 調⽤中),過了⼀會阿姨通知你菜做好了,
但是不知道哪個窗⼝的菜做好了,你⾃⼰看吧。
於是你只能⼀個⼀個窗⼝去確認,後⾯發現 5 號窗⼝菜做好了,
於是你讓 5 號窗⼝的阿姨幫你打菜到飯盒⾥,這個打菜的過程你是要等待的,雖然時間不⻓。
打完菜後,你⾃然就可以離開了。
非同步 I/O 好⽐,
你讓飯堂阿姨將菜做好並把菜打到飯盒⾥後,把飯盒送到你⾯前,整個過程你都不需要任何等待。
阻塞式網路通訊
package NIOAndBIO;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class BIO {
public static void main(String[] args) throws IOException {
Thread thread1 = new Thread(() -> {
try {
server();
} catch (IOException e) {
e.printStackTrace();
}
});
Thread thread2 = new Thread(() -> {
try {
client();
} catch (IOException e) {
e.printStackTrace();
}
});
thread1.start();
thread2.start();
}
public static void client() throws IOException {
// 創建客戶端通道
SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 2022));
// 讀取資訊 D:\\bizhi\\bizhi202008\\wallhaven-kwp2qq.jpg
FileChannel fileChannel = FileChannel.open(Paths.get("D:\\\\bizhi\\\\bizhi202008\\\\wallhaven-kwp2qq.jpg"), StandardOpenOption.READ);
// 創建緩衝區
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
// 寫入數據
while (fileChannel.read(byteBuffer) != -1) {
byteBuffer.flip();
socketChannel.write(byteBuffer);
byteBuffer.clear();
}
fileChannel.close();
socketChannel.close();
}
public static void server() throws IOException {
// 創建服務端通道
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
FileChannel fileChannel = FileChannel.open(Paths.get("D:\\\\bizhi\\\\bizhi202008\\\\wallhaven-kwp2qq.jpg"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
// 綁定鏈接
serverSocketChannel.bind(new InetSocketAddress(2022));
// 獲取客戶端的通道
SocketChannel socketChannel = serverSocketChannel.accept();
// 創建緩衝區
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while (socketChannel.read(byteBuffer) != -1) {
byteBuffer.flip();
fileChannel.write(byteBuffer);
byteBuffer.clear();
}
socketChannel.close();
fileChannel.close();
serverSocketChannel.close();
}
}
非阻塞式網路通訊
package NIOAndBIO;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
public class NIO {
public static void main(String[] args) {
Thread thread1 = new Thread(()->{
try {
server();
} catch (IOException e) {
e.printStackTrace();
}
});
Thread thread2 = new Thread(()->{
try {
client();
} catch (IOException e) {
e.printStackTrace();
}
});
thread1.start();
thread2.start();
}
public static void client() throws IOException {
SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 2020));
// 設置為非阻塞模式
socketChannel.configureBlocking(false);
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String str = scanner.next();
byteBuffer.put(str.getBytes());
byteBuffer.flip();
socketChannel.write(byteBuffer);
byteBuffer.clear();
}
byteBuffer.clear();
socketChannel.close();
}
public static void server() throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.bind(new InetSocketAddress(2020));
// 獲得選擇器
Selector selector = Selector.open();
// 將通道註冊到選擇器中,設定為接收操作
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
// 輪詢接受
while (selector.select() > 0) {
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
// 獲得事件的key
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
if (key.isAcceptable()) {
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
} else if (key.isReadable()) {
// 從選擇器中獲取通道
SocketChannel socketChannel = (SocketChannel) key.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
while (socketChannel.read(byteBuffer) != -1) {
int len = byteBuffer.limit();
byteBuffer.flip();
System.out.println(new String(byteBuffer.array(), 0, len));
byteBuffer.clear();
}
socketChannel.close();
}
iterator.remove();
}
}
serverSocketChannel.close();
}
}
選擇器
選擇器(Selector)是SelectableChannle 對象的多路復用器,Selector 可以同時監控多個SelectableChannel 的IO 狀況,也就是說,利用Selector 可使一個單獨的執行緒管理多個Channel。Selector 是非阻塞IO 的核心 。
選擇器的創建
// 創建一個選擇器
Selector selector = Selector.open();
綁定選擇器
通過調用通道的register方法可以綁定選擇器,register方法有兩個參數
- Selector:即綁定哪個選擇器
- ops:監聽事件類型。ops有4個值可以選擇,為SelectionKey的靜態屬性
// 讓選擇器監聽一種狀態
myChannel.register(selector, SelectionKey.OP_READ);
// 讓選擇器監聽多種狀態
myChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_ACCEPT);
SelectionKey
表示SelectableChannel 和Selector 之間的註冊關係。每次向選擇器註冊通道時就會選擇一個事件(選擇鍵)。選擇鍵包含兩個表示為整數值的操作集。操作集的每一位都表示該鍵的通道所支援的一類可選擇操作。