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. Docker学习笔记【三】安装Redis

    项目中使用到Redis,平常都是别人搭建的,今天试着在Google Cloud Platform 上搭建一个学习环境. 1.使用 docker pull redis 从docker hub中下载镜像 ...

  2. 使用Aes对称加密解密Web.Config数据库连接串

    现在很多公司开始为了保证数据库的安全性,通常会对Web.Config的数据库连接字符串进行加密.本文将介绍学习使用Aes加密解密数据库连接字符串.本文采用MySql数据库. AES概念简述 AES 是 ...

  3. swift之属性

    知识点总结: 1.存储属性 struct Town{ let region = "South" //只读属性 var population = //读写属性 } 2.惰性存储属性 ...

  4. sqlserver 判断字段是否为空字符串或者null

    isnull(f.mzm,'')<>'' 不为null且不为‘’ not(f.mzm is null) 不为null

  5. Socket.io应用之联网拖拽游戏

    服务器端代码: const express=require('express'); const http=require('http'); const sio=require('socket.io') ...

  6. jQuery AJAX 简介

    AJAX 是与服务器交换数据的技术,它在不重载全部页面的情况下,实现了对部分网页的更新. jQuery AJAX 实例 使用 jQuery AJAX 修改文本内容 尝试一下 » 什么是 AJAX? A ...

  7. 基于Java配置Spring加Hibernate和再加SpringData时的差别

    先在类路径application.properties jdbc.driverClassName = org.postgresql.Driver jdbc.url = jdbc:postgresql: ...

  8. webpack2 配置 示例

    // https://github.com/webpack-contrib/extract-text-webpack-plugin var webpack = require("webpac ...

  9. 安卓 新版本 获取wifi状态网络是否可用等

    写下这篇文章目的:当我学习的和百度看看如何获取网络状态 发现都是一些比较老的方法 API23已结过时 所以在此写下记录 ,我不明白国内为什么那么少 那么我们来看看旧方法 package com.che ...

  10. EasyUI常用组件(基础)

    ---------------------------------------------------------------------------------------------------- ...