Я считаю, что это функция, которую вы ищете:
/**
* Copies all available data from in to out without closing any stream.
*
* @return number of bytes copied
*/
private static final int BUFFER_SIZE = 8192;
public static int copyAllBytes(InputStream in, OutputStream out) throws IOException {
int byteCount = 0;
byte[] buffer = new byte[BUFFER_SIZE];
while (true) {
int read = in.read(buffer);
if (read == -1) {
break;
}
out.write(buffer, 0, read);
byteCount += read;
}
return byteCount;
}