Java InputStream to OutputStream copyStream(InputStream sourceStream, OutputStream destinationStream, boolean closeInput, boolean closeOutput)

Here you can find the source of copyStream(InputStream sourceStream, OutputStream destinationStream, boolean closeInput, boolean closeOutput)

Description

Copy InputStream to OutputStream .

License

Open Source License

Parameter

Parameter Description
sourceStream InputStream to copy from.
destinationStream OutputStream to copy to.
closeInput quietly close InputStream .
closeOutput quietly close OutputStream

Exception

Parameter Description
IOException in case something bad happens.

Declaration

public static void copyStream(InputStream sourceStream, OutputStream destinationStream, boolean closeInput,
        boolean closeOutput) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

import java.io.OutputStream;

public class Main {
    private static int DEFAULT_SIZE = 1024 * 1024;

    /**//  w ww. j  av  a 2  s .  c  om
     * Copy {@link InputStream} to {@link OutputStream}.
     * 
     * @param sourceStream
     *                {@link InputStream} to copy from.
     * @param destinationStream
     *                {@link OutputStream} to copy to.
     * @param closeInput
     *                quietly close {@link InputStream}.
     * @param closeOutput
     *                quietly close {@link OutputStream}
     * @throws IOException
     *                 in case something bad happens.
     */
    public static void copyStream(InputStream sourceStream, OutputStream destinationStream, boolean closeInput,
            boolean closeOutput) throws IOException {
        copyStream(sourceStream, destinationStream, DEFAULT_SIZE, closeInput, closeOutput);
    }

    /**
     * Copy {@link InputStream} to {@link OutputStream}.
     * 
     * @param sourceStream
     *                {@link InputStream} to copy from.
     * @param destinationStream
     *                {@link OutputStream} to copy to.
     * @param size
     *                size of the buffer to use internally.
     * @param closeInput
     *                quietly close {@link InputStream}.
     * @param closeOutput
     *                quietly close {@link OutputStream}
     * @throws IOException
     *                 in case something bad happens.
     * 
     */
    public static void copyStream(InputStream sourceStream, OutputStream destinationStream, int size,
            boolean closeInput, boolean closeOutput) throws IOException {

        inputNotNull(sourceStream, destinationStream);
        byte[] buf = new byte[size];
        int n = -1;
        try {
            while (-1 != (n = sourceStream.read(buf))) {
                destinationStream.write(buf, 0, n);
                destinationStream.flush();
            }
        } finally {
            // closing streams and connections
            try {
                destinationStream.flush();
            } finally {
                try {
                    if (closeOutput)
                        destinationStream.close();
                } finally {
                    try {
                        if (closeInput)
                            sourceStream.close();
                    } finally {

                    }
                }
            }
        }
    }

    /**
     * Checks if the input is not null.
     * 
     * @param oList
     *                list of elements to check for null.
     */
    private static void inputNotNull(Object... oList) {
        for (Object o : oList)
            if (o == null)
                throw new NullPointerException("Input objects cannot be null");

    }
}

Related

  1. copyStream(InputStream source, OutputStream dest)
  2. copyStream(InputStream source, OutputStream dest)
  3. copyStream(InputStream source, OutputStream destination, byte[] buffer)
  4. copyStream(InputStream sourceInputStream, OutputStream targetOutputStream)
  5. copyStream(InputStream sourceStream, OutputStream destinationStream)
  6. copyStream(InputStream src, OutputStream dest)
  7. copyStream(InputStream src, OutputStream dest)
  8. copyStream(InputStream src, OutputStream dest, boolean closeStreams, String title)
  9. copyStream(InputStream src, OutputStream osstream)