Apache common-io 包是常用的工具包,他提供了对IO操作的一些封装。首先看一下input包下的 AutoCloseInputStream 类

   1:   * This class is typically used to release any resources related to an open
   2:   * stream as soon as possible even if the client application (by not explicitly
   3:   * closing the stream when no longer needed) or the underlying stream (by not
   4:   * releasing resources once the last byte has been read) do not do that.
   5:   *
   6:   * @version $Id: AutoCloseInputStream.java 1304052 2012-03-22 20:55:29Z ggregory $
   7:   * @since 1.4
   8:   */
   9:  public class AutoCloseInputStream extends ProxyInputStream 

这个类的作用是自动关闭使用的流,具体的实现是每次 read 的时候,都判断一下是否返回的是 -1,是就立马关闭。

    @Override
    protected void afterRead(int n) throws IOException {
        if (n == -1) {
            close();
        }
    }

实现非常简单,但是这里提供了一个非常好的设计。首先,实现了一个抽象类ProxyInputStream,继承该类,通过集成该抽象类,只需要很简单重写 afterRead 类就可以达到每次read 都判断是否应该关闭。

在ProxyInputStream中的所有read方法中,在read之前之后分别调用  beforeRead  以及 afterRead方法,这样在之类中通过覆写 beforeRead 以及 afterRead方法,来做我们想做的事情。

    protected void beforeRead(int n) throws IOException {
    }
 
    protected void afterRead(int n) throws IOException {
    }
 
    @Override
    public int read() throws IOException {
        try {
            beforeRead(1);
            int b = in.read();
            afterRead(b != -1 ? 1 : -1);
            return b;
        } catch (IOException e) {
            handleIOException(e);
            return -1;
        }
    }


    @Override
    public int read(byte[] bts, int off, int len) throws IOException {
        try {
            beforeRead(len);
            int n = in.read(bts, off, len);
            afterRead(n);
            return n;
        } catch (IOException e) {
            handleIOException(e);
            return -1;
        }
    }

所以在AutoCloseInputStream  中,只通过重写了 afterRead方法,每次read都判断是否为 –1 ,是则关闭流。

Apache common-io AutoCloseInputStream 分析的更多相关文章

  1. .apache.commons.io 源代码学习(二)FilenameUtils类

    FilenameUtils是apache common io中一个独立的工具类,对其他没有依赖,看其源代码的import即可知道. import java.io.File;import java.io ...

  2. apache commons io包基本功能

    1. http://jackyrong.iteye.com/blog/2153812 2. http://www.javacodegeeks.com/2014/10/apache-commons-io ...

  3. IO与文件读写---使用Apache commons IO包提高读写效率

    觉得很不错,就转载了, 作者: Paul Lin 首先贴一段Apache commons IO官网上的介绍,来对这个著名的开源包有一个基本的了解:Commons IO is a library of ...

  4. apache commons io入门

    原文参考  http://www.javacodegeeks.com/2014/10/apache-commons-io-tutorial.html    Apache Commons IO 包绝对是 ...

  5. org.apache.common.io-FileUtils详解

    org.apache.common.io---FileUtils详解 getTempDirectoryPath():返回临时目录路径; public static String getTempDire ...

  6. java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream(转)

    java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream 使用Tomcat的Manag ...

  7. .apache.commons.io 源代码学习(一)

    java的初学者,准备通读各种高水平源代码,提升能力. 为了避免自己的惰性,写博客. 版本:2.5 开发平台:netbeans. 今天是第一天,网上先看个例子:http://www.importnew ...

  8. WEB文件上传之apache common upload使用(一)

    文件上传一个经常用到的功能,它有许多中实现的方案. 页面表单 + RFC1897规范 + http协议上传 页面控件(flash/html5/activeX/applet) + RFC1897规范 + ...

  9. [解决]Hadoop 2.4.1 UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0

    问题:UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0 我的系统 win7 64位 Hadoop ...

  10. SEQ!org.apache.hadoop.io.LongWritable

    [uhadoop@10-13-109-236 subdir26]$ $HADOOP_HOME/bin/hadoop fs -cat /data/flumeEvents/FlumeData.155980 ...

随机推荐

  1. Web安全工程师(进阶)课程表

    01-SQL注入漏洞原理与利用 预备知识: 了解HTTP协议,了解常见的数据库.脚本语言.中间件.具备基本的编程语言基础. 授课大纲: 第一章:SQL注入基础 1.1 Web应用架构分析1.2 SQL ...

  2. idea配置maven和gradle,阿里云镜像私服

    安装Gradle 从官方网站下载安装包,解压到目录 设置环境变量 PATH=D:\gradle\gradle-3.4.1\bin GRADLE_HOME=D:\gradle\gradle-3.4.1 ...

  3. POJ1052 Plato's Blocks

    题目来源:http://poj.org/problem?id=1052 题目大意: 把1*1*1的小立方体通过粘接相邻面组成大的立方体的形状.如下图所示: 一层一层地堆叠,立方体从三个方向的投影会分别 ...

  4. Linq 查询内建议不要使用运算语句!

    比如list有2个值,当你运行完上述代码后,你会发现,你的ls中的Num没有按你预期的那样从0开始,导致这个问题的原因是:在你查询完毕后,执行的Count()方法,会导致查询语句中Num=num++再 ...

  5. POJ 3252 区间内一个数的二进制中0的数量要不能少于1的数量(数位DP)

    题意:求区间内二进制中0的数量要不能少于1的数量 分析:很明显的是数位DP: 菜鸟me : 整体上是和数位dp模板差不多的 , 需要注意的是这里有前导零的影响 , 所以需要在dfs()里面增加zor ...

  6. POJ:2976 Dropping tests(二分+最大化平均值)

    Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...

  7. 4.centos7 docker 安装

    参考这个文档进行安装docker: http://www.runoob.com/docker/centos-docker-install.html 开机启动 systemctl enable dock ...

  8. java——链表、链表栈 LinkedListStack、链表队列 LinkedListQueue

    LikedList: package Date_pacage; public class LinkedList<E> { public static void main(String[] ...

  9. 转 Python3 错误和异常/ Python学习之错误调试和测试

    ########sample 0 https://www.cnblogs.com/Simon-xm/p/4073028.html except: #捕获所有异常 except: <异常名> ...

  10. fiddler中安装证书进行https协议的抓取

    Fiddler目前默认安装对http协议进行抓取但是对手机以及其他一些是https协议的通讯抓取需要配置. 电脑Fiddler设置: 1.首页我们要在fiddler中找到菜单栏的Tools > ...