Java ReadableByteChannel Copy copy(ReadableByteChannel src, WritableByteChannel dest)

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

Description

Helper method to copy from a readable channel to a writable channel, using an in-memory buffer.

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Salesforce.com, inc..
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * /* www  .  jav  a 2 s . co m*/
 * Contributors:
 *     Salesforce.com, inc. - initial API and implementation
 ******************************************************************************/

import java.io.IOException;
import java.nio.ByteBuffer;

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

public class Main {
    /**
     * Helper method to copy from a readable channel to a writable channel, using an in-memory buffer.
     */
    private static void copy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
        // use an in-memory byte buffer
        ByteBuffer buffer = ByteBuffer.allocate(8092);
        while (src.read(buffer) != -1) {
            buffer.flip();
            while (buffer.hasRemaining()) {
                dest.write(buffer);
            }
            buffer.clear();
        }
    }
}

Related

  1. copy(ReadableByteChannel _in, WritableByteChannel out, long amount)
  2. copy(ReadableByteChannel input, WritableByteChannel output, long start, long length)
  3. copy(ReadableByteChannel inputChannel, WritableByteChannel outputChannel)
  4. copy(ReadableByteChannel source, WritableByteChannel destination)
  5. copyTo(ReadableByteChannel from, WritableByteChannel to)
  6. fastChannelCopy(final ReadableByteChannel input, final WritableByteChannel output, long toRead)
  7. fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
  8. fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)