java io InputStreamReader源码分析
目录简介字段sd,4个构造函数getEncoding方法,read方法,ready方法,close方法简介package java.io;import java.nio.charset.Charset;import java.nio.charset.CharsetDecoder;import sun.nio.cs.StreamDecoder;/*** InputStreamReader是从字节流到
·
目录
getEncoding方法,read方法,ready方法,close方法
简介
package java.io;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import sun.nio.cs.StreamDecoder;
/**
* InputStreamReader是从字节流到字符流的桥梁:
* 它读取字节并使用指定的字符集将它们解码为字符。
* 它使用的字符集可以通过名称指定,也可以显式地给出,或者可以接受平台的默认字符集。
*
* <p> 对InputStreamReader的read()方法的每次调用都可能导致从底层字节输入流中读取一个或多个字节。
* 为了能够有效地将字节转换为字符,可能要提前从底层流读取比满足当前读取操作所需的字节数更多的字节。
*
* <p> 为了更有效率,考虑用BufferedReader包装一个InputStreamReader:
*
* <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

字段sd,4个构造函数
private final StreamDecoder sd;
/**
* 创建一个使用默认字符集的InputStreamReader。
*
* @param in An InputStream
*/
public InputStreamReader(InputStream in) {
// 用这个in作为lock
super(in);
try {
// 以in作为参数,创建一个StreamDecoder,作为内置的读取流
sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object
} catch (UnsupportedEncodingException e) {
// The default encoding should always be available
throw new Error(e);
}
}
/**
* 创建一个使用charsetName的InputStreamReader。
*
* @param in
* An InputStream
*
* @param charsetName
* The name of a supported
* {@link java.nio.charset.Charset charset}
*
* @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");
// // 以in作为参数,charsetName作为字符集,创建一个StreamDecoder,作为内置的读取流
sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
}
/**
* 创建一个使用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);
}
/**
* 创建一个使用CharsetDecoder的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);
}
getEncoding方法,read方法,ready方法,close方法
/**
* 返回此流使用的字符编码的名称。
*
* <p> 如果编码具有历史名称,则返回该名称;
* 否则返回编码的规范名称。
*
* <p> 如果这个实例是用InputStreamReader(InputStream, String)构造函数创建的,
* 那么对于编码来说,返回的名称可能与传递给构造函数的名称不同。
* 如果流已经关闭,此方法将返回null。
* </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();
}
/**
* 读取单个字符
*
* @return The character read, or -1 if the end of the stream has been
* reached
*
* @exception IOException If an I/O error occurs
*/
public int read() throws IOException {
// 调用sd的read方法,读取一个字符
return sd.read();
}
/**
* 将字符读入数组的一部分。
*
* @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
*
* @exception IOException If an I/O error occurs
*/
public int read(char cbuf[], int offset, int length) throws IOException {
// 调用sd的read方法
return sd.read(cbuf, offset, length);
}
/**
* 告知该流是否已准备好读取。
* 如果输入缓冲区不是空的,或者字节可以从底层字节流中读取,那么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)