Here you can find the source of copy(final ReadableByteChannel src, final WritableByteChannel dest)
public static void copy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; public class Main { public static void copy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { buffer.flip();/*from ww w . ja v a 2s.c o m*/ dest.write(buffer); buffer.compact(); } buffer.flip(); while (buffer.hasRemaining()) { dest.write(buffer); } } public static void copy(File source, File dest) throws IOException { try (InputStream is = new FileInputStream(source); FileOutputStream destination = new FileOutputStream(dest);) { copy(Channels.newChannel(is), destination.getChannel()); } } }