BufferedWriter 소스 코드 분석

다음으로 이동:http://www.fengfly.com/plus/view-214074-1.html
Buffered Writer 는 버퍼 문자 출력 흐름 입 니 다.그것 은 Writer 에 계승 되 었 다.Buffered Writer 는 다른 문자 출력 흐름 에 버퍼 기능 을 추가 하 는 역할 을 합 니 다.
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;  
            }  
        }  
    }  
} 

예 를 들 면 원문 을 볼 수 있다.

좋은 웹페이지 즐겨찾기