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

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

Description

Copies bytes from input stream into the output stream.

License

GNU General Public License

Exception

Parameter Description
IOExceptionIf either passed stream will throw IOException.
NullPointerException If either passed stream is null.

Declaration

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

Method Source Code


//package com.java2s;
/*//from   w ww.ja v a  2 s. c o m
 * Cobertura - http://cobertura.sourceforge.net/
 *
 * Copyright (C) 2005 Grzegorz Lukasik
 * Copyright (C) 2006 John Lewis
 * Copyright (C) 2007 Ignat Zapolsky
 *
 * Note: This file is dual licensed under the GPL and the Apache
 * Source License (so that it can be used from both the main
 * Cobertura classes and the ant tasks).
 *
 * Cobertura 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 2 of the License,
 * or (at your option) any later version.
 *
 * Cobertura 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 Cobertura; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

import java.io.*;

public class Main {
    /**
     * Copies bytes from input stream into the output stream.  Stops
     * when the input stream read method returns -1.  Does not close
     * the streams.
     *
     * @throws IOException          If either passed stream will throw IOException.
     * @throws NullPointerException If either passed stream is null.
     */
    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        // NullPointerException is explicity thrown to guarantee expected behaviour
        if (in == null || out == null)
            throw new NullPointerException();

        int el;
        byte[] buffer = new byte[1 << 15];
        while ((el = in.read(buffer)) != -1) {
            out.write(buffer, 0, el);
        }
    }
}

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