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

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

Description

Copy all bytes from in to out.

License

Open Source License

Parameter

Parameter Description
in a parameter
out a parameter

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code

//package com.java2s;
/*/* w w w .  j  ava2  s.co  m*/
 * Copyright (c) 2012 EDC
 * 
 * This file is part of Stepping Stone.
 * 
 * Stepping Stone is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Stepping Stone is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Stepping Stone.  If not, see <http://www.gnu.org/licenses/gpl.txt>.
 */

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

import java.io.Reader;

import java.util.zip.ZipFile;

public class Main {
    /**
     * Copy all bytes from in to out. Flush out at the end but do not close the streams.
     * 
     * @param in
     * @param out
     * @throws IOException
     */
    public static int copyStream(InputStream in, OutputStream out, boolean closeIn, boolean closeOut)
            throws IOException {
        int totalBytes = 0;
        try {
            byte[] buff = new byte[1024 * 4];
            int bytesRead = 0;
            while ((bytesRead = in.read(buff)) != -1) {
                if (bytesRead > 0) {
                    out.write(buff, 0, bytesRead);
                    totalBytes += bytesRead;
                }
            }
            out.flush();
        } finally {
            if (closeIn)
                closeGracefully(in);

            if (closeOut)
                closeGracefully(out);
        }
        return totalBytes;
    }

    public static int copyStream(InputStream in, OutputStream out) throws IOException {
        return copyStream(in, out, true, true);
    }

    public static void closeGracefully(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ignore) {
            }
        }
    }

    public static void closeGracefully(Reader r) {
        if (r != null) {
            try {
                r.close();
            } catch (IOException ignore) {
            }
        }
    }

    public static void closeGracefully(OutputStream out) {
        if (out != null) {
            try {
                out.close();
            } catch (IOException ignore) {
            }
        }
    }

    public static void closeGracefully(ZipFile zipFile) {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ignore) {
            }
        }
    }
}

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, boolean closeOut)
  7. copyStream(InputStream in, OutputStream out, int bufferSize)
  8. copyStream(InputStream in, OutputStream out, int bufsize)
  9. copyStream(InputStream in, OutputStream out, int len)