Java 7之传统I/O - 其它相关字节输入输出流类

来源:互联网 发布:Python编程pdf 编辑:程序博客网 时间:2024/06/04 18:39

1、FilterInputStream和FilterOutputStream类


下面来看一下FilterInputStream和FilterOutputStream类,这两个类就是装饰角色,他们实现了InputStream和OutputStream类的所有接口,并且类内部还有对这两个类接口的引用;如FilterInputStream类的源代码如下:

public  class FilterInputStream extends InputStream {    //The input stream to be filtered.    protected volatile InputStream in;    protected FilterInputStream(InputStream in) {        this.in = in;    }    public int read() throws IOException {        return in.read();    }    public int read(byte b[]) throws IOException {        return read(b, 0, b.length);    }    public int read(byte b[], int off, int len) throws IOException {        return in.read(b, off, len);    }    public long skip(long n) throws IOException {        return in.skip(n);    }    public int available() throws IOException {        return in.available();    }    public void close() throws IOException {        in.close();    }    public synchronized void mark(int readlimit) {        in.mark(readlimit);    }    public synchronized void reset() throws IOException {        in.reset();    }    public boolean markSupported() {        return in.markSupported();    }}
其中持有InputStream类的引用,并且在方法中调用了具体实现类的方法,所以如果一个具体的实现类想通过这个装饰角色进行流的装饰的话,需要实现InputStream接口中对应的方法。


2、PushbackInputStream



















0 0