Java InputStream to OutputStream copyStream(InputStream is, OutputStream os)

Here you can find the source of copyStream(InputStream is, OutputStream os)

Description

copy Stream

License

Open Source License

Declaration

public static void copyStream(InputStream is, OutputStream os) throws IOException 

Method Source Code

//package com.java2s;
/**/*  ww  w.j a va 2 s  . c  o  m*/
 * Copyright (c) 2006-2009, Cloudsmith Inc.
 * The code, documentation and other materials contained herein have been
 * licensed under the Eclipse Public License - v 1.0 by the copyright holder
 * listed above, as the Initial Contributor under such license. The text of
 * such license is available at www.eclipse.org.
 */

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

public class Main {
    public static void copyStream(InputStream is, OutputStream os) throws IOException {
        copyStream(is, os, true, true);
    }

    public static void copyStream(InputStream is, OutputStream os, boolean closeInput, boolean closeOutput)
            throws IOException {
        byte[] buffer = new byte[1024];
        int len;

        try {
            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
        } finally {
            try {
                if (closeInput)
                    is.close();
            } catch (IOException e) {
                // ignore
            }

            if (closeOutput)
                os.close();
        }
    }

    /**
     * @param is
     */
    public static void close(Closeable is) {
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
                // ignore
            }
    }
}

Related

  1. copyStream(InputStream inputStream, OutputStream outputStream)
  2. copyStream(InputStream inStream, OutputStream outStream)
  3. copyStream(InputStream inStream, OutputStream outStream)
  4. copyStream(InputStream is, OutputStream os)
  5. copyStream(InputStream is, OutputStream os)
  6. copyStream(InputStream is, OutputStream os)
  7. copyStream(InputStream is, OutputStream os)
  8. copyStream(InputStream is, OutputStream os)
  9. copyStream(InputStream is, OutputStream os)