源码(八)

来源:互联网 发布:淘宝网上怎么买二手货 编辑:程序博客网 时间:2024/06/06 12:21

一.概述

org.apache.commons.fileupload.util.Streams用于处理流的实用类
  • 将输入流内容拷贝到输出流
  • 获取输入流内容以默认或指定的编码的字符串的形式

二.源码

package org.apache.commons.fileupload.util;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;// 用于处理流的实用类。public final class Streams {    // 私有构造函数,防止实例化    private Streams() {    }// 在copy(InputStream, OutputStream, boolean)中使用的默认缓冲区大小    private static final int DEFAULT_BUFFER_SIZE = 8192;    /** * 将输入流InputStream的内容拷贝到输出流OutputStream     * @param pInputStream 正在被读取的输入流,pInputStream最终关闭了     * @param pOutputStream 要写入数据的输出流pOutputStream。 可能为null,在这种情况下,输入流内容将被简单地丢弃     * @param pClose true-表示输出流需要关闭,false表示输出流最终调用flush()     * @return 已拷贝的字节数     */    public static long copy(InputStream pInputStream, OutputStream pOutputStream, boolean pClose) throws IOException {        return copy(pInputStream, pOutputStream, pClose, new byte[DEFAULT_BUFFER_SIZE]);    }    public static long copy(InputStream pIn, OutputStream pOut, boolean pClose, byte[] pBuffer) throws IOException {        OutputStream out = pOut;        InputStream in = pIn;        try {// 记录拷贝的字节数            long total = 0;            for (;;) {// 循环从输入流中读取数据// 从输入流中读取数据到缓冲区                int res = in.read(pBuffer);                if (res == -1) {// 没有数据                    break;                }                if (res > 0) {// 读到数据                    total += res;                    if (out != null) {                        out.write(pBuffer, 0, res);                    }                }            }            if (out != null) {                if (pClose) {                    out.close();                } else {                    out.flush();                }                out = null;            }            in.close();            in = null;            return total;        } finally {            if (in != null) {                try {                    in.close();                } catch (Throwable t) {                    /* Ignore me */                }            }            if (pClose  &&  out != null) {                try {                    out.close();                } catch (Throwable t) {                    /* Ignore me */                }            }        }    }    /** * 这种便利的方法允许将org.apache.commons.fileupload.FileItemStream的字节内容转为字符串。  * 平台的默认字符编码用于将字节转换为字符。     * @param pStream 被读取的输入流     * @return 以字符串的形式返回流的内容     */    public static String asString(InputStream pStream) throws IOException {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        copy(pStream, baos, true);        return baos.toString();    }    /**     * 这种便利的方法允许将org.apache.commons.fileupload.FileItemStream的字节内容转为字符串。  * 用给定的支付编码将字节转换为字符。     * @param pStream 被读取的输入流     * @param pEncoding 字符编码,如 "UTF-8".     * @return 以字符串的形式返回流的内容     */    public static String asString(InputStream pStream, String pEncoding) throws IOException {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        copy(pStream, baos, true);        return baos.toString(pEncoding);    }}


原创粉丝点击