java io學習(十七) CharArrayReader(字元數組輸入資料流)

來源:互聯網
上載者:User

CharArrayReader 介紹

CharArrayReader 是字元數組輸入資料流。它和ByteArrayInputStream類似,只不過ByteArrayInputStream是位元組數組輸入資料流,而CharArray是字元數組輸入資料流。CharArrayReader 是用於讀取字元數組,它繼承於Reader。操作的資料是以字元為單位!

CharArrayReader 函數列表

CharArrayReader(char[] buf)CharArrayReader(char[] buf, int offset, int length)     void      close()void      mark(int readLimit)boolean   markSupported()int       read()int       read(char[] buffer, int offset, int len)boolean   ready()void      reset()long      skip(long charCount)

Reader和CharArrayReader源碼分析

Reader是CharArrayReader的父類,我們先看看Reader的源碼,然後再學CharArrayReader的源碼。

1. Reader源碼分析(基於jdk1.7.40)

package java.io;     public abstract class Reader implements Readable, Closeable {         protected Object lock;         protected Reader() {        this.lock = this;    }         protected Reader(Object lock) {        if (lock == null) {            throw new NullPointerException();        }        this.lock = lock;    }         public int read(java.nio.CharBuffer target) throws IOException {        int len = target.remaining();        char[] cbuf = new char[len];        int n = read(cbuf, 0, len);        if (n > 0)            target.put(cbuf, 0, n);        return n;    }         public int read() throws IOException {        char cb[] = new char[1];        if (read(cb, 0, 1) == -1)            return -1;        else            return cb[0];    }         public int read(char cbuf[]) throws IOException {        return read(cbuf, 0, cbuf.length);    }         abstract public int read(char cbuf[], int off, int len) throws IOException;         private static final int maxSkipBufferSize = 8192;         private char skipBuffer[] = null;         public long skip(long n) throws IOException {        if (n < 0L)            throw new IllegalArgumentException("skip value is negative");        int nn = (int) Math.min(n, maxSkipBufferSize);        synchronized (lock) {            if ((skipBuffer == null) || (skipBuffer.length < nn))                skipBuffer = new char[nn];            long r = n;            while (r > 0) {                int nc = read(skipBuffer, 0, (int)Math.min(r, nn));                if (nc == -1)                    break;                r -= nc;            }            return n - r;        }    }         public boolean ready() throws IOException {        return false;    }         public boolean markSupported() {        return false;    }         public void mark(int readAheadLimit) throws IOException {        throw new IOException("mark() not supported");    }         public void reset() throws IOException {        throw new IOException("reset() not supported");    }          abstract public void close() throws IOException;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.