Java InputStream to OutputStream inputStreamToOutputStream(final InputStream in, final OutputStream out)

Here you can find the source of inputStreamToOutputStream(final InputStream in, final OutputStream out)

Description

Copy an input stream completely to an output stream.

License

Open Source License

Parameter

Parameter Description
in The stream to read.
out The stream to write.

Exception

Parameter Description
IOException If problems occur.

Declaration

public static void inputStreamToOutputStream(final InputStream in, final OutputStream out) throws IOException 

Method Source Code

//package com.java2s;
/*//ww  w.j a v  a 2s .c  om
 * Copyright (c) 2003 Stephan D. Cote' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   Stephan D. Cote 
 *      - Initial concept and implementation
 */

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

import java.io.OutputStream;

public class Main {
    /** Size of block used in IO (4096 bytes) */
    static final int CHUNK_SIZE = 4096;

    /**
     * Copy an input stream completely to an output stream.
     *
     * @param in The stream to read.
     * @param out The stream to write.
     *
     * @throws IOException If problems occur.
     */
    public static void inputStreamToOutputStream(final InputStream in, final OutputStream out) throws IOException {
        byte[] buffer = null;
        boolean streaming = true;

        while (streaming) {
            buffer = new byte[CHUNK_SIZE];

            final int count = in.read(buffer);

            if (count == -1) {
                streaming = false;
            } else {
                if (count < CHUNK_SIZE) {
                    streaming = false;
                }

                out.write(buffer, 0, count);
            }
        }
    }
}

Related

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