Java ReadableByteChannel Copy fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest)

Here you can find the source of fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest)

Description

fast Channel Copy

License

Open Source License

Declaration

private static void fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException 

Method Source Code


//package com.java2s;
import java.io.*;
import java.nio.channels.*;
import java.nio.*;

public class Main {
    private static void fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
        while (src.read(buffer) != -1) {
            // prepare the buffer to be drained
            buffer.flip();/*from w w  w  .  jav  a2 s .c o m*/
            // write to the channel, may block
            dest.write(buffer);
            // If partial transfer, shift remainder down
            // If buffer is empty, same as doing clear()
            buffer.compact();
        }
        // EOF will leave buffer in fill state
        buffer.flip();
        // make sure the buffer is fully drained.
        while (buffer.hasRemaining()) {
            dest.write(buffer);
        }
    }
}

Related

  1. copy(ReadableByteChannel src, WritableByteChannel dest)
  2. copyTo(ReadableByteChannel from, WritableByteChannel to)
  3. fastChannelCopy(final ReadableByteChannel input, final WritableByteChannel output, long toRead)
  4. fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
  5. fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
  6. fastCopy(final ReadableByteChannel src, final WritableByteChannel dest)