Java InputStream Copy to File copyStream(final InputStream is, final File destinationFile)

Here you can find the source of copyStream(final InputStream is, final File destinationFile)

Description

Downloads to a temporary location and renames on finish so no partials

License

Open Source License

Parameter

Parameter Description
is a parameter
destinationFile a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static long copyStream(final InputStream is, final File destinationFile) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.google.common.io.ByteStreams;

public class Main {
    private static final int BUF_SIZE = 1_024 * 1_024 * 1;

    /**/* w  ww.j  a v a2  s . co  m*/
     * Downloads to a temporary location and renames on finish so no partials
     *
     * @param is
     * @param destinationFile
     * @return
     * @throws IOException
     */
    public static long copyStream(final InputStream is, final File destinationFile) throws IOException {

        final File tmpFile = File.createTempFile("flickr_download", null);
        try (final BufferedInputStream bis = new BufferedInputStream(is, BUF_SIZE);
                final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tmpFile),
                        BUF_SIZE);) {
            final long total = ByteStreams.copy(bis, bos);
            tmpFile.renameTo(destinationFile);
            return total;
        }

    }
}

Related

  1. copyStream(final InputStream in, final File dest)
  2. copyStream(InputStream copyFrom, File copyTo)
  3. copyStream(InputStream in, File dest)
  4. copyStream(InputStream in, File dest)
  5. copyStreamIntoFile(File outFile, InputStream is)