private final StreamEncoder se;
/**
* 创建一个OutputStreamWriter,它使用charsetName。
*
* @param out
* An OutputStream
*
* @param charsetName
* The name of a supported
* {@link java.nio.charset.Charset charset}
*
* @exception UnsupportedEncodingException
* If the named encoding is not supported
*/
public OutputStreamWriter(OutputStream out, String charsetName)
throws UnsupportedEncodingException
{
super(out);
if (charsetName == null)
throw new NullPointerException("charsetName");
se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
}
/**
* 创建一个使用默认字符编码的OutputStreamWriter。
*
* @param out An OutputStream
*/
public OutputStreamWriter(OutputStream out) {
super(out);
try {
se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
}
/**
* 创建一个使用给定字符集的OutputStreamWriter。
*
* @param out
* An OutputStream
*
* @param cs
* A charset
*
* @since 1.4
* @spec JSR-51
*/
public OutputStreamWriter(OutputStream out, Charset cs) {
super(out);
if (cs == null)
throw new NullPointerException("charset");
se = StreamEncoder.forOutputStreamWriter(out, this, cs);
}
/**
* 创建一个OutputStreamWriter,它使用给定的字符集编码器。
*
* @param out
* An OutputStream
*
* @param enc
* A charset encoder
*
* @since 1.4
* @spec JSR-51
*/
public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
super(out);
if (enc == null)
throw new NullPointerException("charset encoder");
se = StreamEncoder.forOutputStreamWriter(out, this, enc);
}
方法getEncoding,flushBuffer,3个write,flush,close
/**
* 返回当前流使用的字符编码的名称。
*
* <p> 如果编码具有历史名称,则返回该名称;
* 否则返回编码的规范名称。
*
* <p> 如果这个实例是用构造函数OutputStreamWriter(OutputStream, String)创建的,
* 那么对于编码来说唯一的返回的名称可能与传递给构造函数的名称不同。
* 如果流已经关闭,此方法可能返回null。</p>
*
* @return The historical name of this encoding, or possibly
* <code>null</code> if the stream has been closed
*
* @see java.nio.charset.Charset
*
* @revised 1.4
* @spec JSR-51
*/
public String getEncoding() {
return se.getEncoding();
}
/**
* 将输出缓冲区刷新到基础字节流,而不刷新字节流本身。
* 此方法仅是非私有的,因此可能会被PrintStream调用。
*/
void flushBuffer() throws IOException {
se.flushBuffer();
}
/**
* 只写一个字符。
*
* @exception IOException If an I/O error occurs
*/
public void write(int c) throws IOException {
se.write(c);
}
/**
* 写入一个字符数组。
*
* @param cbuf Buffer of characters
* @param off Offset from which to start writing characters
* @param len Number of characters to write
*
* @exception IOException If an I/O error occurs
*/
public void write(char cbuf[], int off, int len) throws IOException {
se.write(cbuf, off, len);
}
/**
* 写入一个字符串的一部分。
*
* @param str A String
* @param off Offset from which to start writing characters
* @param len Number of characters to write
*
* @exception IOException If an I/O error occurs
*/
public void write(String str, int off, int len) throws IOException {
se.write(str, off, len);
}
/**
* 刷新流
*
* @exception IOException If an I/O error occurs
*/
public void flush() throws IOException {
se.flush();
}
public void close() throws IOException {
se.close();
}