Java InputStream to OutputStream copyStream(InputStream sourceStream, OutputStream destinationStream)

Here you can find the source of copyStream(InputStream sourceStream, OutputStream destinationStream)

Description

Copies the data from an InputStream object to an OutputStream object.

License

Open Source License

Parameter

Parameter Description
sourceStream The input stream to be read.
destinationStream The output stream to be written to.

Return

int value of the number of bytes copied.

Declaration

public static int copyStream(InputStream sourceStream, OutputStream destinationStream) throws IOException 

Method Source Code

//package com.java2s;
/*//from   w  w  w .  j  a  va2 s  . co m
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */

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

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

    /**
     * Copies the data from an InputStream object to an OutputStream object.
     * 
     * @param sourceStream
     *            The input stream to be read.
     * @param destinationStream
     *            The output stream to be written to.
     * @return int value of the number of bytes copied.
     * @exception IOException
     *                from java.io calls.
     */
    public static int copyStream(InputStream sourceStream, OutputStream destinationStream) throws IOException {
        int bytesRead = 0;
        int totalBytes = 0;
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

        while (bytesRead >= 0) {
            bytesRead = sourceStream.read(buffer, 0, buffer.length);

            if (bytesRead > 0) {
                destinationStream.write(buffer, 0, bytesRead);
            }

            totalBytes += bytesRead;
        }
        destinationStream.flush();
        destinationStream.close();
        return totalBytes;
    }
}

Related

  1. copyStream(InputStream source, OutputStream dest)
  2. copyStream(InputStream source, OutputStream dest)
  3. copyStream(InputStream source, OutputStream dest)
  4. copyStream(InputStream source, OutputStream destination, byte[] buffer)
  5. copyStream(InputStream sourceInputStream, OutputStream targetOutputStream)
  6. copyStream(InputStream sourceStream, OutputStream destinationStream, boolean closeInput, boolean closeOutput)
  7. copyStream(InputStream src, OutputStream dest)
  8. copyStream(InputStream src, OutputStream dest)
  9. copyStream(InputStream src, OutputStream dest, boolean closeStreams, String title)