Java File Read via ByteBuffer copyChannel(int bufferSize, ReadableByteChannel source, WritableByteChannel destination)

Here you can find the source of copyChannel(int bufferSize, ReadableByteChannel source, WritableByteChannel destination)

Description

Copies the content of the source channel onto the destination channel.

License

Open Source License

Parameter

Parameter Description
bufferSize size of the temp buffer to use for this copy.
source the source ReadableByteChannel .
destination the destination WritableByteChannel ;.

Exception

Parameter Description
IOException in case something bad happens.

Declaration

public static void copyChannel(int bufferSize,
        ReadableByteChannel source, WritableByteChannel destination)
        throws IOException 

Method Source Code

//package com.java2s;
/* (c) 2015 Open Source Geospatial Foundation - all rights reserved
 * (c) 2001 - 2015 OpenPlans//from   w  ww  . j  av a2  s  . c  om
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */

import java.io.IOException;

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

public class Main {
    /**
     * Copies the content of the source channel onto the destination channel.
     * 
     * @param bufferSize size of the temp buffer to use for this copy.
     * @param source the source {@link ReadableByteChannel}.
     * @param destination the destination {@link WritableByteChannel};.
     * @throws IOException in case something bad happens.
     */
    public static void copyChannel(int bufferSize,
            ReadableByteChannel source, WritableByteChannel destination)
            throws IOException {

        inputNotNull(source, destination);
        if (!source.isOpen() || !destination.isOpen())
            throw new IllegalStateException(
                    "Source and destination channels must be open.");

        final java.nio.ByteBuffer buffer = java.nio.ByteBuffer
                .allocateDirect(bufferSize);
        while (source.read(buffer) != -1) {
            //prepare the buffer for draining
            buffer.flip();

            //write to destination
            while (buffer.hasRemaining())
                destination.write(buffer);

            //clear
            buffer.clear();

        }

    }

    /**
     * Checks if the input is not null.
     * @param oList list of elements to check for null.
     */
    private static void inputNotNull(Object... oList) {
        for (Object o : oList)
            if (o == null)
                throw new NullPointerException(
                        "Input objects cannot be null");

    }
}

Related

  1. channelCopy2(ReadableByteChannel src, WritableByteChannel dest)
  2. ClientReadWithBlock(SocketChannel sc)
  3. ClientReadWithWait(SocketChannel sc)
  4. copy(final ReadableByteChannel src, final WritableByteChannel dest)
  5. copy(final ReadableByteChannel srcChannel, final WritableByteChannel destChannel)
  6. copyStream(BufferedReader reader, BufferedWriter writer)
  7. count(final ReadableByteChannel src)
  8. getReadBuffer(File file)
  9. getStringContents(ReadableByteChannel channel)