Java ReadableByteChannel Copy fastCopy(final ReadableByteChannel src, final WritableByteChannel dest)

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

Description

An efficient copy between two channels with a fixed-size buffer.

License

Apache License

Parameter

Parameter Description
src the source channel
dest the destination channel

Exception

Parameter Description
IOException if the copy fails

Declaration

public static void fastCopy(final ReadableByteChannel src,
        final WritableByteChannel dest) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  ww w. j  av a  2s . c o m*/
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the ?License??). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

import java.io.IOException;

import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class Main {
    /**
     * An efficient copy between two channels with a fixed-size buffer.
     *
     * @param src the source channel
     * @param dest the destination channel
     * @throws IOException if the copy fails
     */
    public static void fastCopy(final ReadableByteChannel src,
            final WritableByteChannel dest) throws IOException {
        // TODO(yupeng): make the buffer size configurable
        final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);

        while (src.read(buffer) != -1) {
            buffer.flip();
            dest.write(buffer);
            buffer.compact();
        }

        buffer.flip();

        while (buffer.hasRemaining()) {
            dest.write(buffer);
        }
    }
}

Related

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