麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

通道

2019-11-14 09:05:24
字體:
來源:轉載
供稿:網友

0.概述

通道(channel)是java nio 的第二個主要創新。它們既不是一個擴展也不是一項增強,而是全新、極好的java I/O 示例,提供與I/O服務直接連接,Channel類提供了維持平臺獨立性所需要的抽象過程。多數情況下,通道與操作系統的文件描述(fd)或者文件句柄有著一對一的關系。 這里寫圖片描述

1.通道的基礎

1.1Channel 接口

public interface Channel extends Closeable { /** * Tells whether or not this channel is open. * * @return <tt>true</tt> if, and only if, this channel is open */ public boolean isOpen(); /** * Closes this channel. * * <p> After a channel is closed, any further attempt to invoke I/O * Operations upon it will cause a {@link ClosedChannelException} to be * thrown. * * <p> If this channel is already closed then invoking this method has no * effect. * * <p> This method may be invoked at any time. If some other thread has * already invoked it, however, then another invocation will block until * the first invocation is complete, after which it will return without * effect. </p> * * @throws IOException If an I/O error occurs */ public void close() throws IOException;}

與緩沖區不同,通道的API主要由接口指定不同的操作系統的實現會有著根本的差異 ,所以通道API僅僅描述了可以做什么。下面來看下FileChannel類 為例繼承和實現哪些接口 ,FileChannel具體有哪些接口可以查看JDK文檔或者FileChannel實現。 FileChannel 1.可以從頂層的Channel接口看到,對所有的通道來說是只有兩種共同操作:檢查一個通道是否打開(IsOpen())和關閉一個打開的通道(close())。 2.InterruptibleChannel是一個標記接口,當被通道使用時可以標示該通道是可以中斷的。 3.從Channel引申出的其它接口都是面向字節的子接口,包括WritableByteChannel和ReadableByteChannel 4.看了JDK源碼不難發現出現子接口覆蓋父類的接口情況,子接口可以覆蓋父接口中的方法的意義

1.2打開通道

通道主要有兩種類型:文件通道和套接字通道。FileChannel對象只能通過一個打開的RandomaccessFile、FileInputStream、FileOutPutStream 對象上調用Channel方法來獲取。Socket通道對象可以直接由Socket工廠方法進行創建。 下面給出FileChannel通道的使用Demo

public class FileNio { public static void main(String[] args) throws Exception { FileInputStream inputStream = new FileInputStream("test.txt"); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); FileChannel fileChannel = inputStream.getChannel(); while (fileChannel.read(byteBuffer) != -1) { byteBuffer.flip(); byte[] b = byteBuffer.array(); System.out.PRintln(new String(b, 0, byteBuffer.remaining())); byteBuffer.clear(); } inputStream.close(); fileChannel.close(); }}

1.3使用通道

1.通道可以是單向(unidirectional)或者雙向的(bidirectional)。所謂單向的只可以讀或者只可以寫,雙向的既可以讀也可以寫。 2.ByteChannel繼承了ReadableByteChannel, WritableByteChannel,所有其實雙向的。由于接口本身沒有定義新的API方法,它是一種用來聚集它自己以一個新名稱繼承的多個接口的便捷接口。 3.值得說明的是一個文件可以在不同的權限打開,從FileInputStream對象的getChannel( )方法獲取的FileChannel對象是只讀的,不過從接口聲明的角度來看卻是雙向的,因為FileChannel實現ByteChannel接口。在這樣一個通道上調用write( )方法將拋出未經檢查的NonWritableChannelException異常,因為FileInputStream對象總是以read-only的權限打開文件。

package java.nio.channels;import java.io.IOException;/** * A channel that can read and write bytes. This interface simply unifies * {@link ReadableByteChannel} and {@link WritableByteChannel}; it does not * specify any new operations. * * @author Mark Reinhold * @author JSR-51 Expert Group * @since 1.4 */public interface ByteChannel extends ReadableByteChannel, WritableByteChannel{}

文件copyDemo

public class FileCopyDemo { public static void main(String[] args) throws IOException { String srcFile = "/Users/hsc/src.mp4"; String destFile = "/Users/hsc/dest.mp4"; FileChannel srcChannel = null; FileChannel destChannel = null; FileInputStream input = null; FileOutputStream output = null; try { input = new FileInputStream(srcFile); output = new FileOutputStream(destFile); srcChannel = input.getChannel(); destChannel = output.getChannel(); copy(srcChannel, destChannel); } catch (Exception ex) { System.out.println(ex); } finally { if (srcChannel != null) { srcChannel.close(); } if (destChannel != null) { destChannel.close(); } if (input != null) { input.close(); } if (output != null) { output.close(); } } } static void copy(FileChannel src, FileChannel dest) throws IOException { ByteBuffer byteBuffer = ByteBuffer.allocate(1024); while (src.read(byteBuffer) != -1) { byteBuffer.flip(); while (byteBuffer.hasRemaining()) { dest.write(byteBuffer); } byteBuffer.clear(); } }}

1.4關閉通道

與緩沖區不同,通道不能被重復使用。一個打開的通道即代表一個特定I/O服務的特定連接并封裝該連接的狀態。當該通道關閉時候,關聯的文件描述符或者句柄也會被關閉以及相應的流也會被關閉(如FileInputStream)。可以通過isOpen( )方法來測試通道的開放狀態。如果返回true值,那么該通道可以使用。如果返回false值,那么該通道已關閉,不能再被使用。嘗試進行任何需要通道處于開放狀態作為前提的操作,如讀、寫等都會導致ClosedChannelException異常。 調用通道的close( )方法時,可能會導致在通道關閉底層I/O服務的過程中線程暫時阻塞,哪怕該通道處于非阻塞模式。通道關閉時的阻塞行為(如果有的話)是高度取決于操作系統或者文件系統的。在一個通道上多次調用close( )方法是沒有壞處的,但是如果第一個線程在close( )方法中阻塞,那么在它完成關閉通道之前,任何其他調用close( )方法都會阻塞。后續在該已關閉的通道上調用close( )不會產生任何操作,只會立即返回。

2.Scatter/Gather

通道提供了一種被稱為Scatter/Gather的重要功能(有時候也被稱為矢量IO),它是指在多個緩沖區上實現一個簡單的I/O操作。對于一個write操作而言,數據是從幾個緩沖區按順序抽取(稱為gather)并沿著通道發送的。該gather過程的效果就好比全部緩沖區的內容被連結起來,并在發送數據前存放到一個大的緩沖區中(數組中)。對于read操作而言,從通道讀取的數據會按順序被散布(稱為scatter)到多個緩沖區,將每個緩沖區填滿直至通道中的數據或者緩沖區的最大空間被消耗完。這里寫圖片描述

public class ScatterDemo{ public static void main(String[] args) throws IOException { FileInputStream inputStream=new FileInputStream("test.txt"); ByteBuffer header = ByteBuffer.allocate (10); ByteBuffer body = ByteBuffer.allocate (50); ByteBuffer []byteBuffers={header,body}; FileChannel fileChannel=inputStream.getChannel(); while (fileChannel.read(byteBuffers)!=-1) { printBuffer(header); printBuffer(body); } fileChannel.close(); } private static void printBuffer(ByteBuffer byteBuffer) { byteBuffer.flip(); while (byteBuffer.hasRemaining()) { byte[] b = byteBuffer.array(); System.out.println(new String(b, 0, byteBuffer.remaining())); byteBuffer.position(byteBuffer.remaining()); } byteBuffer.clear(); }}public class GatherDemo { public static void main(String[] args) throws IOException { FileOutputStream outputStream=new FileOutputStream("test.txt"); FileChannel fileChannel=outputStream.getChannel(); ByteBuffer header=ByteBuffer.allocate(10); ByteBuffer body=ByteBuffer.allocate(20); header.put("header".getBytes()); body.put("body".getBytes()); //要寫數據放到一個字節緩沖區數組中 ByteBuffer [] buffers={header,body}; header.flip(); body.flip(); fileChannel.write(buffers); fileChannel.close(); }}

參考文獻[1]https://book.douban.com/subject/1433583/


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产免费一区二区三区在线能观看 | 免费在线观看毛片视频 | 国产一级淫 | 国产99精品视频 | 国产成人在线观看免费网站 | 羞羞视频2023 | 中文字幕www. | 一边吃奶一边摸下娇喘 | 成人毛片视频在线观看 | 欧美成人免费小视频 | 九九热在线视频观看 | 久久思思爱 | 亚洲欧美国产高清 | 最新福利在线 | 欧美成人二区 | 日本成人一区二区三区 | 国产免费人做人爱午夜视频 | 久草在线最新 | 一级黄色欧美 | 精品国产一区二区三区在线观看 | 中文亚洲视频 | 国产午夜小视频 | 久久精品视频在线看99 | 国产乱淫av一区二区三区 | 亚洲91网| 久久久久性 | 最近中文字幕一区二区 | 一日本道久久久精品国产 | 91麻豆蜜桃一区二区三区 | 伊久在线 | 国产精品麻豆一区二区三区 | 午夜视频在线观 | 成人国产在线视频 | 天堂成人一区二区三区 | 国产一区二区观看 | 在线播放视频一区二区 | 亚洲一级片免费观看 | 久久久一区二区精品 | 天天躁狠狠躁夜躁2020挡不住 | 视频一区国产 | 欧美一级高潮 |