BufferedWriter 源码分析

转自:http://www.fengfly.com/plus/view-214074-1.html

BufferedWriter 是缓冲字符输出流。它继承于Writer。
BufferedWriter 的作用是为其他字符输出流添加一些缓冲功能。

public class BufferedWriter extends Writer {  
    // 输出流对象  
    private Writer out;  
    // 保存“缓冲输出流”数据的字符数组  
    private char cb[];  
    // nChars 是cb缓冲区中字符的总的个数  
    // nextChar 是下一个要读取的字符在cb缓冲区中的位置  
    private int nChars, nextChar;  
    // 默认字符缓冲区大小  
    private static int defaultCharBufferSize = 8192;  
    // 行分割符  
    private String lineSeparator;  
 
    // 构造函数,传入“Writer对象”,默认缓冲区大小是8k  
    public BufferedWriter(Writer out) {  
        this(out, defaultCharBufferSize);  
    }  
 
    // 构造函数,传入“Writer对象”,指定缓冲区大小是sz  
    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"));  
    }  
 
    // 确保“BufferedWriter”是打开状态  
    private void ensureOpen() throws IOException {  
        if (out == null)  
            throw new IOException("Stream closed");  
    }  
 
    // 对缓冲区执行flush()操作,将缓冲区的数据写入到Writer中  
    void flushBuffer() throws IOException {  
        synchronized (lock) {  
            ensureOpen();  
            if (nextChar == 0)  
                return;  
            out.write(cb, 0, nextChar);  
            nextChar = 0;  
        }  
    }  
 
    // 将c写入到缓冲区中。先将c转换为char,然后将其写入到缓冲区。  
    public void write(int c) throws IOException {  
        synchronized (lock) {  
            ensureOpen();  
            // 若缓冲区满了,则清空缓冲,将缓冲数据写入到输出流中。  
            if (nextChar >= nChars)  
                flushBuffer();  
            cb[nextChar++] = (char) c;  
        }  
    }  
 
    // 返回a,b中较小的数  
    private int min(int a, int b) {  
        if (a < b) return a;  
        return b;  
    }  
    // 将字符数组cbuf写入到缓冲中,从cbuf的off位置开始写入,写入长度是len。  
    public void write(char cbuf[], int off, int len) throws IOException {  
        synchronized (lock) {  
            ensureOpen();  
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||  
                ((off + len) > cbuf.length) || ((off + len) < 0)) {  
                throw new IndexOutOfBoundsException();  
            } else if (len == 0) {  
                return;  
            }  
            if (len >= nChars) {  
                /* If the request length exceeds the size of the output buffer,  
                   flush the buffer and then write the data directly.  In this  
                   way buffered streams will cascade harmlessly. */ 
                flushBuffer();  
                out.write(cbuf, off, len);  
                return;  
            }  
            int b = off, t = off + len;  
            while (b < t) {  
                int d = min(nChars - nextChar, t - b);  
                System.arraycopy(cbuf, b, cb, nextChar, d);  
                b += d;  
                nextChar += d;  
                if (nextChar >= nChars)  
                    flushBuffer();  
            }  
        }  
    }  
    // 将字符串s写入到缓冲中,从s的off位置开始写入,写入长度是len。  
    public void write(String s, int off, int len) throws IOException {  
        synchronized (lock) {  
            ensureOpen();  
            int b = off, t = off + len;  
            while (b < t) {  
                int d = min(nChars - nextChar, t - b);  
                s.getChars(b, b + d, cb, nextChar);  
                b += d;  
                nextChar += d;  
                if (nextChar >= nChars)  
                    flushBuffer();  
            }  
        }  
    }  
    // 将换行符写入到缓冲中  
    public void newLine() throws IOException {  
        write(lineSeparator);  
    }  
    // 清空缓冲区数据  
    public void flush() throws IOException {  
        synchronized (lock) {  
            flushBuffer();  
            out.flush();  
        }  
    }  
    public void close() throws IOException {  
        synchronized (lock) {  
            if (out == null) {  
                return;  
            }  
            try {  
                flushBuffer();  
            } finally {  
                out.close();  
                out = null;  
                cb = null;  
            }  
        }  
    }  
} 

示例见原文


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值