Java InputStream to OutputStream copyStreamToStream(final InputStream input, final OutputStream output)

Here you can find the source of copyStreamToStream(final InputStream input, final OutputStream output)

Description

Copy any input stream to output stream.

License

Open Source License

Parameter

Parameter Description
input - InputStream to copy from
output - OutputStream to copy to

Exception

Parameter Description
IOException - io error in function
OSSMultiException - double error in function

Declaration

public static void copyStreamToStream(final InputStream input, final OutputStream output) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w  ww  .j  a v a 2  s. com
 *    Debrief - the Open Source Maritime Analysis Application
 *    http://debrief.info
 *
 *    (C) 2000-2014, PlanetMayo Ltd
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the Eclipse Public License v1.0
 *    (http://www.eclipse.org/legal/epl-v10.html)
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * Copy any input stream to output stream. Once the data will be copied
     * both streams will be closed.
     * 
     * @param input  - InputStream to copy from
     * @param output - OutputStream to copy to
     * @throws IOException - io error in function
     * @throws OSSMultiException - double error in function
     */
    public static void copyStreamToStream(final InputStream input, final OutputStream output) throws IOException {
        InputStream is = null;
        OutputStream os = null;
        int ch;

        try {
            if (input instanceof BufferedInputStream) {
                is = input;
            } else {
                is = new BufferedInputStream(input);
            }
            if (output instanceof BufferedOutputStream) {
                os = output;
            } else {
                os = new BufferedOutputStream(output);
            }

            while ((ch = is.read()) != -1) {
                os.write(ch);
            }
            os.flush();
        } finally {
            IOException exec1 = null;
            IOException exec2 = null;
            try {
                // because this close can throw exception we do next close in 
                // finally statement
                if (os != null) {
                    try {
                        os.close();
                    } catch (final IOException exec) {
                        exec1 = exec;
                    }
                }
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (final IOException exec) {
                        exec2 = exec;
                    }
                }
            }
            if ((exec1 != null) && (exec2 != null)) {
                throw exec1;
            } else if (exec1 != null) {
                throw exec1;
            } else if (exec2 != null) {
                throw exec2;
            }
        }
    }
}

Related

  1. CopyStreamToFile(InputStream inputStream, File outputFile)
  2. copyStreamToFile(InputStream inputStream, File outputFile)
  3. copyStreamToFile(InputStream is, File outputFile)
  4. copyStreamToFile(InputStream stream, String outputFilePath)
  5. copyStreamToFileBinary(final InputStream stream, final File output)
  6. copyStreamToStream(InputStream in, OutputStream out)
  7. inputStreamToOutputStream(final InputStream in, final OutputStream out)
  8. inputStreamToOutputStream(final InputStream in, final OutputStream out, int buffer)
  9. inputStreamToOutputStream(final InputStream inputStream, final OutputStream outputStream)