Here you can find the source of copyStreams(InputStream input, OutputStream output)
Parameter | Description |
---|---|
input | The InputStream |
output | The OutputStream |
public static int copyStreams(InputStream input, OutputStream output) throws IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /**/* w w w . jav a 2 s . com*/ *** Copies bytes from one stream to another *** * @param input * The InputStream *** @param output * The OutputStream *** @return The number of bytes copied **/ public static int copyStreams(InputStream input, OutputStream output) throws IOException { return copyStreams(input, output, -1); } /** *** Copies bytes from one stream to another *** * @param input * The InputStream *** @param output * The OutputStream *** @param maxLen * The maximum number of bytes to copy *** @return The number of bytes copied **/ public static int copyStreams(InputStream input, OutputStream output, int maxLen) throws IOException { /* copy nothing? */ if (maxLen == 0) { return 0; } /* copy bytes */ int length = 0; // count of bytes copied byte tmpBuff[] = new byte[10 * 1024]; // 10K blocks while (true) { /* read length */ int readLen; if (maxLen >= 0) { readLen = maxLen - length; if (readLen == 0) { break; // done reading } else if (readLen > tmpBuff.length) { readLen = tmpBuff.length; // max block size } } else { readLen = tmpBuff.length; } /* read input stream */ int cnt = input.read(tmpBuff, 0, readLen); /* copy to output stream */ if (cnt < 0) { if ((maxLen >= 0) && (length != maxLen)) { // Print.logError("Copy size mismatch: " + maxLen + " => " + // length); } break; } else if (cnt > 0) { output.write(tmpBuff, 0, cnt); length += cnt; if ((maxLen >= 0) && (length >= maxLen)) { break; // per 'maxLen', done copying } } else { // Print.logDebug("Read 0 bytes ... continuing"); } } output.flush(); /* return number of bytes copied */ return length; } }