Here you can find the source of copyToTmpFile(InputStream in, String prefix, String suffix)
public static File copyToTmpFile(InputStream in, String prefix, String suffix) throws IOException
//package com.java2s; import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; public class Main { public static File copyToTmpFile(InputStream in, String prefix, String suffix) throws IOException { File tempFile = File.createTempFile(prefix, suffix); tempFile.deleteOnExit();/* w ww .ja v a 2 s .c o m*/ copy(in, new FileOutputStream(tempFile)); return tempFile; } public static void copy(InputStream in, OutputStream out) throws IOException { final ReadableByteChannel inputChannel = Channels.newChannel(in); final WritableByteChannel outputChannel = Channels.newChannel(out); // copy the channels copy(inputChannel, outputChannel); // closing the channels inputChannel.close(); outputChannel.close(); } private static void copy(ReadableByteChannel src, WritableByteChannel dest) throws IOException { final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { // prepare the buffer to be drained buffer.flip(); // write to the channel, may block dest.write(buffer); // If partial transfer, shift remainder down // If buffer is empty, same as doing clear() buffer.compact(); } // EOF will leave buffer in fill state buffer.flip(); // make sure the buffer is fully drained. while (buffer.hasRemaining()) { dest.write(buffer); } } }