charset
}. The charset that it uses
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的读取方法的使用都会从下面的字节输入流读取一个或多个字节。
* 为使字节转换到字符更有效,更多字节会预先从后面的流中读取。
*
*
For top efficiency, consider wrapping an InputStreamReader within a
* BufferedReader. For example:
*
* 为达到最高效率,将InputStreamReader包装在一个BufferedReader中。
*
*
* BufferedReader in
* = new BufferedReader(new InputStreamReader(System.in));
*
}
*
* @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.
* 使用给定的字符集创建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.
* 使用给定的字符集解码器创建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.
* 使用这个流返回字符编码的名字
*
* If the encoding has an historical name then that name is returned;
* otherwise the encoding's canonical name is returned.
* 如果字符编码有名字了则返回这个名字;
* 否则返回编码的规范名字。
*
*
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 null
if the
* stream has been closed.
*
*
* 如果示例是通过构造方法创建的,则返回编码的唯一名字,这个名字可能不同于传到构造方法的名字。
* 如果流呗关闭了则返回空。
*
* @return The historical name of this encoding, or
* null
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();
}
}