Here you can find the source of copyStream(final InputStream input, final OutputStream output, long count)
public static void copyStream(final InputStream input, final OutputStream output, long count) throws IOException
//package com.java2s; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /**/*from w ww .j ava 2 s . co m*/ * Copies count bytes from input to output. If the count is negative, bytes * are copied until the end of stream. */ public static void copyStream(final InputStream input, final OutputStream output, long count) throws IOException { final byte[] buffer = new byte[64 * 1024]; // Easier to duplicate loops than to unify control behavior if (count < 0) { int read; while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); } } else { while (count > 0) { // Safe to cast to int because it's less than the buffer size int maxToRead = count > buffer.length ? buffer.length : (int) count; final int read = input.read(buffer, 0, maxToRead); if (read == -1) { return; } count -= read; output.write(buffer, 0, read); } } } }