Java InputStream to OutputStream copyStream(InputStream is, OutputStream os)

Here you can find the source of copyStream(InputStream is, OutputStream os)

Description

Copy an InputStream to an OutputStream.

License

Open Source License

Parameter

Parameter Description
is the source <tt>InputStream</tt>
os the destination <tt>OutputStream</tt>

Exception

Parameter Description
IOException on error

Return

total number of bytes copied

Declaration

public static int copyStream(InputStream is, OutputStream os) throws IOException 

Method Source Code


//package com.java2s;
/* FileUtils/*from   w w  w. j a va  2 s.  c o m*/
 *
 * $Id: FileUtils.java 5863 2008-07-10 21:38:48Z gojomo $
 *
 * Created on Feb 2, 2004
 *
 * Copyright (C) 2004 Internet Archive.
 *
 * This file is part of the Heritrix web crawler (crawler.archive.org).
 *
 * Heritrix is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * any later version.
 *
 * Heritrix 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 Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with Heritrix; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

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

import java.io.OutputStream;

public class Main {
    /**
     * Copy an <tt>InputStream</tt> to an <tt>OutputStream</tt>. If either
     * stream is not already buffered, then it's wrapped in the corresponding
     * buffered stream (i.e., <tt>BufferedInputStream</tt> or
     * <tt>BufferedOutputStream</tt>) before copying. Calling this method is
     * equivalent to:
     * 
     * <blockquote>
     * 
     * <pre>
     * copyStream(src, dst, 8192);
     * </pre>
     * 
     * </blockquote>
     * 
     * @param is
     *            the source <tt>InputStream</tt>
     * @param os
     *            the destination <tt>OutputStream</tt>
     * 
     * @return total number of bytes copied
     * 
     * @throws IOException
     *             on error
     * 
     * @see #copyStream(InputStream,OutputStream,int)
     * @see #copyReader(Reader,Writer)
     * @see #copyFile(File,File)
     */
    public static int copyStream(InputStream is, OutputStream os) throws IOException {
        return copyStream(is, os, -1);
    }

    /**
     * Copy an <tt>InputStream</tt> to an <tt>OutputStream</tt>. If either
     * stream is not already buffered, then it's wrapped in the corresponding
     * buffered stream (i.e., <tt>BufferedInputStream</tt> or
     * <tt>BufferedOutputStream</tt>) before copying.
     * 
     * @param src
     *            the source <tt>InputStream</tt>
     * @param dst
     *            the destination <tt>OutputStream</tt>
     * @param bufferSize
     *            the buffer size to use, or -1 for a default
     * 
     * @return total number of bytes copied
     * 
     * @throws IOException
     *             on error
     * 
     * @see #copyReader(Reader,Writer,int)
     * @see #copyStream(InputStream,OutputStream)
     * @see #copyFile(File,File)
     */
    public static int copyStream(InputStream src, OutputStream dst, int bufferSize) throws IOException {
        int totalCopied = 0;

        if (!(src instanceof BufferedInputStream)) {
            if (bufferSize > 0)
                src = new BufferedInputStream(src, bufferSize);
            else
                src = new BufferedInputStream(src);
        }

        if (!(dst instanceof BufferedOutputStream)) {
            if (bufferSize > 0)
                dst = new BufferedOutputStream(dst, bufferSize);
            else
                dst = new BufferedOutputStream(dst);
        }

        int b;

        while ((b = src.read()) != -1) {
            dst.write(b);
            totalCopied++;
        }

        dst.flush();

        return totalCopied;
    }
}

Related

  1. copyStream(InputStream inStream, OutputStream outStream)
  2. copyStream(InputStream is, OutputStream os)
  3. copyStream(InputStream is, OutputStream os)
  4. copyStream(InputStream is, OutputStream os)
  5. copyStream(InputStream is, OutputStream os)
  6. copyStream(InputStream is, OutputStream os)
  7. copyStream(InputStream is, OutputStream os)
  8. copyStream(InputStream is, OutputStream os)
  9. copyStream(InputStream is, OutputStream os)