BufferedInputStream也是对其他流的一个装饰,并提供了其他的功能,主要是内部提供一个缓存区。以读取为例,在第一次读取的时候会填充缓冲区,就是一次性读取数据放入缓冲区中,下次读取的时候直接从缓存区里面取就行。如果缓存区里的数据都被读取,那么再重新填充缓存区。
BufferedOutputStream也是一样的原理,先把数据写入缓存区,缓存区满了再把数据写入输出流中。
看下write的源码:
public synchronized void write(byte b[], int off, int len) throws IOException {
if (len >= buf.length) {
/* If the request length exceeds the size of the output buffer,
flush the output buffer and then write the data directly.
In this way buffered streams will cascade harmlessly. */
flushBuffer();
out.write(b, off, len);
return;
}
if (len > buf.length - count) {
flushBuffer();
}
System.arraycopy(b, off, buf, count, len);
count += len;
}
private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);
count = 0;
}
}
第一个条件,如果写入数据的长度大于缓冲区的长度,那么先刷新缓存区也就是把缓存区中的数据写入输出流,然后直接把数据写入输出流,也就是不在经过缓存区。
第二个条件,如果写入数据的长度大于当前缓存区的剩余长度,那么刷新缓存区。
最后,把数据拷贝到缓存区中。如果第二个条件不满足,也就是缓存区能够放下写入的数据,那么就直接拷贝到缓存区中。
所以,用BufferedOutputStream写入数据后最好调用一下flush方法(这个方法调用flushBuffer),当然也可以不调用,但是不要忘记调用close方法,close方法也会调用flushBuffer。否则会导致数据丢失。