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

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

Description

Copies the content from in into os.

License

Open Source License

Parameter

Parameter Description
in a parameter
os a parameter

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2013, Linagora//from   ww w.j  av  a2 s .co  m
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *       Linagora - initial API and implementation
 *******************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * Copies the content from in into os.
     * <p>
     * Neither <i>in</i> nor <i>os</i> are closed by this method.<br />
     * They must be explicitly closed after this method is called.
     * </p>
     *
     * @param in
     * @param os
     * @throws IOException
     */
    public static void copyStream(InputStream in, OutputStream os) throws IOException {

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            os.write(buf, 0, len);
        }
    }

    /**
     * Copies the content from in into outputFile.
     * <p>
     * <i>in</i> is not closed by this method.<br />
     * It must be explicitly closed after this method is called.
     * </p>
     *
     * @param in
     * @param outputFile will be created if it does not exist
     * @throws IOException
     */
    public static void copyStream(InputStream in, File outputFile) throws IOException {

        if (!outputFile.exists() && !outputFile.createNewFile())
            throw new IOException("Failed to create " + outputFile.getAbsolutePath() + ".");

        OutputStream os = new FileOutputStream(outputFile);
        copyStream(in, os);
        os.close();
    }

    /**
     * Copies the content from inputFile into outputFile.
     *
     * @param inputFile
     * @param outputFile will be created if it does not exist
     * @throws IOException
     */
    public static void copyStream(File inputFile, File outputFile) throws IOException {

        InputStream is = new FileInputStream(inputFile);
        copyStream(is, outputFile);
        is.close();
    }

    /**
     * Copies the content from inputFile into an output stream.
     *
     * @param inputFile
     * @param os the output stream
     * @throws IOException
     */
    public static void copyStream(File inputFile, OutputStream os) throws IOException {

        InputStream is = new FileInputStream(inputFile);
        copyStream(is, os);
        is.close();
    }
}

Related

  1. copyStream(InputStream fis, OutputStream fos)
  2. copyStream(InputStream from, OutputStream to)
  3. copyStream(InputStream in, boolean closeIn, OutputStream out, boolean closeOut)
  4. copyStream(InputStream in, File outputFile)
  5. copyStream(InputStream in, FileOutputStream out, IProgressMonitor monitor, int length)
  6. copyStream(InputStream in, OutputStream os)
  7. copyStream(InputStream in, OutputStream out)
  8. copyStream(InputStream in, OutputStream out)
  9. copyStream(InputStream in, OutputStream out)