Java InputStream to OutputStream copyStream(java.io.InputStream inputStream, java.io.OutputStream outputStream)

Here you can find the source of copyStream(java.io.InputStream inputStream, java.io.OutputStream outputStream)

Description

copy Stream

License

Open Source License

Declaration

public static void copyStream(java.io.InputStream inputStream,
            java.io.OutputStream outputStream) throws IOException 

Method Source Code

//package com.java2s;
/*/* ww  w.jav a  2 s  .com*/
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2016 Serge Rieder (serge@jkiss.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (version 2)
 * as published by the Free Software Foundation.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.io.*;

public class Main {
    public static final int DEFAULT_BUFFER_SIZE = 16384;

    public static void copyStream(java.io.InputStream inputStream,
            java.io.OutputStream outputStream) throws IOException {
        copyStream(inputStream, outputStream, DEFAULT_BUFFER_SIZE);
    }

    /**
       Read entire input stream and writes all data to output stream
       then closes input and flushed output
     */
    public static void copyStream(java.io.InputStream inputStream,
            java.io.OutputStream outputStream, int bufferSize)
            throws IOException {
        try {
            byte[] writeBuffer = new byte[bufferSize];
            for (int br = inputStream.read(writeBuffer); br != -1; br = inputStream
                    .read(writeBuffer)) {
                outputStream.write(writeBuffer, 0, br);
            }
            outputStream.flush();
        } finally {
            // Close input stream
            inputStream.close();
        }
    }

    public static void close(Closeable closeable) {
        try {
            closeable.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. copyStream(InputStream src, OutputStream dest, boolean closeStreams, String title)
  2. copyStream(InputStream src, OutputStream osstream)
  3. CopyStream(InputStream sSource, OutputStream sTarget)
  4. copyStream(int bufferSize, InputStream in, OutputStream out, boolean canStop)
  5. copyStream(java.io.InputStream in, java.io.OutputStream out)
  6. copyStream(java.io.InputStream src, java.io.OutputStream dest)
  7. copyStream(OutputStream out, InputStream in, int bufsz)
  8. copyStream(OutputStream outStream, InputStream inStream)
  9. copyStream(Reader input, Writer output)