聲明:本系列只供本人自學(xué)使用,勿噴结耀。
緩沖流本質(zhì)上是通過一個(gè)內(nèi)部緩沖區(qū)數(shù)組實(shí)現(xiàn)的留夜。例如,當(dāng)我們通過read()讀取輸入流的數(shù)據(jù)時(shí)图甜,BufferedInputStream會(huì)將該輸入流的數(shù)據(jù)分批的填入到緩沖區(qū)中碍粥。每當(dāng)緩沖區(qū)中的數(shù)據(jù)被讀完之后,輸入流會(huì)再次填充數(shù)據(jù)緩沖區(qū)黑毅;如此反復(fù)即纲,直到我們讀完輸入流數(shù)據(jù)位置。
那么為什么需要內(nèi)部緩沖呢博肋?答案是效率低斋。因?yàn)槠胀ǖ腎nputStream讀取數(shù)據(jù)是需要逐個(gè)字節(jié)讀取,假如原始數(shù)據(jù)是在硬盤上匪凡,那速度會(huì)很慢膊畴。而運(yùn)用BufferedInputStream,則可以一次性讀n個(gè)字節(jié)到內(nèi)存中的緩沖區(qū)病游,這樣在內(nèi)存中讀取數(shù)據(jù)會(huì)快很多唇跨。
一稠通、BufferedInputStream
- 構(gòu)造器(入?yún)⒈仨氂蠭nputStream )
public class BufferedInputStream extends FilterInputStream{
private static int DEFAULT_BUFFER_SIZE = 8192;
private static int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
// 緩沖數(shù)組
protected volatile byte buf[];
// bufUpdater提供buf的compareAndSet方法,這是必要的买猖,因?yàn)閏lose()可以是異步的改橘,可以將buf是否為null作為是否close的主要指標(biāo)。
private static final AtomicReferenceFieldUpdater<BufferedInputStream, byte[]> bufUpdater =
AtomicReferenceFieldUpdater.newUpdater(BufferedInputStream.class, byte[].class, "buf");
// buf中的有效字節(jié)數(shù)
protected int count;
// buf中的當(dāng)前字節(jié)位置
protected int pos;
public BufferedInputStream(InputStream in) {
this(in, DEFAULT_BUFFER_SIZE);
}
public BufferedInputStream(InputStream in, int size) {
super(in);
if (size <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
}
}
- 核心方法
// 往buf填充數(shù)據(jù)玉控,方法里主要是考慮到mark的幾種情況
private void fill() throws IOException
public synchronized int read() throws IOException {
// 當(dāng)讀完buf中的數(shù)據(jù)后飞主,就需要把InputStream的數(shù)據(jù)重新填充到buf
if (pos >= count) {
fill();
if (pos >= count)
return -1;
}
// 返回buf的下一個(gè)字節(jié)
return getBufIfOpen()[pos++] & 0xff;
}
private int read1(byte[] b, int off, int len) throws IOException
// 調(diào)用 read1
public synchronized int read(byte b[], int off, int len) throws IOException
- 其他方法
public synchronized long skip(long n) throws IOException
public synchronized int available() throws IOException
public synchronized void mark(int readlimit)
public synchronized void reset() throws IOException
public boolean markSupported()
二、BufferedOutputStream
- 構(gòu)造器(入?yún)⒈仨氂蠴utputStream )
public class BufferedOutputStream extends FilterOutputStream{
private static int DEFAULT_BUFFER_SIZE = 8192;
private static int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
// 緩沖數(shù)組
protected byte buf[];
// buf中的有效字節(jié)數(shù)
protected int count;
public BufferedOutputStream(OutputStream out) {
this(out, 8192);
}
public BufferedOutputStream(OutputStream out, int size) {
super(out);
if (size <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
}
}
- 核心方法
public synchronized void write(int b) throws IOException
public synchronized void write(byte b[], int off, int len) throws IOException
public synchronized void flush() throws IOException
三高诺、BufferedReader
- 構(gòu)造器(入?yún)⒈仨氂蠷eader)
public class BufferedInputStream extends FilterInputStream{
private Reader in;
private static int defaultCharBufferSize = 8192;
private static int defaultExpectedLineLength = 80;
// 緩沖數(shù)組
private char cb[];
// 其他 mark相關(guān)變量
public BufferedReader(Reader in) {
this(in, defaultCharBufferSize);
}
public BufferedReader(Reader in, int sz) {
super(in);
if (sz <= 0)
throw new IllegalArgumentException("Buffer size <= 0");
this.in = in;
cb = new char[sz];
nextChar = nChars = 0;
}
}
- 核心方法
public int read() throws IOException
public int read(char cbuf[], int off, int len) throws IOException
public String readLine() throws IOException
- 其他方法
public long skip(long n) throws IOException
public boolean ready() throws IOException
public boolean markSupported()
public void mark(int readAheadLimit) throws IOException
public void reset() throws IOException
public void close() throws IOException
public Stream<String> lines()
四碌识、BufferedWriter
- 構(gòu)造器(入?yún)⒈仨氂蠾riter)
public class BufferedWriter extends Writer{
private Writer out;
private char cb[];
private int nChars, nextChar;
private static int defaultCharBufferSize = 8192;
private String lineSeparator;
public BufferedWriter(Writer out) {
this(out, defaultCharBufferSize);
}
public BufferedWriter(Writer out, int sz) {
super(out);
if (sz <= 0)
throw new IllegalArgumentException("Buffer size <= 0");
this.out = out;
cb = new char[sz];
nChars = sz;
nextChar = 0;
lineSeparator = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
}
}
- 核心方法
public void write(int c) throws IOException
public void write(char cbuf[], int off, int len) throws IOException
public void write(String s, int off, int len) throws IOException
public void newLine() throws IOException
- 其他方法
public void flush() throws IOException
public void close() throws IOException