Java InputStream Copy to File copyStreamToFile(InputStream stream, File destFile)

Here you can find the source of copyStreamToFile(InputStream stream, File destFile)

Description

Copies data from the specified input stream to a file.

License

Open Source License

Parameter

Parameter Description
stream An InputStream containing the data to be copied.
destFile A File object representing the path (and name) of the destination file.

Exception

Parameter Description
IOException an exception

Declaration

public static void copyStreamToFile(InputStream stream, File destFile) throws IOException 

Method Source Code

//package com.java2s;
/*==========================================================================*\
 |  $Id: FileUtilities.java,v 1.5 2014/06/16 15:59:40 stedwar2 Exp $
 |*-------------------------------------------------------------------------*|
 |  Copyright (C) 2006-2012 Virginia Tech
 |
 |  This file is part of Web-CAT.// w w  w .  ja  v a2s.  c o  m
 |
 |  Web-CAT is free software; you can redistribute it and/or modify
 |  it under the terms of the GNU Affero General Public License as published
 |  by the Free Software Foundation; either version 3 of the License, or
 |  (at your option) any later version.
 |
 |  Web-CAT 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 Affero General Public License
 |  along with Web-CAT; if not, see <http://www.gnu.org/licenses/>.
\*==========================================================================*/

import java.io.File;

import java.io.FileOutputStream;

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

public class Main {
    /**
     * Copies data from the specified input stream to a file.
     *
     * @param stream An InputStream containing the data to be copied.
     * @param destFile A File object representing the path (and name) of the
     * destination file.
     *
     * @throws IOException
     */
    public static void copyStreamToFile(InputStream stream, File destFile) throws IOException {
        OutputStream outStream = new FileOutputStream(destFile);
        copyStream(stream, outStream);
        outStream.flush();
        outStream.close();
    }

    /**
     * Copies data from the specified input stream to a file and sets the
     * file's timestamp.
     *
     * @param stream An InputStream containing the data to be copied.
     * @param destFile A File object representing the path (and name) of the
     * destination file.
     * @param fileTime The timestamp to use for the destination file
     *
     * @throws IOException
     */
    public static void copyStreamToFile(InputStream stream, File destFile, long fileTime) throws IOException {
        copyStreamToFile(stream, destFile);
        if (fileTime != -1) {
            destFile.setLastModified(fileTime);
        }
    }

    /**
     * Copies the contents of an input stream onto an existing output
     * stream.  The output stream is flushed when the operation
     * is complete.
     *
     * @param in  the input stream to copy
     * @param out the destination
     * @throws IOException if there are IO errors
     */
    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        final int BUFFER_SIZE = 65536;

        // read in increments of BUFFER_SIZE
        byte[] b = new byte[BUFFER_SIZE];
        int count = in.read(b);
        while (count > -1) {
            out.write(b, 0, count);
            count = in.read(b);
        }
        out.flush();
    }
}

Related

  1. copyStreamToFile(InputStream in, File out)
  2. copyStreamToFile(InputStream in, File target)
  3. copyStreamToFile(InputStream inputStream, File destFile)
  4. copyStreamToFile(InputStream pInputStream, File pFile)
  5. copyStreamToFile(InputStream source, File target)
  6. copyStreamToFile(InputStream stream, File destFile, long fileTime)
  7. copyStreamToFile(InputStream stream, File file)