该类继承自Writer
引入了如下包:
import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; import sun.nio.cs.StreamEncoder;
类头注释如下:
/** * An OutputStreamWriter is a bridge from character streams to byte streams: * Characters written to it are encoded into bytes using a specified {@link * java.nio.charset.Charset charset}. The charset that it uses * may be specified by name or may be given explicitly, or the platform's * default charset may be accepted. * * <p> Each invocation of a write() method causes the encoding converter to be * invoked on the given character(s). The resulting bytes are accumulated in a * buffer before being written to the underlying output stream. The size of * this buffer may be specified, but by default it is large enough for most * purposes. Note that the characters passed to the write() methods are not * buffered. * * <p> For top efficiency, consider wrapping an OutputStreamWriter within a * BufferedWriter so as to avoid frequent converter invocations. For example: * * <pre> * Writer out * = new BufferedWriter(new OutputStreamWriter(System.out)); * </pre> * * <p> A <i>surrogate pair</i> is a character represented by a sequence of two * <tt>char</tt> values: A <i>high</i> surrogate in the range '\uD800' to * '\uDBFF' followed by a <i>low</i> surrogate in the range '\uDC00' to * '\uDFFF'. * * <p> A <i>malformed surrogate element</i> is a high surrogate that is not * followed by a low surrogate or a low surrogate that is not preceded by a * high surrogate. * * <p> This class always replaces malformed surrogate elements and unmappable * character sequences with the charset's default <i>substitution sequence</i>. * The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more * control over the encoding process is required. * * @see BufferedWriter * @see OutputStream * @see java.nio.charset.Charset * * @author Mark Reinhold * @since JDK1.1 */
大意如下:
该类是一个转换通道,从字符转换到byte
字符通过指定方式解码为字节
使用的字符集可以用名字指示,或者明确给出(chaset对象)或者使用平台默认的字符集
每次对写方法的调用引起解码转换器对输入的字符进行解码
转化所得的byte会先放在缓冲之中,然后再写入底层输出流
缓冲的大小可以被指定,不过默认的大小足以解决大部分情况
注意,被输入的字符并没有缓冲机制
为了效率最优,考虑将OutputStreamWriter封装在BufferedWriter来避免频繁的解码器调用
代理对 是一个字符,它由两个 char 值序列表示:高 代理项的范围为 '\uD800' 到 '\uDBFF',后跟范围为 '\uDC00' 到 '\uDFFF' 的低 代理项。
错误代理元素 指的是后面不跟低代理项的高代理项,或前面没有高代理项的低代理项。
此类总是使用字符集的默认替代序列 替代错误代理元素和不可映射的字符序列。如果需要更多地控制编码过程,则应该使用 CharsetEncoder 类。
该类含有如下的成员变量:
编码流
private final StreamEncoder se;
该类含有如下的成员方法:
构造函数(提供字符集名称
public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException { super(out); if (charsetName == null) throw new NullPointerException("charsetName"); se = StreamEncoder.forOutputStreamWriter(out, this, charsetName); }
构造函数(使用默认字符集名称
public OutputStreamWriter(OutputStream out) { super(out); try { se = StreamEncoder.forOutputStreamWriter(out, this, (String)null); } catch (UnsupportedEncodingException e) { throw new Error(e); } }
构造函数(使用指定字符集
public OutputStreamWriter(OutputStream out, Charset cs) { super(out); if (cs == null) throw new NullPointerException("charset"); se = StreamEncoder.forOutputStreamWriter(out, this, cs); }
构造函数(使用指定编码器
public OutputStreamWriter(OutputStream out, CharsetEncoder enc) { super(out); if (enc == null) throw new NullPointerException("charset encoder"); se = StreamEncoder.forOutputStreamWriter(out, this, enc); }
返回该流使用的字符编码名称
public String getEncoding() { return se.getEncoding(); }
清空缓冲区,将转码好的字节写出
void flushBuffer() throws IOException { se.flushBuffer(); }
向转码器写入一个字符
public void write(int c) throws IOException { se.write(c); }
向转码器写入一个字符数组
public void write(char cbuf[], int off, int len) throws IOException { se.write(cbuf, off, len); }
向转码器写入字符串
public void write(String str, int off, int len) throws IOException { se.write(str, off, len); }
刷新流
public void flush() throws IOException { se.flush();
关闭流
public void close() throws IOException { se.close(); }
该类代码看下来主要是对字符编码流进行操作(字符编码流对传入的输出流进行操作,属于一个嵌套关系),同时继承Writer方便继承和调用,属于模块功能的交合点