Here you can find the source of fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest)
private static void fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException
//package com.java2s; import java.io.*; import java.nio.channels.*; import java.nio.*; public class Main { private static void fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException { ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { // prepare the buffer to be drained buffer.flip();/*from w w w . jav a2 s .c o m*/ // 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); } } }