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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

通道

2019-11-14 08:45:48
字體:
供稿:網(wǎng)友

0.概述

通道(channel)是java nio 的第二個(gè)主要?jiǎng)?chuàng)新。它們既不是一個(gè)擴(kuò)展也不是一項(xiàng)增強(qiáng),而是全新、極好的java I/O 示例,提供與I/O服務(wù)直接連接,Channel類提供了維持平臺獨(dú)立性所需要的抽象過程。多數(shù)情況下,通道與操作系統(tǒng)的文件描述(fd)或者文件句柄有著一對一的關(guān)系。 這里寫圖片描述

1.通道的基礎(chǔ)

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;}

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

1.2打開通道

通道主要有兩種類型:文件通道和套接字通道。FileChannel對象只能通過一個(gè)打開的RandomaccessFile、FileInputStream、FileOutPutStream 對象上調(diào)用Channel方法來獲取。Socket通道對象可以直接由Socket工廠方法進(jìn)行創(chuàng)建。 下面給出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,所有其實(shí)雙向的。由于接口本身沒有定義新的API方法,它是一種用來聚集它自己以一個(gè)新名稱繼承的多個(gè)接口的便捷接口。 3.值得說明的是一個(gè)文件可以在不同的權(quán)限打開,從FileInputStream對象的getChannel( )方法獲取的FileChannel對象是只讀的,不過從接口聲明的角度來看卻是雙向的,因?yàn)镕ileChannel實(shí)現(xiàn)ByteChannel接口。在這樣一個(gè)通道上調(diào)用write( )方法將拋出未經(jīng)檢查的NonWritableChannelException異常,因?yàn)镕ileInputStream對象總是以read-only的權(quán)限打開文件。

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關(guān)閉通道

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

2.Scatter/Gather

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

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()); //要寫數(shù)據(jù)放到一個(gè)字節(jié)緩沖區(qū)數(shù)組中 ByteBuffer [] buffers={header,body}; header.flip(); body.flip(); fileChannel.write(buffers); fileChannel.close(); }}

參考文獻(xiàn)[1]https://book.douban.com/subject/1433583/


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产一级在线观看视频 | 免费观看在线 | 粉嫩蜜桃麻豆免费大片 | av中文在线观看 | 国产羞羞视频免费在线观看 | 红杏亚洲影院一区二区三区 | 91成人久久 | 美国一级免费视频 | 亚洲精品tv久久久久久久久久 | 成人啪啪18免费网站 | 哪里可以看免费的av | 国产精品高清一区 | 国产午夜精品理论片a级探花 | 国产精品亚洲激情 | 91久久综合 | 欧美一区二区三区中文字幕 | 中国产一级毛片 | 亚洲射逼 | 黄色久| 久久精品视频3 | 国产成人精品无人区一区 | 一本色道久久99精品综合蜜臀 | 黄色影院在线观看视频 | 久久久精品视频网站 | 女人裸体让男人桶全过程 | 国产精品视频2021 | 久在线播放 | 久久精品视频在线看99 | 四虎久草 | 精品久久久久久国产 | 国产一区二区三区四区波多野结衣 | 成人一级视频在线观看 | 欧美日韩精品一区二区三区蜜桃 | 91久久国产综合精品女同国语 | 成人免费一区二区 | 亚洲免费视频大全 | 日本欧美一区二区三区视频麻豆 | 国产成年人网站 | 久久精品中文字幕一区 | 免费a级网站 | 日本精品二区 |