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;
    }
}

你可能感兴趣的:(IOUtils)