NIO之通道(Channel)的原理與獲取以及數據傳輸與內存映射文件
阿新 • • 發佈:2018-09-09
rom 讀取數據 mode catch dst 1.7 rto class frame
- 通道(Channel):
由java.nio.channels包定義的,Channel表示IO源與目標打開的連接,Channel類似於傳統的“流”,只不過Channel本身不能直接訪問數據,Channel只能與Buffer進行交互。通道主要用於傳輸數據,從緩沖區的一側傳到另一側的實體(如文件、套接字...),反之亦然;通道是訪問IO服務的導管,通過通道,我們可以以最小的開銷來訪問操作系統的I/O服務;順便說下,緩沖區是通道內部發送數據和接收數據的端點。
在標準的IO當中,都是基於字節流/字符流進行操作的,而在NIO中則是是基於Channel和Buffer進行操作,其中的Channel的雖然模擬了流的概念,實則大不相同。
區別 | Stream | Channel |
---|---|---|
支持異步 | 不支持 | 支持 |
是否可雙向傳輸數據 | 不能,只能單向 | 可以,既可以從通道讀取數據,也可以向通道寫入數據 |
是否結合Buffer使用 | 不 | 必須結合Buffer使用 |
性能 | 較低 | 較高 |
- 傳統與革新:
早一代IO操作是由CPU負責IO接口:
新一代DMA授權處理IO接口:
通道(Channel)模式:
- 代碼示例:
1 package com.expgiga.NIO; 2 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.nio.ByteBuffer; 7 import java.nio.MappedByteBuffer; 8 import java.nio.channels.FileChannel; 9 importjava.nio.file.Paths; 10 import java.nio.file.StandardOpenOption; 11 12 /** 13 * 一、Channel:用於源節點與目標節點之間的連接。在Java NIO中,負責緩沖區中數據傳輸,Channel本身不存儲數據,因此需要配合緩沖區進行傳輸。 14 * 15 * 二、Channel的實現類: 16 * java.nio.channels.Channel 接口: 17 * |-- FileChannel 18 * |-- SocketChannel 19 * |-- ServerSocketChannel 20 * |-- DatagramChannel 21 * 22 * 三、獲取通道Channel 23 * 1.Java針對支持通道的類提供了getChannel()方法 24 * 本地IO 25 * FileInputStream/FileOutputStream 26 * RandomAccessFile 27 * 28 * 網絡IO: 29 * Socket 30 * ServerSocket 31 * DatagramSocket 32 * 33 * 2.在jdk1.7中的NIO.2針對各個通道提供了靜態方法open() 34 * 35 * 3.在jdk1.7中的NIO.2的Files工具類的newByteChannel() 36 * 37 * 四、通道之間的數據傳輸 38 * transferFrom() 39 * transferTo() 40 * 41 */ 42 public class TestChannel { 43 44 public static void main(String[] args) throws IOException { 45 46 /* 47 * 1.利用通道完成文件的復制(非直接緩沖區) 48 */ 49 FileInputStream fis = null; 50 FileOutputStream fos = null; 51 52 FileChannel inChannel = null; 53 FileChannel outChannel = null; 54 55 try { 56 fis = new FileInputStream("1.jpg"); 57 fos = new FileOutputStream("2.jpg"); 58 //1.獲取通道 59 inChannel = fis.getChannel(); 60 outChannel = fos.getChannel(); 61 62 //2.分配指定大小的緩沖區 63 ByteBuffer buffer = ByteBuffer.allocate(1024); 64 65 //3.將通道中的數據緩沖區中 66 while (inChannel.read(buffer) != -1) { 67 68 buffer.flip();//切換成都數據模式 69 70 //4.將緩沖區中的數據寫入通道中 71 outChannel.write(buffer); 72 buffer.clear();//清空緩沖區 73 } 74 } catch (Exception e) { 75 e.printStackTrace(); 76 } finally { 77 if (outChannel != null) { 78 try { 79 outChannel.close(); 80 } catch (IOException e) { 81 e.printStackTrace(); 82 } 83 } 84 85 if (inChannel != null) { 86 try { 87 inChannel.close(); 88 } catch (IOException e) { 89 e.printStackTrace(); 90 } 91 } 92 93 if (fis != null) { 94 try { 95 fis.close(); 96 } catch (IOException e) { 97 e.printStackTrace(); 98 } 99 } 100 101 if (fos != null) { 102 try { 103 fos.close(); 104 } catch (IOException e) { 105 e.printStackTrace(); 106 } 107 } 108 } 109 110 111 /* 112 * 2.利用(直接緩沖區)通道完成文件的復制(內存映射文件的方式) 113 */ 114 115 long start = System.currentTimeMillis(); 116 FileChannel inChannel2 = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ); 117 FileChannel outChannel2 = FileChannel.open(Paths.get("3.jpg"), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE); 118 119 //內存映射文件 120 MappedByteBuffer inMappedBuf = inChannel2.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size()); 121 MappedByteBuffer outMappedBuf = outChannel2.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size()); 122 123 //直接對緩沖區進行數據讀寫操作 124 byte[] dst = new byte[inMappedBuf.limit()]; 125 inMappedBuf.get(dst); 126 outMappedBuf.put(dst); 127 128 inChannel2.close(); 129 outChannel2.close(); 130 131 long end = System.currentTimeMillis(); 132 System.out.println("耗費的時間為:" + (end - start)); 133 134 /* 135 * 通道之間的數據傳輸(直接緩沖區) 136 */ 137 FileChannel inChannel3 = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ); 138 FileChannel outChannel3 = FileChannel.open(Paths.get("3.jpg"), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE); 139 140 inChannel3.transferTo(0, inChannel3.size(), outChannel3); 141 //等價於 142 // outChannel3.transferFrom(inChannel3, 0, inChannel3.size()); 143 144 inChannel3.close(); 145 outChannel3.close(); 146 } 147 }
NIO之通道(Channel)的原理與獲取以及數據傳輸與內存映射文件