Java 日看一类(41)之IO包中的OutputStreamWriter类

该类继承自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 '&#92;uD800' to
 * '&#92;uDBFF' followed by a <i>low</i> surrogate in the range '&#92;uDC00' to
 * '&#92;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方便继承和调用,属于模块功能的交合点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值