Java InputStream to OutputStream copyStreams(InputStream source, OutputStream destination)

Here you can find the source of copyStreams(InputStream source, OutputStream destination)

Description

Copies the data from the given InputStream to the given OutputStream .

License

Apache License

Parameter

Parameter Description
source a byte data source
destination the target of the copy operation

Exception

Parameter Description
IOException if an error occurs while copying

Declaration

public static void copyStreams(InputStream source, OutputStream destination) throws IOException 

Method Source Code

//package com.java2s;
/*//from  w w w . j  a  va 2  s.c  o  m
 * Copyright (C) 2007 Roland Krueger
 * 
 * Created on 26.03.2010
 * 
 * Author: Roland Krueger (www.rolandkrueger.info)
 * 
 * This file is part of RoKlib.
 * 
 * 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.IOException;
import java.io.InputStream;

import java.io.OutputStream;

public class Main {
    /**
     * <p>
     * Copies the data from the given {@link InputStream} to the given {@link OutputStream}.
     * <p>
     * Both source and destination stream will be closed after the copy process.
     * 
     * @param source
     *          a byte data source
     * @param destination
     *          the target of the copy operation
     * @throws IOException
     *           if an error occurs while copying
     */
    public static void copyStreams(InputStream source, OutputStream destination) throws IOException {
        copyStreams(source, destination, true, true);
    }

    /**
     * <p>
     * Copies the data from the given {@link InputStream} to the given {@link OutputStream}. The two boolean parameters
     * let you define whether you want the source and destination streams to be closed after the operation.
     * 
     * @param source
     *          a byte data source
     * @param destination
     *          the target of the copy operation
     * @param closeSource
     *          source stream will be closed if <code>true</code>
     * @param closeDestination
     *          destination stream will be closed if <code>true</code>
     * @throws IOException
     *           if an error occurs while copying
     */
    public static void copyStreams(InputStream source, OutputStream destination, boolean closeSource,
            boolean closeDestination) throws IOException {
        byte[] buffer = new byte[4096];
        int read;
        while ((read = source.read(buffer)) != -1) {
            destination.write(buffer, 0, read);
        }
        destination.flush();
        if (closeDestination)
            destination.close();
        if (closeSource)
            source.close();
    }
}

Related

  1. copyStreams(InputStream in, OutputStream out)
  2. copyStreams(InputStream in, OutputStream out, int buf)
  3. copyStreams(InputStream input, OutputStream output)
  4. copyStreams(InputStream inputStream, OutputStream outputStream)
  5. copyStreams(InputStream is, OutputStream os)
  6. copyStreams(InputStream source, OutputStream target)
  7. copyStreamSafely(InputStream in, ByteArrayOutputStream os)
  8. copyStreamToFile(final InputStream stream, final File output)
  9. copyStreamToFile(InputStream input, String outputPath)