JDK源碼(二):BufferedOuputStream

學習更多源碼,請關注微信公衆號:jdkSpring ,或者微信掃一下二維碼:

BufferedOutputStream 是緩衝輸出流。它繼承於FilterOutputStream。

BufferedOutputStream 的作用是爲另一個輸出流提供“緩衝功能”。


import java.io.*;

public class BufferedOutputStreamDemo {

    public static void main(String[] args) throws Exception{

        /**
         * 把字符串寫入文本
         */
        String out = "qwertyuiopasdfghjklzxcvbnm";
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("D:\\jdk_s_d\\out.txt"));
        outputStream.write(out.getBytes());
        outputStream.flush();
        outputStream.close();


        /**
         * 把一個文件的內容寫入另外一個文件
         */
        BufferedInputStream bf = new BufferedInputStream(new FileInputStream(new File("D:\\jdk_s_d\\BufferedInputStream.txt")));
        BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream("D:\\jdk_s_d\\out.txt"));
        //追加
        //BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream("D:\\jdk_s_d\\out.txt", true));
        byte [] bytes = new byte[1024];
        int j;
        while ((j = bf.read(bytes)) != -1) {
            //os.write(bytes);
            String bs = new String(bytes, 0, j);
            os.write(bs.getBytes());
        }
        os.flush();
        bf.close();
        os.close();

    }
}

源碼(JDK1.8)

public class BufferedOutputStream extends FilterOutputStream {
    /**
     * 存儲數據的內部緩衝區.
     */
    protected byte buf[];

    /**
     * 緩衝區中的有效字節數. 大小範圍爲[0, buf.length].
     */
    protected int count;

    /**
     * 創建新的緩衝輸出流以將數據寫入指定的輸出流
     *
     * @param   out   the underlying output stream.
     */
    public BufferedOutputStream(OutputStream out) {
        this(out, 8192);
    }

    /**
     * 創建新的緩衝輸出流以將數據寫入指定的輸出流
     * 並指定緩衝區大小
     * @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];
    }

    /** 刷新內部緩衝區 */
    private void flushBuffer() throws IOException {
        if (count > 0) {
            out.write(buf, 0, count);
            count = 0;
        }
    }

    /**
     * 將指定的字節寫入此緩衝輸出流
     *
     * @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;
    }

    /**
     * 從指定的字節數組寫入緩衝輸出流,範圍從off開始len個字節
     */
    public synchronized void write(byte b[], int off, int len) throws IOException {
        if (len >= buf.length) {
            /* 如果請求長度超過輸出緩衝區的大小,刷新輸出緩衝區,然後直接寫入數據。*/
            flushBuffer();
            out.write(b, off, len);
            return;
        }
        if (len > buf.length - count) {//len大於緩衝區剩餘大小
            flushBuffer();
        }
        System.arraycopy(b, off, buf, count, len);
        count += len;
    }

    /**
     * 刷新此緩衝輸出流。這將強制將任何緩衝輸出字節寫入輸出流
     *
     * @exception  IOException  if an I/O error occurs.
     * @see        java.io.FilterOutputStream#out
     */
    public synchronized void flush() throws IOException {
        flushBuffer();
        out.flush();
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章