JAVA_IO 之BufferedInputStream BufferedOutputStream

weixin_33716557發表於2017-08-20

JAVA IO 之BufferedInputStream BufferedOutputStream

有時間整理一下fill方法,並且把BufferedOutputStream原始碼解釋翻譯一下

1 BufferedInputStream

BufferedInputStream為另一個輸入流新增一些功能,即緩衝輸入以及支援mark和reset方法,在建立BufferedInputStream時會建立一個內部緩衝區陣列。在讀取或跳過流中的位元組時,可根據需要從包含的輸入流再次填充該內部緩衝區。一次填充多個位元組。mark操作記錄輸入流中的某個節點,reset操作使得從包含的輸入流中獲取新位元組之前,再次讀取自最後一次mark操作後讀取的所有位元組。

主要欄位:

   protected byte[] buf;//儲存資料的內部緩衝區陣列
  protected int count;//緩衝區中有效位元組的個數
  protected int marklimit;//呼叫mark方法後,在後續呼叫reset方法失敗之前允許的最大提前讀取量
  protected int markpos;//最後一次呼叫mark方法時pos欄位的值
  protected int pos;//緩衝區中的當前位置

構造方法:

  BufferedInputStream(InputStream in)
    建立一個BufferedInputStream並儲存其引數,即輸入流in,以便將來使用。
  BufferedInputStream(InputStream in,int size)
    建立具有指定緩衝區大小的BufferedInputStream並儲存其引數,即輸入流in,以便將來使用。

主要方法:

  int available(): 返回快取位元組輸入流中可讀取的位元組數
  void close(): 關閉此快取位元組輸入流並釋放與該流有關的系統資源.
  void mark(int readlimit): 在流中標記位置
  boolean markSupported(): 測試該輸入流是否支援mark和reset方法
  int read(): 從緩衝輸入流中讀取一個位元組資料
  int read(byte[] b,int off,int len): 從緩衝輸入流中將最多len個位元組的資料讀入到位元組陣列b中
  long skip(long n): 從緩衝輸入流中跳過並丟棄n個位元組的資料

BufferedInputStream的作用就是為其他輸入流提供緩衝服務功能。建立BufferedInputSteam時我們會通過它的建構函式指定某個輸入流作為引數,BufferedInputStream緩衝位元組輸入流。它作為FilterInputStream的一個子類,為傳入的底層位元組輸入流提供緩衝功能,通過底層位元組輸入流讀取位元組 到自己的buffer中(內建的快取位元組陣列),然後程式呼叫BufferedInputStream的read方法將buffer中的位元組讀取到程式中,當buffer中的位元組被讀取完之後,BufferedInputStream會從in中讀取下一批資料塊到buffer中,直到in中的資料被讀取完畢,這樣做的好處是提高讀取的效率和減少開啟儲存介質的連結次數。

2 BufferedOutputStream

在BufferedOutputStream內部也提供了一個緩衝區,當緩衝區中的資料滿了以後或者直接呼叫flush()方法就會把緩衝區中的資料寫入到輸出流。BufferedOutputStream比較簡單直接看原始碼如下:

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
 */
public
class 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 <tt>0</tt> through <tt>buf.length</tt>; elements
     * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> 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.
     */
    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.
     */
    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 */
    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 {
        if (count >= buf.length) {
            flushBuffer();
        }
        buf[count++] = (byte)b;
    }

    /**
     * Writes <code>len</code> bytes from the specified byte array
     * starting at offset <code>off</code> to this buffered output stream.
     *
     * <p> 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
     * <code>BufferedOutputStream</code>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 {
        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;
    }

    /**
     * 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 {
        flushBuffer();
        out.flush();
    }
}

參考博文

http://www.cnblogs.com/xinhuaxuan/p/6062552.html

相關文章