Here you can find the source of copyStream(InputStream in, boolean closeIn, OutputStream out, boolean closeOut)
public static int copyStream(InputStream in, boolean closeIn, OutputStream out, boolean closeOut) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2013 Red Hat, Inc.//from w w w . java2 s . c om * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Red Hat, Inc. - initial API and implementation ******************************************************************************/ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static int copyStream(InputStream in, boolean closeIn, OutputStream out, boolean closeOut) throws IOException { try { int written = 0; byte[] buffer = new byte[16 * 1024]; int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); written += len; } return written; } finally { try { if (closeIn) { in.close(); } } finally { if (closeOut) { out.close(); } } } } }