Here you can find the source of copyStream(InputStream in, OutputStream out, long maxLen)
public static void copyStream(InputStream in, OutputStream out, long maxLen) throws IOException
//package com.java2s; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; public class Main { public static final int COPY_BUF_SIZE = 4096 * 2; public static void copyStream(InputStream in, OutputStream out, long maxLen) throws IOException { byte[] buf = new byte[COPY_BUF_SIZE]; int len;/*from w w w. j a v a2s. com*/ if (maxLen <= 0) while ((len = in.read(buf)) > 0) out.write(buf, 0, len); else while ((len = in.read(buf)) > 0) if (len <= maxLen) { out.write(buf, 0, len); maxLen -= len; } else { out.write(buf, 0, (int) maxLen); break; } } public static void copyStream(Reader in, Writer out) throws IOException { char[] buf = new char[COPY_BUF_SIZE]; int len; while ((len = in.read(buf)) != -1) out.write(buf, 0, len); } public static void copyStream(Reader in, OutputStream out, String charSet) throws IOException { char[] buf = new char[4096]; int len; if (charSet == null) while ((len = in.read(buf)) != -1) { out.write(new String(buf, 0, len).getBytes()); } else while ((len = in.read(buf)) != -1) out.write(new String(buf, 0, len).getBytes(charSet)); } }