//package com.java2s;
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 w   w.    d  em    o  2  s.    c  o m */
            out.write(buf);
            buf.compact();
        }
        buf.flip();
        while (buf.hasRemaining()) {
            out.write(buf);
        }
    }
}