通道(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)系。
與緩沖區(qū)不同,通道的API主要由接口指定不同的操作系統(tǒng)的實(shí)現(xiàn)會有著根本的差異 ,所以通道API僅僅描述了可以做什么。下面來看下FileChannel類 為例繼承和實(shí)現(xiàn)哪些接口 ,F(xiàn)ileChannel具體有哪些接口可以查看JDK文檔或者FileChannel實(shí)現(xiàn)。 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)子接口覆蓋父類的接口情況,子接口可以覆蓋父接口中的方法的意義
通道主要有兩種類型:文件通道和套接字通道。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.通道可以是單向(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(); } }}與緩沖區(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)生任何操作,只會立即返回。
通道提供了一種被稱為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ū)的最大空間被消耗完。
參考文獻(xiàn)[1]https://book.douban.com/subject/1433583/
新聞熱點(diǎn)
疑難解答
圖片精選