Here you can find the source of copyChannels(ReadableByteChannel in, WritableByteChannel out)
public static void copyChannels(ReadableByteChannel in, WritableByteChannel out) throws IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; public class Main { public static void copyChannels(ReadableByteChannel in, WritableByteChannel out) throws IOException { ByteBuffer buf = ByteBuffer.allocate(16 * 1024); while (in.read(buf) != -1) { buf.flip();//w ww . jav a2 s . c o m out.write(buf); buf.compact(); } buf.flip(); while (buf.hasRemaining()) { out.write(buf); } } }