Here you can find the source of copyStream(final InputStream source, final OutputStream target)
static long copyStream(final InputStream source, final OutputStream target) throws IOException
//package com.java2s; /* Utils.java/*from www . j a va 2 s. c om*/ * * Created: 2012-10-01 (Year-Month-Day) * Character encoding: UTF-8 * ****************************************** LICENSE ******************************************* * * Copyright (c) 2012 - 2013 XIAM Solutions B.V. (http://www.xiam.nl) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { private static final int BUFFER_SIZE = 64 * 1024; /** * Copies the supplied {@code InputStream} to the provided {@code OutputStream}. * Both streams are always closed, before this method returns. * * @return The bytes copied */ static long copyStream(final InputStream source, final OutputStream target) throws IOException { long size = 0L; try { final byte[] buffer = new byte[BUFFER_SIZE]; int length; while ((length = source.read(buffer, 0, buffer.length)) >= 0) { size += length; target.write(buffer, 0, length); } return size; } finally { close(source); close(target); } } /** * {@code null} safe closes the supplied {@code Closeable}. */ static void close(final Closeable closeable) throws IOException { if (closeable != null) { closeable.close(); } } }