Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static void writeInputStreamToOutputStream(InputStream in, OutputStream out, long maxLength) throws IOException { byte[] buf = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buf)) != -1) { maxLength -= bytesRead; if (maxLength <= 0) { throw new IOException("Stream exceeded max size"); } out.write(buf, 0, bytesRead); } } }