Java File Copy nio copyToTempFile(final InputStream is, final String prefix, final String suffix)

Here you can find the source of copyToTempFile(final InputStream is, final String prefix, final String suffix)

Description

Copies the stream to the temporary file.

License

Apache License

Parameter

Parameter Description
is the input stream
prefix the prefix part of the file name
suffix the suffix part of the file name

Exception

Parameter Description

Return

the temporary file

Declaration

public static File copyToTempFile(final InputStream is, final String prefix, final String suffix)
        throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.FileChannel;
import java.nio.channels.Channels;
import java.nio.ByteBuffer;

public class Main {
    /**/* w  w  w  . ja v  a 2  s . c o  m*/
     * Copies the stream to the temporary file.
     *
     * @param is     the input stream
     * @param prefix the prefix part of the file name
     * @param suffix the suffix part of the file name
     * @return the temporary file
     * @throws java.io.IOException I/O Exception
     */
    public static File copyToTempFile(final InputStream is, final String prefix, final String suffix)
            throws IOException {
        return copyToFile(is, File.createTempFile(prefix, suffix));
    }

    /**
     * Copies the stream to the temporary file.
     *
     * @param is the input stream
     * @param file the file
     * @return the temporary file
     * @throws IOException I/O Exception
     */
    public static File copyToFile(final InputStream is, final File file) throws IOException {
        final FileOutputStream fos = new FileOutputStream(file);

        ReadableByteChannel source = null;
        FileChannel target = null;

        try {
            source = Channels.newChannel(is);
            target = fos.getChannel();

            final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);

            while (source.read(buffer) != -1) {
                // prepare the buffer to be drained
                buffer.flip();
                // make sure the buffer was fully drained.
                while (buffer.hasRemaining()) {
                    target.write(buffer);
                }

                // make the buffer empty, ready for filling
                buffer.clear();
            }
        } finally {
            if (source != null) {
                source.close();
            }

            if (target != null) {
                target.close();
            }
        }

        return file;
    }
}

Related

  1. copyFile(final File source, final File dest)
  2. copyFile(final File source, final File target)
  3. copyFile(final File source, final File target)
  4. copyFileByMapped(String sourcePath, String targetPath)
  5. copyToFile(final InputStream is, final File file)
  6. copyToTmpFile(InputStream in, String prefix, String suffix)
  7. fileCopy(File source, File destination)
  8. fileCopy(File source, File target)
  9. fileCopy(File sourceFile, File destFile)