Here you can find the source of copyStream(InputStream sourceStream, OutputStream destinationStream, boolean closeInput, boolean closeOutput)
Parameter | Description |
---|---|
sourceStream | InputStream to copy from. |
destinationStream | OutputStream to copy to. |
closeInput | quietly close InputStream . |
closeOutput | quietly close OutputStream |
Parameter | Description |
---|---|
IOException | in case something bad happens. |
public static void copyStream(InputStream sourceStream, OutputStream destinationStream, boolean closeInput, boolean closeOutput) throws IOException
//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"); } }