Java FileOutputStream Write saveStream(InputStream stream, File targetFile)

Here you can find the source of saveStream(InputStream stream, File targetFile)

Description

Saves the data from an InputStream into a file.

License

Open Source License

Parameter

Parameter Description
stream The stream to read the data from.
targetFile The file where to store the data.

Exception

Parameter Description
IOException When saving failed or when the InputStream throws anIOException.

Declaration

public static void saveStream(InputStream stream, File targetFile) throws IOException 

Method Source Code

//package com.java2s;
/*//from   w w w  .  j  a  v a 2  s .  c o  m
 * TV-Browser
 * Copyright (C) 04-2003 Martin Oberhauser (martin_oat@yahoo.de)
 *
 * This program 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.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * CVS information:
 *  $RCSfile$
 *   $Source$
 *     $Date: 2010-11-21 15:38:33 +0100 (Sun, 21 Nov 2010) $
 *   $Author: bananeweizen $
 * $Revision: 6835 $
 */

import java.io.BufferedOutputStream;

import java.io.File;

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

public class Main {
    /**
     * Saves the data from an InputStream into a file.
     *
     * @param stream The stream to read the data from.
     * @param targetFile The file where to store the data.
     * @throws IOException When saving failed or when the InputStream throws an
     *         IOException.
     */
    public static void saveStream(InputStream stream, File targetFile) throws IOException {
        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(targetFile), 0x4000);

            pipeStreams(stream, out);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException exc) {
            }
        }
    }

    /**
     * Pipes all data from the specified InputStream to the specified OutputStream,
     * until the InputStream has no more data.
     * <p>
     * Note: None of the streams is closed! You have to do that for yourself!
     *
     * @param from The stream to read the data from.
     * @param to The stream to write the data to.
     * @throws IOException Thrown if something goes wrong.
     */
    public static void pipeStreams(InputStream from, OutputStream to) throws IOException {
        int len;
        byte[] buffer = new byte[10240];
        while ((len = (from.read(buffer))) != -1) {
            to.write(buffer, 0, len);
        }
    }
}

Related

  1. saveKeyToFile(String key)
  2. saveLongList(String file, Collection c, boolean append)
  3. saveProxyClass(String path, String proxyClassName, Class[] interfaces)
  4. saveResouce(String resourceName, String outputFile)
  5. saveStream(InputStream is, File output)
  6. saveStreamToFile(InputStream in, File outFile)
  7. saveStreamToFile(InputStream is, File destFile)
  8. saveStringIntoFile(File file, String contents)
  9. saveStringIntoFile(String filePath, String contents)