package java.io;/** * The class implements a buffered output stream. By setting up such * an output stream, an application can write bytes to the underlying * output stream without necessarily causing a call to the underlying * system for each byte written. * * @author Arthur van Hoff * @since JDK1.0 *///设计模式为装饰模式//自己的理解://这个类 做的事情 就是你调用write方法写入的数据进行累加 累加到一定量 在调用out.write()去写出//如果调用write len大于buf.length 就没有作用了 起不到累加的作用//而调用write()之前的分段读取数据,是为了减少对一次性对内存的使用量publicclass BufferedOutputStream extends FilterOutputStream { /** * The internal buffer where data is stored. */ //内部缓冲区 protected byte buf[]; /** * The number of valid bytes in the buffer. This value is always * in the range 0 through buf.length; elements * buf[0] through buf[count-1] contain valid * byte data. */ //缓冲区数据的长度 protected int count; /** * Creates a new buffered output stream to write data to the * specified underlying output stream. * * @param out the underlying output stream. */ //默认声明的缓冲区大小是8192 (8k) public BufferedOutputStream(OutputStream out) { this(out, 8192); } /** * Creates a new buffered output stream to write data to the * specified underlying output stream with the specified buffer * size. * * @param out the underlying output stream. * @param size the buffer size. * @exception IllegalArgumentException if size <= 0. */ //父类装饰了out 定义buf的大小 public BufferedOutputStream(OutputStream out, int size) { super(out); if (size <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } buf = new byte[size]; } /** Flush the internal buffer */ //将buf中的数据写出 并将count设为0 private void flushBuffer() throws IOException { if (count > 0) { out.write(buf, 0, count); count = 0; } } /** * Writes the specified byte to this buffered output stream. * * @param b the byte to be written. * @exception IOException if an I/O error occurs. */ public synchronized void write(int b) throws IOException { //count只有可能等于buf.length 不会大于吧 找不到大于可能 if (count >= buf.length) { flushBuffer(); } buf[count++] = (byte)b; } /** * Writeslen
bytes from the specified byte array * starting at offsetoff
to this buffered output stream. * *Ordinarily this method stores bytes from the given array into this * stream's buffer, flushing the buffer to the underlying output stream as * needed. If the requested length is at least as large as this stream's * buffer, however, then this method will flush the buffer and write the * bytes directly to the underlying output stream. Thus redundant *
BufferedOutputStream
s will not copy data unnecessarily. * * @param b the data. * @param off the start offset in the data. * @param len the number of bytes to write. * @exception IOException if an I/O error occurs. */ public synchronized void write(byte b[], int off, int len) throws IOException { //如果len大于buf.length 直接输出到out 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; } //如果len大于buf剩余大小 就是buf剩余大小不够了 if (len > buf.length - count) { //直接输出所有buf里的内容 flushBuffer(); } //累加到buf中 System.arraycopy(b, off, buf, count, len); //count累加 count += len; } /** * Flushes this buffered output stream. This forces any buffered * output bytes to be written out to the underlying output stream. * * @exception IOException if an I/O error occurs. * @see java.io.FilterOutputStream#out */ public synchronized void flush() throws IOException { //冲刷出buf的数据到out flushBuffer(); out.flush(); }}
父类FilterOutputStream:简单装饰了OutputStream
package java.io;/** * This class is the superclass of all classes that filter output * streams. These streams sit on top of an already existing output * stream (the underlying output stream) which it uses as its * basic sink of data, but possibly transforming the data along the * way or providing additional functionality. ** The class
FilterOutputStream
itself simply overrides * all methods ofOutputStream
with versions that pass * all requests to the underlying output stream. Subclasses of *FilterOutputStream
may further override some of these * methods as well as provide additional methods and fields. * * @author Jonathan Payne * @since JDK1.0 */publicclass FilterOutputStream extends OutputStream { protected OutputStream out; public FilterOutputStream(OutputStream out) { this.out = out; } public void write(int b) throws IOException { out.write(b); } public void write(byte b[]) throws IOException { write(b, 0, b.length); } public void write(byte b[], int off, int len) throws IOException { //与操作 int类型 右边开始数第32位为1表示是负数 //如果a|b|c如果abc中有负数则结果就是负数 因为正数|负数 右数32位会变成1 //所以这里表示如果off,len.....中有负数 if ((off | len | (b.length - (len + off)) | (off + len)) < 0) throw new IndexOutOfBoundsException(); //循环len次 读len个byte for (int i = 0 ; i < len ; i++) { write(b[off + i]);//从(off+0)开始读 } } public void flush() throws IOException { out.flush(); } @SuppressWarnings("try") public void close() throws IOException { //java7新特性 //out会在 try 执行完毕后自动被关闭, //前提是,这些可关闭的资源必须实现 java.lang.AutoCloseable 接口。 try (OutputStream ostream = out) { flush(); } }}