Here you can find the source of copyStream(InputStream in, OutputStream out, int len)
static void copyStream(InputStream in, OutputStream out, int len) throws IOException
//package com.java2s; /*//from w ww .j av a 2 s. c o m * Copyright (c) Nmote Ltd. 2004-2015. All rights reserved. * See LICENSE doc in a root of project folder for additional information. */ import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { static void copyStream(InputStream in, OutputStream out, int len) throws IOException { byte[] buffer = new byte[4096]; for (int i = 0; i < len;) { int toRead = Math.min(buffer.length, len - i); int r = in.read(buffer, 0, toRead); if (r < 0) { throw new EOFException(); } i += r; out.write(buffer, 0, r); } } static void copyStream(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[4096]; for (;;) { int r = in.read(buffer); if (r < 0) { break; } out.write(buffer, 0, r); } } }