BufferedReader的使用及源碼分析

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

     BufferedReader是爲了提供讀的效率而設計的一個包裝類,它可以包裝字符流。可以從字符輸入流中讀取文本,緩衝各個字符,從而實現字符、數組和行的高效讀取.

     BufferReader的作用是爲其它Reader提供緩衝功能。創建BufferReader時,我們會通過它的構造函數指定某個Reader爲參數。BufferReader會將該Reader中的數據分批讀取,每次讀取一部分到緩衝中;操作完緩衝中的這部分數據之後,再從Reader中讀取下一部分的數據。

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Stream;

public class BufferedReaderDemo {

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

        /**
         * BufferedReader讀取行操作
         */
        BufferedReader bufferedReader =
                new BufferedReader(
                        new InputStreamReader(
                                new FileInputStream("D:\\jdk_s_d\\out.txt")));
        String str;
        while ((str = bufferedReader.readLine()) != null){
            System.out.println(str);
        }
        bufferedReader.close();

        /**
         * 根據文件編碼讀取文件,解決亂碼問題
         */
        //獲取文件的字符編碼
        String encoding = resolveCode("D:\\jdk_s_d\\out.txt");
        BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream("D:\\jdk_s_d\\out.txt"), encoding));
        //jdk1.8新增方法lines
        Stream<String> lines = br.lines();
        lines.forEach(l -> System.out.println(l));
        br.close();
    }

    /**
     * 獲取文件的字符編碼,此方法只能用於帶BOM頭的文件,如果沒有BOM頭,則不需要調用此方法,
     * 根據實際的文件編碼即可。
     * @param path
     * @return
     * @throws Exception
     */
    public static String resolveCode(String path) throws Exception {
//      String filePath = "D:/article.txt"; //[-76, -85, -71]  ANSI
//      String filePath = "D:/article111.txt";  //[-2, -1, 79] unicode big endian
//      String filePath = "D:/article222.txt";  //[-1, -2, 32]  unicode
//      String filePath = "D:/article333.txt";  //[-17, -69, -65] UTF-8
        InputStream inputStream = new FileInputStream(path);
        byte[] head = new byte[3];
        inputStream.read(head);
        String code = "gb2312";  //或GBK
        if (head[0] == -1 && head[1] == -2 )
            code = "UTF-16";
        else if (head[0] == -2 && head[1] == -1 )
            code = "Unicode";
        else if(head[0]==-17 && head[1]==-69 && head[2] ==-65)
            code = "UTF-8";

        inputStream.close();
        return code;
    }
}

使用readLine()一定要注意:

  1. 1.讀入的數據要注意有/r或/n或/r/n

  2. 2.沒有數據時會阻塞,在文件讀取結束和數據流異常或斷開時纔會返回null

  3. 3.使用socket之類的數據流時,要謹慎使用readLine(),以免爲了等待一個換行/回車符而一直阻塞

源碼(jdk1.8)

public class BufferedReader extends Reader {

    private Reader in;//字符輸入流

    private char cb[];//字符緩衝區
  //nChars:讀取字符存儲的結束下標,nextChar:讀取字符存儲的開始下標
    private int nChars, nextChar; 

    private static final int INVALIDATED = -2;
    private static final int UNMARKED = -1;
    private int markedChar = UNMARKED;
    private int readAheadLimit = 0; /* Valid only when markedChar > 0 */

    // 如果下個字符是換行符,則跳過--專用於readLine()方法裏面控制
    private boolean skipLF = false;

    // 設置標誌時的markedSkipLF--用於mark()方法的變量
    private boolean markedSkipLF = false;

    //默認的緩衝區大小
    private static int defaultCharBufferSize = 8192;
  //用於readLine()方法時初始化StringBuffer的初始容量
    private static int defaultExpectedLineLength = 80;

    /**
     * 創建一個指定緩衝區大小的緩衝字符輸入流
     *
     * @param  in   A Reader
     * @param  sz   Input-buffer size
     *
     * @exception  IllegalArgumentException  If {@code sz <= 0}
     */
    public BufferedReader(Reader in, int sz) {
        super(in);
        if (sz <= 0)
            throw new IllegalArgumentException("Buffer size <= 0");
        this.in = in;
        cb = new char[sz];
    //初始從0開始讀
        nextChar = nChars = 0;
    }

    /**
     * 創建一個默認緩衝區大小的緩衝字符輸入流
     *
     * @param  in   A Reader
     */
    public BufferedReader(Reader in) {
        this(in, defaultCharBufferSize);
    }

    /** 檢查輸入流是否關閉 */
    private void ensureOpen() throws IOException {
        if (in == null)
            throw new IOException("Stream closed");
    }

    /**
     * 填充輸入緩衝區,並考慮標記是否有效.
     */
    private void fill() throws IOException {
        int dst;
        if (markedChar <= UNMARKED) {//沒有標記
            dst = 0;
        } else {
            /* Marked */
            int delta = nextChar - markedChar;
            if (delta >= readAheadLimit) {
                /* 超過預讀限制:無效標記 */
                markedChar = INVALIDATED;
                readAheadLimit = 0;
                dst = 0;
            } else {
                if (readAheadLimit <= cb.length) {
                    /* Shuffle in the current buffer */
                    System.arraycopy(cb, markedChar, cb, 0, delta);
                    markedChar = 0;
                    dst = delta;
                } else {
                    /* 將內部緩衝字符數組cb[]擴容至readAheadLimit大小*/
                    char ncb[] = new char[readAheadLimit];
                    System.arraycopy(cb, markedChar, ncb, 0, delta);
                    cb = ncb;
                    markedChar = 0;
                    dst = delta;
                }
                nextChar = nChars = delta;
            }
        }

        int n;
        do {
        //從輸入源對象in讀取cb剩餘空閒個數的字符,從dst索引處開始寫入
            n = in.read(cb, dst, cb.length - dst);
        } while (n == 0);
        if (n > 0) {
        //讀滿cb[]
            nChars = dst + n;
            nextChar = dst;
        }
    }

    /**
     * Reads a single character.
     *
     * @return The character read, as an integer in the range
     *         0 to 65535 (<tt>0x00-0xffff</tt>), or -1 if the
     *         end of the stream has been reached
     * @exception  IOException  If an I/O error occurs
     */
    public int read() throws IOException {
        synchronized (lock) {
            ensureOpen();
            for (;;) {
                if (nextChar >= nChars) {
                    fill();
                    if (nextChar >= nChars)
                        return -1;
                }
                if (skipLF) {
                    skipLF = false;
                    if (cb[nextChar] == '\n') {
                        nextChar++;
                        continue;
                    }
                }
                return cb[nextChar++];
            }
        }
    }

    /**
     * 將字符讀入數組的一部分,必要時從底層流讀取
     */
    private int read1(char[] cbuf, int off, int len) throws IOException {
        if (nextChar >= nChars) {
            /* 如果請求的長度至少與緩衝區一樣大,
             * 並且如果沒有標記/重置操作,
             * 並且如果換行符沒有被跳過時. 就不需要讀到緩衝區
             */
            if (len >= cb.length && markedChar <= UNMARKED && !skipLF) {
                return in.read(cbuf, off, len);
            }
            fill();
        }
    //如果上面的if處理過後,nextChar讀取的是cb.length+1,則返回-1
        if (nextChar >= nChars) return -1;
        if (skipLF) {
            skipLF = false;
      //如果是換行,nextChar+1
            if (cb[nextChar] == '\n') {
                nextChar++;
                if (nextChar >= nChars)
                    fill();
                if (nextChar >= nChars)
                    return -1;
            }
        }
        int n = Math.min(len, nChars - nextChar);
        System.arraycopy(cb, nextChar, cbuf, off, n);
        nextChar += n;
        return n;
    }

    /**
     * Reads characters into a portion of an array.
     */
    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;
            }

            int n = read1(cbuf, off, len);
            if (n <= 0) return n;
            while ((n < len) && in.ready()) {
                int n1 = read1(cbuf, off + n, len - n);
                if (n1 <= 0) break;
                n += n1;
            }
            return n;
        }
    }

    /**
     * 閱讀一行文字。換行符('\n')、回車符('\r')或緊跟換行符的回車符.
     */
    String readLine(boolean ignoreLF) throws IOException {
        StringBuffer s = null;
        int startChar;
        synchronized (lock) {
            ensureOpen();
            boolean omitLF = ignoreLF || skipLF;
        //外循環bufferLoop
        bufferLoop:
            for (;;) {
                if (nextChar >= nChars)//第一次讀入內容
                    fill();
                if (nextChar >= nChars) { /* EOF */
                    //輸入流in對象沒有可讀字符了
                    if (s != null && s.length() > 0)
                        return s.toString();
                    else
                        return null;
                }
                boolean eol = false;
                char c = 0;
                int i;

                /* Skip a leftover '\n', if necessary */
                //跳過換行符
                if (omitLF && (cb[nextChar] == '\n'))
                    nextChar++;
                skipLF = false;
                omitLF = false;
            //內循環charLoop【識別cb[]內部是否有換行符】
            charLoop:
                for (i = nextChar; i < nChars; i++) {
                    c = cb[i];
                    if ((c == '\n') || (c == '\r')) {
                        eol = true;
                        //跳出內循環,執行內循環下面的代碼
                        break charLoop;
                    }
                }
                startChar = nextChar;
                // nextChar此時指向\n或\r或緩衝區末尾
                nextChar = i;
                if (eol) {
                    String str;
                    if (s == null) {
              //範圍爲上一個nextChar的位置到\n或\r或緩衝區末尾的位置
                        str = new String(cb, startChar, i - startChar);
                    } else {
                        s.append(cb, startChar, i - startChar);
                        str = s.toString();
                    }
                    //跳過\n
                    nextChar++;
                    //跳過換行
                    if (c == '\r') {
                        skipLF = true;
                    }
                    return str;
                }
                if (s == null)
                    s = new StringBuffer(defaultExpectedLineLength);
                s.append(cb, startChar, i - startChar);
            }
        }
    }


    public String readLine() throws IOException {
        return readLine(false);
    }

    /**
     * 跳過n個字符
     */
    public long skip(long n) throws IOException {
        if (n < 0L) {
            throw new IllegalArgumentException("skip value is negative");
        }
        synchronized (lock) {
            ensureOpen();
            long r = n;
            while (r > 0) {
                if (nextChar >= nChars)
                    fill();
                if (nextChar >= nChars) /* EOF */
                    break;
                if (skipLF) {
                    skipLF = false;
                    if (cb[nextChar] == '\n') {
                        nextChar++;
                    }
                }
                long d = nChars - nextChar;
                if (r <= d) {
                    nextChar += r;
                    r = 0;
                    break;
                }
                else {
                    r -= d;
                    nextChar = nChars;
                }
            }
            return n - r;
        }
    }

    /**
     * 此流是否準備好讀取。如果緩衝區不是空的,
     * 或者如果基礎字符流已準備就緒,則緩衝字符流已準備就緒
     *

    */
    public boolean ready() throws IOException {
        synchronized (lock) {
            ensureOpen();

            /*
             * 如果需要跳過換行符,
             * 而下一個要讀取的字符是換行符,那麼就直接跳過它
             */
            if (skipLF) {
                /* 注意,in.ready()將返回true,
                * 前提是且僅當流上的下一次讀取不會阻塞
                 */
                if (nextChar >= nChars && in.ready()) {
                    fill();
                }
                if (nextChar < nChars) {
                    if (cb[nextChar] == '\n')
                        nextChar++;
                    skipLF = false;
                }
            }
            return (nextChar < nChars) || in.ready();
        }
    }

    /**
     * Tells whether this stream supports the mark() operation, which it does.
     */
    public boolean markSupported() {
        return true;
    }

    /**
     * 標記流中的當前位置。對reset()的後續調用將嘗試將流重新定位到此點。
     */
    public void mark(int readAheadLimit) throws IOException {
        if (readAheadLimit < 0) {
            throw new IllegalArgumentException("Read-ahead limit < 0");
        }
        synchronized (lock) {
            ensureOpen();
            this.readAheadLimit = readAheadLimit;
            markedChar = nextChar;
            markedSkipLF = skipLF;
        }
    }

    /**
     * 將流重置爲最新標記
     */
  public void reset() throws IOException {
        synchronized (lock) {
            ensureOpen();
            if (markedChar < 0)
                throw new IOException((markedChar == INVALIDATED)
                                      ? "Mark invalid"
                                      : "Stream not marked");
            nextChar = markedChar;
            skipLF = markedSkipLF;
        }
    }

    public void close() throws IOException {
        synchronized (lock) {
            if (in == null)
                return;
            try {
                in.close();
            } finally {
                in = null;
                cb = null;
            }
        }
    }

    /**
     * 返回一個Stream  裏面的元素爲讀到的所有行內容
     */
    public Stream<String> lines() {
        Iterator<String> iter = new Iterator<String>() {
            String nextLine = null;

            @Override
            public boolean hasNext() {
                if (nextLine != null) {
                    return true;
                } else {
                    try {
                        nextLine = readLine();
                        return (nextLine != null);
                    } catch (IOException e) {
                        throw new UncheckedIOException(e);
                    }
                }
            }

            @Override
            public String next() {
                if (nextLine != null || hasNext()) {
                    String line = nextLine;
                    nextLine = null;
                    return line;
                } else {
                    throw new NoSuchElementException();
                }
            }
        };
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
                iter, Spliterator.ORDERED | Spliterator.NONNULL), false);
    }
}

 

 

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