您的位置:首页 > 编程语言 > Java开发

Java中IO框架——FilterInputStream源码解析

2018-02-24 23:01 429 查看
FilterInputStream 继承自 InputStream 。

InputStream 的源码解析见:http://blog.csdn.net/yx0628/article/details/79343633

这里的源码结构使用了装饰者模式。从源码可以看出, FilterInputStream 类引用了一个输入流,而其各个方法,只是对所引用的流对象相应方法的调用,没有做其他处理,所以需要其子类来进行复写此方法,达到装饰的目的。

即 FilterInputStream 的子类要用来封装其它的输入流,并为它们提供额外的功能。其常用子类有 BufferedInputStream 和 DataInputStream 。

属性

引用了一个输入流。

protected volatile InputStream in;


构造函数

构造函数中需要接收一个输入流。而后就可以对这个输入流对象进行包装。

protected FilterInputStream(InputStream in) {
this.in = in;
}


方法

见 InputStream 中的源码解释。

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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: