Java IO 之 OutputStream源碼
InputStream中有幾個 read() 方法和 OutputStream中有幾個 write() 方法。它們是一一對應的,而核心的是read()和write()方法。它們都沒實現,所有本質調用是各自實現類實現的該兩個方法。
read() 和 write() ,對應著係統的Input和Output,即係統的輸出輸入。
二、OutputStream
也是一個抽象類,即表示所有字節輸入流實現類的基類。它的作用就是抽象地表示所有要輸出到的目標,例如常見的FileOutStream、FilterOutputStream等。它實現了java.io.Closeable和java.io.Flushable兩個接口。其中空實現了flush方法,即擁有刷新緩存區字節數組作用。
那些輸出目標呢?比如:
1) 字節數組(不代表String類,但可以轉換)
2) 文件
3) 管道(多線程環境中的數據源)
等等
FilterOutputStream是為各種OutputStream實現類提供的“裝飾器模式”的基類。將屬性或者有用的接口與輸出流連接起來。
三、細解OutputStream源碼的核心
一樣的,先看源碼:
/** * 所有字節輸出流實現類的基類 */ public abstract class SOutputStream implements Closeable, Flushable { // 將指定的字節寫入輸出流 public abstract void write(int b) throws IOException; // 將指定的byte數組的字節全部寫入輸出流 public void write(byte b[]) throws IOException { write(b, 0, b.length); } // 將指定的byte數組中從偏移量off開始的len個字節寫入輸出流 public void write(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } for (int i = 0 ; i < len ; i++) { write(b[off + i]); } } // 刷新輸出流,並強製寫出所有緩衝的輸出字節 public void flush() throws IOException { } // 關閉輸出流,並釋放與該流有關的所有資源 public void close() throws IOException { } }
其中三個核心的write()方法,對應著三個InputStream的read()方法:
1. abstract void write(int b) 抽象方法
public abstract void write(int b) throws IOException;
對應著,InputStream的read()方法,此方法依舊是抽象方法。因為子類必須實現此方法的一個實現。這就是輸入流的關鍵方法。
二者,下麵兩個write方法中調用了此核心方法。
可見,核心的是read()和write()方法在傳統的IO是多麼重要。
2. void write(byte b[]) 方法
public void write(byte b[]) throws IOException
將指定的byte數組的字節全部寫入輸出流。該效果實際上是由下一個write方法實現的,隻是調用的額時候指定了長度:
3. void write(byte b[], int off, int len) 方法
public void write(byte b[], int off, int len) throws IOException
將指定的byte數組中從偏移量off開始的len個字節寫入輸出流。代碼詳細流程解讀如下:
a. 如果
b
為null
,則拋出NullPointerException
。b. 如果
off
為負,或len
為負,或者off+len
大於數組b
的長度,則拋出 IndexOutOfBoundsException。c. 將數組
b
中的某些字節按順序寫入輸出流;元素b[off]
是此操作寫入的第一個字節,b[off+len-1]
是此操作寫入的最後一個字節。
四、小結
重要的事情說三遍:
OutputStream 解讀對照著 InputStream來看!注意 一個write對應一個read
OutputStream 解讀對照著 InputStream來看!注意 一個write對應一個read
OutputStream 解讀對照著 InputStream來看!注意 一個write對應一個read
最後更新:2017-05-22 12:01:24