Here you can find the source of fastCopy(final ReadableByteChannel src, final WritableByteChannel dest)
Parameter | Description |
---|---|
src | the source channel |
dest | the destination channel |
Parameter | Description |
---|---|
IOException | if the copy fails |
public static void fastCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException
//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); } } }