StringReader源碼分析

StringReader 可以將字符串打包,當作讀取來源, StringWriter 則可以作為寫入目的地,最后用 toString() 取得所有寫入的字符組成的字符串。 CharArrayReader 、 CharArrayWriter 則類似,將 char 數組當作讀取來源以及寫入目的地。這幾個類的結構和實現差不多,所以我以StringReader為例分析了這些常用的Reader,Writer子類。


/*StringReader類是一個以String數據源的字符流*/
public class StringReader extends Reader {

    private String str;
    private int length;
    private int next = 0;
    private int mark = 0;


    public StringReader(String s) {
        this.str = s;
        this.length = s.length();
    }

    /**確保Stream沒有被關閉 */
    private void ensureOpen() throws IOException {
        if (str == null)
            throw new IOException("Stream closed");
    }

    /**讀取當個字符*/
    public int read() throws IOException {
        synchronized (lock) {
            ensureOpen();
            if (next >= length)
                return -1;
            return str.charAt(next++);
        }
    }

    /**讀取多個字符到cbuf[]里,返回讀取的字符個數*/
    public int read(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 0;
            }
            if (next >= length)
                return -1;
            int n = Math.min(length - next, len);
            str.getChars(next, next + n, cbuf, off);
            next += n;
            return n;
        }
    }

    /**跳過ns個字符,這個方法比較特別,因為ns可以為負數,即它可以向前跳過ns個字符,
     如何實現的呢?原因就在 n = Math.max(-next, n);這條語句里*/
    public long skip(long ns) throws IOException {
        synchronized (lock) {
            ensureOpen();
            if (next >= length)
                return 0;
            // Bound skip by beginning and end of the source
            long n = Math.min(length - next, ns);
            n = Math.max(-next, n);
            next += n;
            return n;
        }
    }

    public boolean ready() throws IOException {
        synchronized (lock) {
            ensureOpen();
            return true;
        }
    }

    /**支持mark方法*/
    public boolean markSupported() {
        return true;
    }

    public void mark(int readAheadLimit) throws IOException {
        if (readAheadLimit < 0){
            throw new IllegalArgumentException("Read-ahead limit < 0");
        }
        synchronized (lock) {
            ensureOpen();
            mark = next;
        }
    }

    public void reset() throws IOException {
        synchronized (lock) {
            ensureOpen();
            next = mark;
        }
    }

    /**實現了Closeable接口,而其父接口有事AutoCloseable接口,故它可以自動關閉數據流,不過這是JDK7以后才有的*/
    public void close() {
        str = null;
    }
}
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容