Java中FilterInputStream和FilterOutputStream的用法详解
作者:kuiwu-wang 发布时间:2023-10-02 06:47:03
标签:Java,FilterInputStream,FilterOutputStream
FilterInputStream
FilterInputStream 的作用是用来“封装其它的输入流,并为它们提供额外的功能”。它的常用的子类有BufferedInputStream和DataInputStream。
BufferedInputStream的作用就是为“输入流提供缓冲功能,以及mark()和reset()功能”。
DataInputStream 是用来装饰其它输入流,它“允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型”。应用程序可以使用DataOutputStream(数据输出流)写入由DataInputStream(数据输入流)读取的数据。
FilterInputStream 源码(基于jdk1.7.40):
package java.io;
public class FilterInputStream extends InputStream {
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();
}
}
FilterOutputStream
FilterOutputStream 的作用是用来“封装其它的输出流,并为它们提供额外的功能”。它主要包括BufferedOutputStream, DataOutputStream和PrintStream。
(01) BufferedOutputStream的作用就是为“输出流提供缓冲功能”。
(02) DataOutputStream 是用来装饰其它输出流,将DataOutputStream和DataInputStream输入流配合使用,“允许应用程序以与机器无关方式从底层输入流中读写基本 Java 数据类型”。
(03) PrintStream 是用来装饰其它输出流。它能为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。
FilterOutputStream 源码(基于jdk1.7.40):
package java.io;
public class FilterOutputStream extends OutputStream {
protected OutputStream out;
public FilterOutputStream(OutputStream out) {
this.out = out;
}
public void write(int b) throws IOException {
out.write(b);
}
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
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 {
flush();
} catch (IOException ignored) {
}
out.close();
}
}


猜你喜欢
- /// <summary> /// 将List转换成Da
- 本文实例讲述了C#读取目录下所有指定类型文件的方法。分享给大家供大家参考。具体分析如下:首先要引入命名空间:using System.IO;
- 导读Lombok:可以让你的POJO代码特别简洁,不止简单在BO/VO/DTO/DO等大量使用,还有设计模式,对象对比等MybatisPlu
- Java虚拟机(JVM)是可运行Java代码的假想计算机。只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的任何Java
- Android Service是分为两种:本地服务(Local Service): 同一个apk内被调用远程服务(Remote Servic
- 1、在设计初期阶段,应该要有意识的将不同的两层分离,比如考虑数据访问层、业务逻辑层、表示层之间建立外观模式,这样可以为子系统提供简单一致的接
- 1、MediaCodec调用流程首先,我们先看下MediaCodec::CreateByType函数里面做了什么:sp<MediaCo
- 本文实例讲述了C#通过创建Windows服务启动程序的方法。分享给大家供大家参考,具体如下:1. 新建一个Windows服务应用程序创建项目
- 一、概述RocketMQ主要提供了两种消费模式:集群消费以及广播消费。我们只需要在定义消费者的时候通过setMessageModel(Mes
- 本文介绍在使用C#开发WinForm程序时,如何使用自定义的XML配置文件。虽然也可以使用app.config,但命名方面很别扭。我们在使用
- 本文讲述了Android使用国内镜像在线更新SDK的方法。分享给大家供大家参考,具体如下:什么是Android SDK:SDK:(softw
- Android的消息机制几乎是面试必问的话题,当然也并不是因为面试,而去学习,更重要的是它在Android的开发中是必不可少的,占着举足轻重
- 在JavaBeans中有这样的一个描述:当一些信息需要使用类似于字典嵌套字典再嵌套列表这种很深的结构来储存的时候,请改用类来储存。实际上,这
- 实现网页版的在线聊天室的方法有很多,在没有来到HTML5之前,常见的有:定时轮询、长连接+长轮询、基于第三方插件(如FLASH的Socket
- 本文实例讲述了Android开发圆角Button按钮实现过程,分享给大家供大家参考,具体内容如下需求及效果图:实现思路:1、shape实现圆
- 在Java的学习中,涉及到两个系统环境变量path和classpath一. path环境变量path环境变量是系统环境变量的一种,它用于保存
- Android RollPagerView实现轮播图android图片轮播效果,RollViewPager的简单使用 <c
- 在application.properties中配置了static的默认路径我的static目录结构是这样的index.html中这样引用c
- 实现android双击后退键退出当前APP功能实现该功能基本思路是,1, 监听后退键 , 比较两次后退间隔 , 低于两秒则出发退出2, 退出
- 定义:将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。类型:行为类