Java InputStream to OutputStream copyStream(InputStream in, OutputStream out)

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

Description

Copies streams.

License

LGPL

Parameter

Parameter Description
in Input stream
out Output stream

Exception

Parameter Description
IOException In case of a reading or writing error

Declaration

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

Method Source Code

//package com.java2s;
/**//  w ww. j a v  a  2 s  .c  o m
 * Copyright 2009-2016 Three Crickets LLC.
 * <p>
 * The contents of this file are subject to the terms of the LGPL version 3.0:
 * http://www.gnu.org/copyleft/lesser.html
 * <p>
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly from Three Crickets
 * at http://threecrickets.com/
 */

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

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

import java.io.OutputStream;

public class Main {
    /**
     * Copies streams. The input stream is entirely consumed and closed.
     * 
     * @param in
     *        Input stream
     * @param out
     *        Output stream
     * @throws IOException
     *         In case of a reading or writing error
     */
    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        in = new BufferedInputStream(in);
        try {
            out = new BufferedOutputStream(out);
            try {
                while (true) {
                    int data = in.read();
                    if (data == -1)
                        break;
                    out.write(data);
                }
            } finally {
                out.flush();
            }
        } finally {
            in.close();
        }
    }
}

Related

  1. copyStream(InputStream in, OutputStream out)
  2. copyStream(InputStream in, OutputStream out)
  3. copyStream(InputStream in, OutputStream out)
  4. copyStream(InputStream in, OutputStream out)
  5. copyStream(InputStream in, OutputStream out)
  6. copyStream(InputStream in, OutputStream out)
  7. copyStream(InputStream in, OutputStream out)
  8. copyStream(InputStream in, OutputStream out)
  9. copyStream(InputStream in, OutputStream out)