Here you can find the source of fastChannelCopy(final ReadableByteChannel input, final WritableByteChannel output, long toRead)
private static void fastChannelCopy(final ReadableByteChannel input, final WritableByteChannel output, long toRead) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; public class Main { private static final int DEFAULT_BUFFER_SIZE = 10240; private static void fastChannelCopy(final ReadableByteChannel input, final WritableByteChannel output, long toRead) throws IOException { final ByteBuffer buffer = ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE); int read; while (toRead > 0) { read = input.read(buffer);/*from w w w . j av a 2s . c o m*/ if (read > 0) { // prepare the buffer to be drained buffer.flip(); if (toRead < read) { buffer.limit((int) toRead); } // write to the channel, may block output.write(buffer); // If partial transfer, shift remainder down // If buffer is empty, same as doing clear() buffer.compact(); toRead -= read; } else { break; } } } }