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

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

Description

Reads an InputStream and copy all data to an OutputStream.

License

Open Source License

Parameter

Parameter Description
in the Input Stream
out the Output Stream
closeOut if true, out is closed when the copy has finished

Exception

Parameter Description
IOException if an I/O error occurs.

Declaration

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

Method Source Code

//package com.java2s;
/*//from   w ww.  j  a va  2  s  .c  om
 * Copyright 2004-2009 Luciano Vernaschi
 *
 * This file is part of MeshCMS.
 *
 * MeshCMS 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.
 *
 * MeshCMS 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 MeshCMS.  If not, see <http://www.gnu.org/licenses/>.
 */

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

import java.io.OutputStream;

public class Main {
    /**
     * A standard size for buffers.
     */
    public static final int BUFFER_SIZE = 2048;

    /**
     * Reads an <code>InputStream</code> and copy all data to an
     * <code>OutputStream</code>.
     *
     * @param in the Input Stream
     * @param out the Output Stream
     * @param closeOut if true, out is closed when the copy has finished
     *
     * @throws IOException if an I/O error occurs.
     */
    public static void copyStream(InputStream in, OutputStream out, boolean closeOut) throws IOException {
        byte b[] = new byte[BUFFER_SIZE];
        int n;

        try {
            while ((n = in.read(b)) != -1) {
                out.write(b, 0, n);
            }
        } finally {
            try {
                in.close();
            } finally {
                if (closeOut) {
                    out.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, boolean closeIn, boolean closeOut)
  6. copyStream(InputStream in, OutputStream out, int bufferSize)
  7. copyStream(InputStream in, OutputStream out, int bufsize)
  8. copyStream(InputStream in, OutputStream out, int len)
  9. copyStream(InputStream in, OutputStream out, JarEntry entry)