JDK中java.io.InputStreamReader源码

/*
 * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */


package java.io;


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




/**
 * An InputStreamReader is a bridge from byte streams to character streams: It
 * reads bytes and decodes them into characters using a specified {@link
 * java.nio.charset.Charset <code>charset</code>}.  The charset that it uses
 * may be specified by name or may be given explicitly, or the platform's
 * default charset may be accepted.
 * 
 * InputStreamReader是将字节流转换为字符流的桥梁,它读取一些字节并使用规定的字符集将它们编译为字符。
 * 所使用的字符集可通过名字指定或明确给出,或者是平台默认的字符集。
 *
 * <p> Each invocation of one of an InputStreamReader's read() methods may
 * cause one or more bytes to be read from the underlying byte-input stream.
 * To enable the efficient conversion of bytes to characters, more bytes may
 * be read ahead from the underlying stream than are necessary to satisfy the
 * current read operation.
 * 
 * 每一次InputStreamReader的读取方法的使用都会从下面的字节输入流读取一个或多个字节。
 * 为使字节转换到字符更有效,更多字节会预先从后面的流中读取。
 *
 * <p> For top efficiency, consider wrapping an InputStreamReader within a
 * BufferedReader.  For example:
 *
 * 为达到最高效率,将InputStreamReader包装在一个BufferedReader中。
 *
 * <pre>
 * BufferedReader in
 *   = new BufferedReader(new InputStreamReader(System.in));
 * </pre>
 *
 * @see BufferedReader
 * @see InputStream
 * @see java.nio.charset.Charset
 *
 * @author      Mark Reinhold
 * @since       JDK1.1
 */


public class InputStreamReader extends Reader {


    private final StreamDecoder sd;


    /**
     * Creates an InputStreamReader that uses the default charset.
     * 使用默认字符集创建InputStreamReader
     *
     * @param  in   An InputStream
     */
    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);
        }
    }


    /**
     * Creates an InputStreamReader that uses the named charset.
     * 使用命名字符集创建InputStreamReader。
     *
     * @param  in
     *         An InputStream
     *
     * @param  charsetName
     *         The name of a supported
     *         {@link java.nio.charset.Charset </code>charset<code>}
     *
     * @exception  UnsupportedEncodingException
     *             If the named charset is not supported
     */
    public InputStreamReader(InputStream in, String charsetName)
        throws UnsupportedEncodingException
    {
        super(in);
        if (charsetName == null)
            throw new NullPointerException("charsetName");
        sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
    }


    /**
     * Creates an InputStreamReader that uses the given charset. </p>
     * 使用给定的字符集创建InputStreamReader。
     *
     * @param  in       An InputStream
     * @param  cs       A charset
     *
     * @since 1.4
     * @spec JSR-51
     */
    public InputStreamReader(InputStream in, Charset cs) {
        super(in);
        if (cs == null)
            throw new NullPointerException("charset");
        sd = StreamDecoder.forInputStreamReader(in, this, cs);
    }


    /**
     * Creates an InputStreamReader that uses the given charset decoder.  </p>
     * 使用给定的字符集解码器创建InputStreamReader。
     *
     * @param  in       An InputStream
     * @param  dec      A charset decoder
     *
     * @since 1.4
     * @spec JSR-51
     */
    public InputStreamReader(InputStream in, CharsetDecoder dec) {
        super(in);
        if (dec == null)
            throw new NullPointerException("charset decoder");
        sd = StreamDecoder.forInputStreamReader(in, this, dec);
    }


    /**
     * Returns the name of the character encoding being used by this stream.
     * 使用这个流返回字符编码的名字
     *
     * <p> If the encoding has an historical name then that name is returned;
     * otherwise the encoding's canonical name is returned.
     * 如果字符编码有名字了则返回这个名字;
     * 否则返回编码的规范名字。
     *
     * <p> If this instance was created with the {@link
     * #InputStreamReader(InputStream, String)} constructor then the returned
     * name, being unique for the encoding, may differ from the name passed to
     * the constructor. This method will return <code>null</code> if the
     * stream has been closed.
     * </p>
     * 
     * 如果示例是通过构造方法创建的,则返回编码的唯一名字,这个名字可能不同于传到构造方法的名字。
     * 如果流呗关闭了则返回空。
     * 
     * @return The historical name of this encoding, or
     *         <code>null</code> if the stream has been closed
     *
     * @see java.nio.charset.Charset
     *
     * @revised 1.4
     * @spec JSR-51
     */
    public String getEncoding() {
        return sd.getEncoding();
    }


    /**
     * Reads a single character.
     * 只读取一个字符。
     *
     * @return The character read, or -1 if the end of the stream has been
     *         reached
     *         返回一个读取的字符或者返回-1当到达流的结尾时。
     *
     * @exception  IOException  If an I/O error occurs
     */
    public int read() throws IOException {
        return sd.read();
    }


    /**
     * Reads characters into a portion of an array.
     * 读取多个字符并存入一列数组。
     *
     * @param      cbuf     Destination buffer 目标字符数组
     * @param      offset   Offset at which to start storing characters 数组中开始存放的位置
     * @param      length   Maximum number of characters to read 最大读取字符数
     *
     * @return     The number of characters read, or -1 if the end of the
     *             stream has been reached
     *             返回读取的字符数或者-1当到达流的结尾时。
     *
     * @exception  IOException  If an I/O error occurs
     */
    public int read(char cbuf[], int offset, int length) throws IOException {
        return sd.read(cbuf, offset, length);
    }


    /**
     * Tells whether this stream is ready to be read.  An InputStreamReader is
     * ready if its input buffer is not empty, or if bytes are available to be
     * read from the underlying byte stream.
     * 
     * 通知流是否已准备好被读取。
     * 如果输入缓存非空或者从下一行字节流可读取到字节则InputStreamReader准备好了。
     *
     * @exception  IOException  If an I/O error occurs
     */
    public boolean ready() throws IOException {
        return sd.ready();
    }


    public void close() throws IOException {
        sd.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值