Java 日看一类(25)之IO包中的FilterOutputStream类

本文介绍了FilterOutputStream类,它是所有过滤输出流类的超类。此类继承自OutputStream类,并为子类提供了一个基本的数据输出方式,同时允许子类进一步覆盖方法及提供额外的功能。

该类继承自OutputStream类

无引入包




类头注释如下:

/**
 * This class is the superclass of all classes that filter output
 * streams. These streams sit on top of an already existing output
 * stream (the <i>underlying</i> output stream) which it uses as its
 * basic sink of data, but possibly transforming the data along the
 * way or providing additional functionality.
 * <p>
 * The class <code>FilterOutputStream</code> itself simply overrides
 * all methods of <code>OutputStream</code> with versions that pass
 * all requests to the underlying output stream. Subclasses of
 * <code>FilterOutputStream</code> may further override some of these
 * methods as well as provide additional methods and fields.
 *
 * @author  Jonathan Payne
 * @since   JDK1.0
 */

大意如下:

这个类是所有过滤输出流类的祖先

这些流位于所有已存在的输出流(底层输出流)之上,使用基础类型的数据

但也可能改变数据的类型或者提供一些额外的方法


FilterOutputStream类对那些将所有请求都传递给底层输出流的OutputStream方法做了简单的覆写

以该类作为基类的类可能会覆写该类的方法并且提供额外的方法和参数



该类含有如下成员变量:

该类的底层输出流

protected OutputStream out;




该类含有如下的成员方法:

构造方法(过滤传入的输出流

public FilterOutputStream(OutputStream out) {
    this.out = out;
}

写出一个字节

public void write(int b) throws IOException {
    out.write(b);
}

写出一个byte数组

public void write(byte b[]) throws IOException {
    write(b, 0, b.length);
}

写出该byte数组中特定位置和长度的数据

public void write(byte b[], int off, int len) throws IOException {
    if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
        throw new IndexOutOfBoundsException();

    for (int i = 0 ; i < len ; i++) {
        write(b[off + i]);
    }
}

刷新输出流(将输出流中的数据输出

public void flush() throws IOException {
    out.flush();
}

关闭输出流(关闭前先讲输出流中的数据写出

public void close() throws IOException {
    try (OutputStream ostream = out) {
        flush();
    }
}





该类仅做了一些简单的覆写,主体还是OutputStream,大概看看就行,主要关注子类


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值