標籤:通訊 關閉 輸出 程式 迴圈 long throw public support
1.InputStream和OutputStream簡介
在java中InputStream和OutputStream分別代表位元組輸入資料流和位元組輸出資料流,表示以位元組的方式來實現進程或者程式的通訊,InputStream是輸入資料流,表示以位元組的方式從檔案(FileInputStream)或者位元組數組(ByteArrayInputStream)等讀取資料,與之相對應的OutputStream是輸出資料流,表示以位元組的方式向檔案(FileOutputStream)或者位元組數組(ByteArrayOutputStream)等寫入資料,InputStream和OutputStream分別是所有的位元組流的超類,定義了位元組輸入資料流和位元組輸出資料流的抽象方法和公用實現,這裡使用設計模式中的模版模式,超類定義一些公用的實現和相關的方法的約束,子類實現。下面讓我們來看一些InputStream和OutputStream的源碼
2.InputStream源碼解析
1 package java.io; 2 public abstract class InputStream implements Closeable { 3 4 //定義可以跳過的最大位元組數 5 private static final int MAX_SKIP_BUFFER_SIZE = 2048; 6 7 //抽象讀取方法,定義讀取一個位元組的方法約束,讓子類去實現, 8 public abstract int read() throws IOException; 9 10 //把讀取的位元組放到byte數組中,返回的是讀取位元組的數量11 public int read(byte b[]) throws IOException {12 return read(b, 0, b.length);13 }14 //具體的讀取位元組到位元組數組的實現,並提供讀取到位元組數組b的起始位置off和讀入的長度len15 public int read(byte b[], int off, int len) throws IOException {16 if (b == null) {17 throw new NullPointerException();18 } else if (off < 0 || len < 0 || len > b.length - off) {19 throw new IndexOutOfBoundsException();20 } else if (len == 0) {21 return 0;22 }23 //先讀取一個位元組,如果返回-1,說明當前流中沒有資料,直接返回-124 int c = read();25 if (c == -1) {26 return -1;27 }28 //如果有資料,則把資料存到b中29 b[off] = (byte)c;30 31 int i = 1;32 try {33 //通過迴圈讀取,然後把讀取的內容存到傳入的buffer中34 for (; i < len ; i++) {35 c = read();36 //如果返回-1,則表示當前讀取到檔案的結尾37 if (c == -1) {38 break;39 }40 b[off + i] = (byte)c;41 }42 } catch (IOException ee) {43 }44 return i;//返回讀取的位元組的數量45 }46 47 //返回真實跳過的位元組的數量48 public long skip(long n) throws IOException {49 50 long remaining = n;51 int nr;52 //如果當前要求跳過0個,則直接返回053 if (n <= 0) {54 return 0;55 }56 //字元緩衝區的大小不能大於2048.跳過的數量n可能大於2048.57 //這裡有個原因,為什麼不直接聲明一個和跳過位元組n一樣大小的數組。因為記憶體是個很昂貴的資源,如果一個檔案很大的話,一次在記憶體中聲明一個很大的記憶體,58 //一方面會佔用很大的記憶體,另一方面也影響記憶體回收59 60 61 int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);62 byte[] skipBuffer = new byte[size];//定義一個緩衝記錄跳過的位元組的數量63 while (remaining > 0) {64 //如果跳過的數量大於2048,則通過緩衝區多次執行read方法計算跳過的數量65 nr = read(skipBuffer, 0, (int)Math.min(size, remaining));66 if (nr < 0) {67 break;68 }69 remaining -= nr;70 }71 //返回實際跳過的位元組的數量72 return n - remaining;73 }74 //返回有多少資料能夠讀取75 public int available() throws IOException {76 return 0;77 }78 79 //關閉流,留給子類去實現80 public void close() throws IOException {}81 82 //標記一個位置,然後執行reset方法的時候可以重新回到標記的位置83 public synchronized void mark(int readlimit) {}84 85 //重新從標記的位置讀取86 public synchronized void reset() throws IOException {87 throw new IOException("mark/reset not supported");88 }89 90 //表明是否支援標記功能91 public boolean markSupported() {92 return false;93 }94 95 }
3.OutputSteam源碼解析
1 public abstract class OutputStream implements Closeable, Flushable { 2 //寫入一個位元組b到輸入資料流 3 public abstract void write(int b) throws IOException; 4 5 //把位元組數組b中的位元組寫入到輸入資料流中 6 public void write(byte b[]) throws IOException { 7 write(b, 0, b.length); 8 } 9 10 //把位元組數組b中的位元組寫入到輸入資料流中,並提供控制參數,從位元組數組的那個位置開始讀取,以及讀取多長的長度11 public void write(byte b[], int off, int len) throws IOException {12 if (b == null) {13 throw new NullPointerException();14 } else if ((off < 0) || (off > b.length) || (len < 0) ||15 ((off + len) > b.length) || ((off + len) < 0)) {16 throw new IndexOutOfBoundsException();17 } else if (len == 0) {18 return;19 }20 for (int i = 0 ; i < len ; i++) {21 write(b[off + i]);22 }23 }24 25 //重新整理輸出資料流,強制把字元緩衝中的位元組從記憶體中寫入到輸出資料流中,如果輸出資料流是記憶體比如ByteArrayOutputStream等,則該實現為空白26 27 public void flush() throws IOException {28 }29 30 //關閉輸出資料流,並釋放資源,如果子類不需要,則該實現為空白31 public void close() throws IOException {32 }33 34 }
4.結語
雖然InputStream和OutputStream的兩個抽象類別的實現比較簡單,代碼量比較少,但是我們還是可以從中學到一些東西,比如模版設計模式的使用方法,代碼中對方法參數的校正邏輯等。多多體會別人寫的代碼,總會學到一些東西,或許現在看不出來有什麼用,但是對你的影響會慢慢看出來的。
===============================================================
努力工作,用心生活
===============================================================
java(jdk1.7) IO系列01之InputStream和OutputStream解析