本章內容包括3個部分:BufferedOutputStream介紹,BufferedOutputStream原始碼,以及BufferedOutputStream使用示例。
轉載請註明出處:http://www.cnblogs.com/skywang12345/p/io_13.html
BufferedOutputStream 介紹
BufferedOutputStream 是緩衝輸出流。它繼承於FilterOutputStream。
BufferedOutputStream 的作用是為另一個輸出流提供“緩衝功能”。
BufferedOutputStream 函式列表
BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStream out, int size) synchronized void close()
synchronized void flush()
synchronized void write(byte[] buffer, int offset, int length)
synchronized void write(int oneByte)
BufferedOutputStream 原始碼分析(基於jdk1.7.40)
package java.io; public class BufferedOutputStream extends FilterOutputStream {
// 儲存“緩衝輸出流”資料的位元組陣列
protected byte buf[]; // 緩衝中資料的大小
protected int count; // 建構函式:新建位元組陣列大小為8192的“緩衝輸出流”
public BufferedOutputStream(OutputStream out) {
this(out, 8192);
} // 建構函式:新建位元組陣列大小為size的“緩衝輸出流”
public BufferedOutputStream(OutputStream out, int size) {
super(out);
if (size <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
} // 將緩衝資料都寫入到輸出流中
private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);
count = 0;
}
} // 將“資料b(轉換成位元組型別)”寫入到輸出流中
public synchronized void write(int b) throws IOException {
// 若緩衝已滿,則先將緩衝資料寫入到輸出流中。
if (count >= buf.length) {
flushBuffer();
}
// 將“資料b”寫入到緩衝中
buf[count++] = (byte)b;
} public synchronized void write(byte b[], int off, int len) throws IOException {
// 若“寫入長度”大於“緩衝區大小”,則先將緩衝中的資料寫入到輸出流,然後直接將陣列b寫入到輸出流中
if (len >= buf.length) {
flushBuffer();
out.write(b, off, len);
return;
}
// 若“剩餘的緩衝空間 不足以 儲存即將寫入的資料”,則先將緩衝中的資料寫入到輸出流中
if (len > buf.length - count) {
flushBuffer();
}
System.arraycopy(b, off, buf, count, len);
count += len;
} // 將“緩衝資料”寫入到輸出流中
public synchronized void flush() throws IOException {
flushBuffer();
out.flush();
}
}
說明:
BufferedOutputStream的原始碼非常簡單,這裡就BufferedOutputStream的思想進行簡單說明:BufferedOutputStream通過位元組陣列來緩衝資料,當緩衝區滿或者使用者呼叫flush()函式時,它就會將緩衝區的資料寫入到輸出流中。
示例程式碼
關於BufferedOutputStream中API的詳細用法,參考示例程式碼(BufferedOutputStreamTest.java):
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Scanner; /**
* BufferedOutputStream 測試程式
*
* @author skywang
*/
public class BufferedOutputStreamTest { private static final int LEN = 5;
// 對應英文字母“abcddefghijklmnopqrsttuvwxyz”
private static final byte[] ArrayLetters = {
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
}; public static void main(String[] args) {
testBufferedOutputStream() ;
} /**
* BufferedOutputStream的API測試函式
*/
private static void testBufferedOutputStream() { // 建立“檔案輸出流”對應的BufferedOutputStream
// 它對應緩衝區的大小是16,即緩衝區的資料>=16時,會自動將緩衝區的內容寫入到輸出流。
try {
File file = new File("out.txt");
OutputStream out =
new BufferedOutputStream(
new FileOutputStream(file), 16); // 將ArrayLetters陣列的前10個位元組寫入到輸出流中
out.write(ArrayLetters, 0, 10);
// 將“換行符\n”寫入到輸出流中
out.write('\n'); // TODO!
//out.flush(); readUserInput() ; out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 讀取使用者輸入
*/
private static void readUserInput() {
System.out.println("please input a text:");
Scanner reader=new Scanner(System.in);
// 等待一個輸入
String str = reader.next();
System.out.printf("the input is : %s\n", str);
}
}
執行結果:
生成檔案“out.txt”,檔案的內容是“abcdefghij”。
分步測試:
分別按照下面3種步驟測試程式,來檢視緩衝區大小以及flush()的作用。
第1種:原始程式。
(01) 執行程式。在程式等待使用者輸入時,檢視“out.txt”的文字內容;發現:內容為空。
(02) 執行程式。在使用者輸入之後,檢視“out.txt”的文字內容;發現:內容為“abcdefghij”。
從中,我們發現(01)和(02)的結果不同;之所以(01)中的out.txt內容為空,是因為out.txt對應的緩衝區大小是16位元組,而我們只寫入了11個位元組,所以,它不會執行清空緩衝操作(即,將緩衝資料寫入到輸出流中)。
而(02)對應out.txt的內容是“abcdefghij”,是因為執行了out.close(),它會關閉輸出流;在關閉輸出流之前,會將緩衝區的資料寫入到輸出流中。
注意:重新測試時,要先刪除out.txt。
第2種:在readUserInput()前新增如下語句,
out.flush();
這句話的作用,是將“緩衝區的內容”寫入到輸出流中。
(01) 執行程式。在程式等待使用者輸入時,檢視“out.txt”的文字內容;發現:內容為“abcdefghij”。
(02) 執行程式。在使用者輸入之後,檢視“out.txt”的文字內容;發現:內容為“abcdefghij”。
從中,我們發現(01)和(02)結果一樣,對應out.txt的內容都是“abcdefghij”。這是因為執行了flush()操作,它的作用是將緩衝區的資料寫入到輸出流中。
注意:重新測試時,要先刪除out.txt!
第3種:在第1種的基礎上,將
out.write(ArrayLetters, 0, 10);
修改為
out.write(ArrayLetters, 0, 20);
(01) 執行程式。在程式等待使用者輸入時,檢視“out.txt”的文字內容;發現:內容為“abcdefghijklmnopqrst”(不含回車)。
(02) 執行程式。在使用者輸入之後,檢視“out.txt”的文字內容;發現:內容為“abcdefghijklmnopqrst”(含回車)。
從中,我們發現(01)執行結果是“abcdefghijklmnopqrst”(不含回車)。這是因為,緩衝區的大小是16,而我們通過out.write(ArrayLetters, 0, 20)寫入了20個位元組,超過了緩衝區的大小;這時,會直接將全部的輸入都寫入都輸出流中,而不經過緩衝區。
(02)執行結果是“abcdefghijklmnopqrst”(含回車),這是因為執行out.close()時,將回車符號'\n'寫入了輸出流中。
注意:重新測試時,要先刪除out.txt!
更多內容
03. java io系列02之 ByteArrayInputStream的簡介,原始碼分析和示例(包括InputStream)
04. java io系列03之 ByteArrayOutputStream的簡介,原始碼分析和示例(包括OutputStream)
05. java io系列04之 管道(PipedOutputStream和PipedInputStream)的簡介,原始碼分析和示例
06. java io系列05之 ObjectInputStream 和 ObjectOutputStream
07. java io系列06之 序列化總結(Serializable 和 Externalizable)
08. java io系列07之 FileInputStream和FileOutputStream
10. java io系列09之 FileDescriptor總結
11. java io系列10之 FilterInputStream
12. java io系列11之 FilterOutputStream
13. java io系列12之 BufferedInputStream(緩衝輸入流)的認知、原始碼和示例
14. java io系列13之 BufferedOutputStream(緩衝輸出流)的認知、原始碼和示例