// First, we need a buffer to hold blocks of copied bytes.
ByteBuffer buffer = ByteBuffer.allocateDirect(32 * 1024);
// Now loop until no more bytes to read and the buffer is empty
while(in.read(buffer) != -1 || buffer.position() > 0) {
// The read() call leaves the buffer in "fill mode". To prepare
// to write bytes from the bufferwe have to put it in "drain mode"
// by flipping it: setting limit to position and position to zero
buffer.flip();
// Now write some or all of the bytes out to the output channel
out.write(buffer);
// Compact the buffer by discarding bytes that were written,
// and shifting any remaining bytes. This method also
// prepares the buffer for the next call to read() by setting the
// position to the limit and the limit to the buffer capacity.
buffer.compact();
}