IOUtils

功能:

  • copy 从source中copy数据到target
源码:

package com.dotions.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @Description IO toolkit
 * 
 * @author Dotions 2016年7月26日 下午3:22:52
 */
public class IOUtil {
    
    public static long copy(byte[] source, OutputStream target) throws IOException {
        target.write(source);
        target.flush();
        return source.length;
    }

    public static long copy(InputStream source, OutputStream target) throws IOException {
        byte[] buff = new byte[Buffer.LENGTH];
        int len = 0;
        long length = 0;
        
        while(true) {
            len = source.read(buff);
            if(len <= 0) {
                break;
            }
            
            length += len;
            target.write(buff, 0, len);
            
            if(Buffer.FLUSH) {
                target.flush();
            }
        }
        
        if(!Buffer.FLUSH) {
            target.flush();
        }
        
        return length;
    }
    
    private static interface Buffer {
        // 8KB
        public int LENGTH = 8 * 1024;
        public boolean FLUSH = true;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容