日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Java NIO 三大组件之 Channel

發布時間:2024/10/8 编程问答 36 如意码农
生活随笔 收集整理的這篇文章主要介紹了 Java NIO 三大组件之 Channel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java NIO 之 Channel

一、什么是Channel

Channel用于源節點(例如磁盤)與目的節點的連接,它可以進行讀取,寫入,映射和讀/寫文件等操作。

在Java NIO中負責緩沖區中數據的傳輸。Channel本省不存儲數據,因此需要配合緩沖區進行傳輸。(個人理解其實就是相當于保存兩通信地間的上寫問和進行讀寫操作,相當于郵局)

二、通道的主要實現類

java.nio.channels.Channel接口:

  • FileChannel(本地)
  • SocketChannel(TCP)
  • ServerSocketChannel(TCP)
  • DatagramChannel(UDP)

三、獲取通道的方式

  1. Java 針對支持通道的類提供了getChannel()方法

    • FileInputStream/FileOutputStream(本地IO)
    • RandomAccessFile(本地IO)
    • Socket(網絡IO)
    • ServerSocket(網絡IO)
    • DatagramSocket(網絡IO)
  2. 在JDK1.7中NIO.2針對各個通道提供了靜態方法open()
  3. 在JDK1.7中NIO.2的Files工具類的newByteChannel()

四、通道之間的數據傳輸

  • transferFrom()
  • transferTo()
@Test
public void test3() throws IOException {
FileChannel inChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.READ);
FileChannel outchannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE); inChannel.transferTo(0,inChannel.size(),outchannel);
//outchannel.transferFrom(inChannel, 0,inChannel.size());
inChannel.close();
outchannel.close();
}

其他兩種兩種傳輸方式

  1. 使用直接緩沖區完成文件的復制(內存映射文件)
//實驗證明如果傳大型文件內存會出現大量占用,如果GC不對內存進行回收,無法確定OS何時把數據寫入磁盤
@Test
public void test2() throws IOException {
FileChannel inChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.READ);
FileChannel outchannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE); //內存映射文件
MappedByteBuffer inMappedBuf = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer outMappedBuf = outchannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size()); //直接對緩存區進行數據的讀寫操作
byte[] dst = new byte[inMappedBuf.limit()];
inMappedBuf.get(dst);
outMappedBuf.put(dst); }
  1. 利用通道完成文件的復制(非直接緩沖區)
@Test
public void test1(){
String path = this.getClass().getClassLoader().getResource("1.jpg").getPath(); FileInputStream fis = null;
FileOutputStream fos = null; FileChannel fisChannel = null;
FileChannel fosChannel = null;
try {
fis = new FileInputStream(path);
fos = new FileOutputStream("src/main/resources/2.jpg"); //獲取通道
fisChannel = fis.getChannel();
fosChannel = fos.getChannel(); //二、分配指定大小的緩沖區
ByteBuffer buf = ByteBuffer.allocate(1024); //三、將通道中的數據存入緩沖區中
while(fisChannel.read(buf) != -1){
buf.flip(); //切換讀取數據的模式
//四、將緩沖區的數據寫入通道中
fosChannel.write(buf);
buf.clear();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
} try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

分散(Scatter)與聚集(Gather)

  • 分散讀取(Scattering Reads): 將通道中的數據分散到多個緩沖區中
  • 聚集寫入(Gathering Writes):將多個緩沖區中的數據聚集到通道中

總結

以上是生活随笔為你收集整理的Java NIO 三大组件之 Channel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。