FilterInputStream的作用是用来封装其他的输入流,并为它们提供了额外的功能,它的常用的子类有BufferedInputStream和DataInputStream。FilterOutputStream的作用是用来封装其他的输出流,并为它们提供额外的功能,主要包括BufferedOutputStream,DataOutputStream和PrintStream。

基于JDK8的FilterInputStream的源码:
public class FilterInputStream extends InputStream {
    /**
     * The input stream to be filtered.
     */
    //InputStream
    protected volatile InputStream in;

    /**
     * Creates a <code>FilterInputStream</code>
     * by assigning the  argument <code>in</code>
     * to the field <code>this.in</code> so as
     * to remember it for later use.
     *
     * @param   in   the underlying input stream, or <code>null</code> if
     *          this instance is to be created without an underlying stream.
     */

    protected FilterInputStream(InputStream in) {
        this.in = in;
    }
    //从输入流读取下一个字节
    public int read() throws IOException {
        return in.read();
    }
    //从输入流中读取len长度的字节
    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();
    }
}

基于JDK8的FilterOutputStream源码:

public class FilterOutputStream extends OutputStream {
    /**
     * The underlying output stream to be filtered.
     */
    protected OutputStream out;

    public FilterOutputStream(OutputStream out) {
        this.out = out;
    }
    //写到输入流中
    public void write(int b) throws IOException {
        out.write(b);
    }
    //写b到输入流中
    public void write(byte b[]) throws IOException {
        write(b, 0, b.length);
    }
    //写b中的起始位置为off,长度为len
    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]);
        }
    }

    /**
     * Flushes this output stream and forces any buffered output bytes
     * to be written out to the stream.
     * <p>
     * The <code>flush</code> method of <code>FilterOutputStream</code>
     * calls the <code>flush</code> method of its underlying output stream.
     *
     * @exception  IOException  if an I/O error occurs.
     * @see        java.io.FilterOutputStream#out
     */
    //刷新,强制所有的缓冲数据都被写出
    public void flush() throws IOException {
        out.flush();
    }
    //关闭资源
    @SuppressWarnings("try")
    public void close() throws IOException {
        try (OutputStream ostream = out) {
            flush();
        }
    }
}

Java-IO之FilterInputStream和FilterOuptStream的更多相关文章

  1. java.io包中的字节流—— FilterInputStream和FilterOutputStream

    接着上篇文章,本篇继续说java.io包中的字节流.按照前篇文章所说,java.io包中的字节流中的类关系有用到GoF<设计模式>中的装饰者模式,而这正体现在FilterInputStre ...

  2. java io系列10之 FilterInputStream

    FilterInputStream 介绍 FilterInputStream 的作用是用来“封装其它的输入流,并为它们提供额外的功能”.它的常用的子类有BufferedInputStream和Data ...

  3. Java IO(九)FilterInputStream 和 FilterOutputStream

    Java IO(九)FilterInputStream 和 FilterOutputStream 一.介绍 FilterInputStream 和 FilterOutputStream 是过滤字节输入 ...

  4. java.IO输入输出流:过滤流:buffer流和data流

    java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...

  5. Java IO之字符流和文件

    前面的博文介绍了字节流,那字符流又是什么流?从字面意思上看,字节流是面向字节的流,字符流是针对unicode编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...

  6. java IO流详解

    流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  7. Java IO流学习总结

    Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  8. [Java IO]02_字节流

    概要 字节流有两个核心抽象类:InputStream 和 OutputStream.所有的字节流类都继承自这两个抽象类. InputStream 负责输入,OutputStream 负责输出. 字节流 ...

  9. Java IO之字节流

    Java中的输入是指从数据源等读到Java程序中,这里的数据源可以是文件,内存或网络连接,输出则是指从Java程序中写到目的地. 输入输出流可以分为以下几种类型(暂时不考虑File类) 类名 中文名 ...

  10. java io 流分类表

    Java输入/输出流体系中常用的流分类(表内容来自java疯狂讲义) 注:下表中带下划线的是抽象类,不能创建对象.粗体部分是节点流,其他就是常用的处理流. 流分类 使用分类 字节输入流 字节输出流 字 ...

随机推荐

  1. Ubuntu一些常用的软件安装及配置

    软件 安装 Vim echo "y" | sudo apt-get install vim 安装搜狗输入法 这个我在虚拟机里面尝试了好多遍,不断恢复备份然后重试.终于有了这个纯靠命 ...

  2. Spring中@Autowired与@Resource的区别

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...

  3. SQL AND和OR求值顺序

    假如需要列出价格为10美元及以上,且由DLL01或BRS01制造的所有产品.下面的SELECT语句使用组合的AND和OR操作符建立了一个WHERE子句: ; 分析▼ 请看上面的结果.返回的行中有4行价 ...

  4. jquery 实现图片无缝向左滚动

    <script type="text/javascript" src="_pub/Script/jquery.js"></script> ...

  5. 项目管理软件系列-Linux一键安装禅道

    linux用一键安装包 简介:本文介绍如何在linux下面使用禅道一键安装包搭建禅道的运行环境. linux一键安装包内置了apache, php, mysql这些应用程序,只需要下载解压缩即可运行禅 ...

  6. C#利用Attribute实现简易AOP介绍

    首先看一段简单的代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } //来自UI层的调用 ...

  7. 编程英语之KNN算法

    School of Computer Science The University of Adelaide   Artificial Intelligence Assignment 2   Semes ...

  8. 再谈RunLoop

    RunLoop 一 概述: 一句话解释RunLoop:运行任务的循环. 为什么要有RunLoop:解决交互式UI设计中的一个问题,如何快速响应用户输入,如何快速将程序运行结果输出到屏幕? 计算机是个笨 ...

  9. Spring boot 整合 Mybatis + Thymeleaf开发web(二)

    上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染, ...

  10. Bootstrap3 代码-内联代码

    通过 <code> 标签包裹内联样式的代码片段. For example, <section> should be wrapped as inline. For example ...