Java InputStream to OutputStream copyStream(final InputStream source, final OutputStream target)

Here you can find the source of copyStream(final InputStream source, final OutputStream target)

Description

Copies the supplied InputStream to the provided OutputStream .

License

Apache License

Return

The bytes copied

Declaration

static long copyStream(final InputStream source, final OutputStream target) throws IOException 

Method Source Code

//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();
        }
    }
}

Related

  1. copyStream(final InputStream inputStream, final OutputStream outputStream)
  2. copyStream(final InputStream inputStream, final OutputStream outputStream)
  3. copyStream(final InputStream is, final OutputStream os)
  4. copyStream(final InputStream is, final OutputStream os)
  5. copyStream(final InputStream is, final OutputStream out, final Long amount, final int bufferSize)
  6. copyStream(final InputStream src, OutputStream dest)
  7. copyStream(final OutputStream to, final InputStream from)
  8. copyStream(InputStream fis, OutputStream fos)
  9. copyStream(InputStream from, OutputStream to)