廖雪峰Java6 IO编程-2input和output-4Filter模式
Posted singleSpace
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了廖雪峰Java6 IO编程-2input和output-4Filter模式相关的知识,希望对你有一定的参考价值。
1.JDK提供的InputStream分为两类:
- 直接提供数据的InputStream
* FileInputStream:从文件读取
* ServletInputStream:从HTTP请求读取数据
* Socket.getInputStream():从TCP连接读取数据 - 提供额外附加功能的FilterInputStream
* 如果要给FileInputStream添加缓冲功能:- BufferedFileInputStream extends FileInputStream
* 如果要给FileInputStream添加计算机签名的功能:
* DigestFileInputStream extends FileInputStream
* 如果要给FileInputStream添加加密/解密功能:
* CipherFileInputStream extends FileInputStream
- 组合功能而非继承的设计模式称为Filter模式(或者Decorator模式)
- 通过少量的类实现了各种功能的组合
//演示代码
InputStream input = new GZIPInputStream(//直接读取解压缩包的内容
new BufferedInputStream(//提供缓冲的功能
new FileInputStream("test.gz")));
![](https://image.cha138.com/20210524/8b218b2687c7498e8217bce2eea1c7ae.jpg)
```#java
public class Main {
static void printCount1(List<Integer> list) throws IOException{
try(InputStream input = new GZIPInputStream(
new BufferedInputStream(
new FileInputStream("./src/main/java/com/testList/test.gz")))){
byte[] buffer = new byte[1024];//创建竹筒
int count=0;
int n ;
while((n=input.read(buffer))!=-1){
count += n;
list.add(n);
}
System.out.println("直接读取的和:"+count);
System.out.println("直接读取得到的集合的和"+getSum(list));
}
}
static void printCount2(List<Integer> list) throws IOException{
try(CountInputStream input = new CountInputStream(
new GZIPInputStream(
new BufferedInputStream(
new FileInputStream("./src/main/java/com/testList/test.gz"))))){
byte[] buffer = new byte[1024];
int n;
while((n=input.read(buffer))!=-1){
list.add(n);
// System.out.println();
}
System.out.println("通过CountInputStream获取的和:"+input.count);
System.out.println("通过CountInputStream获取的集合的和:"+getSum(list));
}
}
static Integer getSum(List<Integer> list){
int sum = 0;
for(int i=0;i<list.size();i++){
sum += list.get(i);
}
return sum;
}
public static void main(String[] args) throws IOException {
List<Integer> list1 = new LinkedList<>();
List<Integer> list2 = new LinkedList<>();
printCount2(list2);
printCount1(list1);
//从结果
System.out.println(list1);
}
}
![](https://image.cha138.com/20210524/fc0c369b09a84dd6b8197854902b5258.jpg)
2.总结
- Java IO使用Filter模式为InputStream/OutputStream增加功能
- 可以把一个InputStream和任意FilterInputStream组合
- 可以把一个OutputStream和任意FilterOutputStream组合
- Filter模式可以在运行期动态增加功能(又成Decorator模式)
以上是关于廖雪峰Java6 IO编程-2input和output-4Filter模式的主要内容,如果未能解决你的问题,请参考以下文章