Java InputStream to OutputStream copyStream(InputStream in, boolean closeIn, OutputStream out, boolean closeOut)

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

Description

copy Stream

License

Open Source License

Declaration

public static int copyStream(InputStream in, boolean closeIn, OutputStream out, boolean closeOut)
            throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Red Hat, Inc.//from   w  w  w  .  java2 s . c om
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

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

import java.io.OutputStream;

public class Main {
    public static int copyStream(InputStream in, boolean closeIn, OutputStream out, boolean closeOut)
            throws IOException {
        try {
            int written = 0;
            byte[] buffer = new byte[16 * 1024];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
                written += len;
            }
            return written;
        } finally {
            try {
                if (closeIn) {
                    in.close();
                }
            } finally {
                if (closeOut) {
                    out.close();
                }
            }
        }
    }
}

Related

  1. copyStream(final InputStream source, final OutputStream target)
  2. copyStream(final InputStream src, OutputStream dest)
  3. copyStream(final OutputStream to, final InputStream from)
  4. copyStream(InputStream fis, OutputStream fos)
  5. copyStream(InputStream from, OutputStream to)
  6. copyStream(InputStream in, File outputFile)
  7. copyStream(InputStream in, FileOutputStream out, IProgressMonitor monitor, int length)
  8. copyStream(InputStream in, OutputStream os)
  9. copyStream(InputStream in, OutputStream os)