javaIO(4):Reader,InputStreamReader和FileReader源码分析

前言

前面把OutputStream,InputStream和Writer体系讲了,同时也讲了“装饰者模式”在IO体系中的应用。Reader体系跟前面的很相似。本文就将最后一个Reader体系给讲了。

正文

一,Reader源码

package java.io;


/**
 * 用于读取字符流的抽象类。
 * 子类必须实现的方法只有 read(char[], int, int) 和 close()。
 * 但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。
 */
public abstract class Reader implements Readable, Closeable {

    /**
     * 锁对象
     */
    protected Object lock;

    /**
     * 构造方法1,使用本类类型"锁对象"
     */
    protected Reader() {
        this.lock = this;
    }

    /**
     * 构造方法2,使用指定类型的"锁对象"
     */
    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;

    /** Maximum skip-buffer size */
    private static final int maxSkipBufferSize = 8192;

    /** Skip buffer, null until allocated */
    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;
    }

    /**
     * 判断此流是否支持 mark() 操作。
     */
    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;
}

二,InputStreamReader源码

package java.io;

import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import sun.nio.cs.StreamDecoder;


/**
 * InputStreamReader 是字节流通向字符流的桥梁
 * 它使用指定的 charset 读取字节并将其解码为字符。
 * 它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。
 * 每次调用 InputStreamReader 中的一个 read() 方法都会导致从底层输入流读取一个或多个字节。
 * 要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节,使其超过满足当前读取操作所需的字节。 
 * 为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader,如:
 * BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 */

public class InputStreamReader extends Reader {

    /**
     * 从字节到字符的解码过程严重依赖StreamDecoder类及其方法,通篇都在使用这个类。
     */
    private final StreamDecoder sd; 

    /**
     * 构造方法1,创建一个使用默认字符集的 InputStreamReader。 
     */
    public InputStreamReader(InputStream in) {
        super(in);
        try {
            sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object
        } catch (UnsupportedEncodingException e) {
            // The default encoding should always be available
            throw new Error(e);
        }
    }

    /**
     * 构造方法2,创建使用指定字符集的 InputStreamReader。 
     */
    public InputStreamReader(InputStream in, String charsetName)
        throws UnsupportedEncodingException
    {
        super(in);
        if (charsetName == null)
            throw new NullPointerException("charsetName");
        sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
    }

    /**
     * 构造方法3,创建使用给定字符集的 InputStreamReader。
     */
    public InputStreamReader(InputStream in, Charset cs) {
        super(in);
        if (cs == null)
            throw new NullPointerException("charset");
        sd = StreamDecoder.forInputStreamReader(in, this, cs);
    }

    /**
     * 构造方法4,创建使用给定字符集解码器的 InputStreamReader。
     */
    public InputStreamReader(InputStream in, CharsetDecoder dec) {
        super(in);
        if (dec == null)
            throw new NullPointerException("charset decoder");
        sd = StreamDecoder.forInputStreamReader(in, this, dec);
    }

    /**
     * 返回此流使用的字符编码的名称。 
     */
    public String getEncoding() {
        return sd.getEncoding();
    }

    /**
     * 读取单个字符。 
     */
    public int read() throws IOException {
        return sd.read();
    }

    /**
     * 将字符读入数组中的某一部分。 
     */
    public int read(char cbuf[], int offset, int length) throws IOException {
        return sd.read(cbuf, offset, length);
    }

    /**
     * 判断此流是否已经准备好用于读取。
     */
    public boolean ready() throws IOException {
        return sd.ready();
    }

    /**
     * 关闭该流并释放与之关联的所有资源。
     */
    public void close() throws IOException {
        sd.close();
    }
}

三,FileReader源码

FileReader是具体的Reader类,具有明确的源和read方式,同样的类还有:CharArrayReader,StringReader。

package java.io;
/**
 * 用来读取字符文件的便捷类。
 * 此类的构造方法假定默认字符编码和默认字节缓冲区大小都是适当的。
 * 要自己指定这些值,可以先在 FileInputStream 上构造一个 InputStreamReader。
 * FileReader 用于读取字符流。要读取原始字节流,请考虑使用 FileInputStream。 
 */
public class FileReader extends InputStreamReader {

   /**
    * 构造方法1,在给定从中读取数据的文件名的情况下创建一个新 FileReader。
    */
    public FileReader(String fileName) throws FileNotFoundException {
        super(new FileInputStream(fileName));
    }

   /**
    * 构造方法2,在给定从中读取数据的 File 的情况下创建一个新 FileReader。 
    */
    public FileReader(File file) throws FileNotFoundException {
        super(new FileInputStream(file));
    }

   /**
    * 构造方法3,在给定从中读取数据的 FileDescriptor 的情况下创建一个新 FileReader。 
    */
    public FileReader(FileDescriptor fd) {
        super(new FileInputStream(fd));
    }
    // 方法全部调用父类的方法
}

四,BufferedReader源码

BufferedReader是个装饰者类,相同的还有:LineNumberReader,PushbackReader。

package java.io;


import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。 
 * 可以指定缓冲区的大小,或者可使用默认的大小。
 * 通常,Reader 所作的每个读取请求都会导致对底层字符或字节流进行相应的读取请求。
 * 建议用 BufferedReader包装所有其read操作可能开销很高的Reader,如FileReader和 InputStreamReader)。
 * 例如,BufferedReader in = new BufferedReader(new FileReader("foo.in"));
 */

public class BufferedReader extends Reader {

    private Reader in; // 持有父类对象,用于包装其子类方法

    private char cb[]; // buffer
    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 */

    /** If the next character is a line feed, skip it */
    private boolean skipLF = false;

    /** The skipLF flag when the mark was set */
    private boolean markedSkipLF = false;

    private static int defaultCharBufferSize = 8192;  // 默认buffer大小
    private static int defaultExpectedLineLength = 80;

    /**
     * 创建一个使用指定大小输入缓冲区的缓冲字符输入流。
     */
    public BufferedReader(Reader in, int sz) {
        super(in);
        if (sz <= 0)
            throw new IllegalArgumentException("Buffer size <= 0");
        this.in = in;
        cb = new char[sz];
        nextChar = nChars = 0;
    }

    /**
     * 创建一个使用默认大小输入缓冲区的缓冲字符输入流。
     */
    public BufferedReader(Reader in) {
        this(in, defaultCharBufferSize);
    }

    /** 检查流是否开启 */
    private void ensureOpen() throws IOException {
        if (in == null)
            throw new IOException("Stream closed");
    }

    /**
     * fill方法用于向buffer中写入字符。
     */
    private void fill() throws IOException {
        int dst;
        if (markedChar <= UNMARKED) {
            /* No mark */
            dst = 0;
        } else {
            /* Marked */
            int delta = nextChar - markedChar;
            if (delta >= readAheadLimit) {
                /* Gone past read-ahead limit: Invalidate mark */
                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 {
                    /* Reallocate buffer to accommodate read-ahead limit */
                    char ncb[] = new char[readAheadLimit];
                    System.arraycopy(cb, markedChar, ncb, 0, delta);
                    cb = ncb;
                    markedChar = 0;
                    dst = delta;
                }
                nextChar = nChars = delta;
            }
        }

        int n;
        do {
            n = in.read(cb, dst, cb.length - dst);
        } while (n == 0);
        if (n > 0) {
            nChars = dst + n;
            nextChar = dst;
        }
    }

    /**
     * 读取单个字符。
     * 底层调用的是Reader子类型的read方法,本类read方法对其进行了"缓冲区装饰"。
     */
    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 the requested length is at least as large as the buffer, and
               if there is no mark/reset activity, and if line feeds are not
               being skipped, do not bother to copy the characters into the
               local buffer.  In this way buffered streams will cascade
               harmlessly. */
            if (len >= cb.length && markedChar <= UNMARKED && !skipLF) {
                return in.read(cbuf, off, len);
            }
            fill();
        }
        if (nextChar >= nChars) return -1;
        if (skipLF) {
            skipLF = false;
            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;
    }

    /**
     * 将字符读入数组的某一部分。
     * 它将通过重复地调用底层流的 read 方法,尝试读取尽可能多的字符。也是对老read方法进行了"装饰"。
     */
    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:
            for (;;) {

                if (nextChar >= nChars)
                    fill();
                if (nextChar >= nChars) { /* EOF */
                    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:
                for (i = nextChar; i < nChars; i++) {
                    c = cb[i];
                    if ((c == '\n') || (c == '\r')) {
                        eol = true;
                        break charLoop;
                    }
                }

                startChar = nextChar;
                nextChar = i;

                if (eol) {
                    String str;
                    if (s == null) {
                        str = new String(cb, startChar, i - startChar);
                    } else {
                        s.append(cb, startChar, i - startChar);
                        str = s.toString();
                    }
                    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);
    }

    /**
     * 跳过字符。 
     */
    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 newline needs to be skipped and the next char to be read
             * is a newline character, then just skip it right away.
             */
            if (skipLF) {
                /* Note that in.ready() will return true if and only if the next
                 * read on the stream will not block.
                 */
                if (nextChar >= nChars && in.ready()) {
                    fill();
                }
                if (nextChar < nChars) {
                    if (cb[nextChar] == '\n')
                        nextChar++;
                    skipLF = false;
                }
            }
            return (nextChar < nChars) || in.ready();
        }
    }

    /**
     * 判断此流是否支持 mark() 操作(它一定支持)。 
     */
    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;
            }
        }
    }

    /**
     * 返回一个流集合,里面包含的是读取到的行
     */
    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);
    }
}

总结

Reader体系跟其他的体系结构差不多,一通百通。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值