Java ReadableByteChannel Copy copyTo(ReadableByteChannel from, WritableByteChannel to)

Here you can find the source of copyTo(ReadableByteChannel from, WritableByteChannel to)

Description

Copies all bytes from the readable channel to the writable channel.

License

Open Source License

Parameter

Parameter Description
from the readable channel to read from
to the writable channel to write to

Exception

Parameter Description
IOException if an I/O error occurs

Return

the number of bytes copied

Declaration

public static long copyTo(ReadableByteChannel from, WritableByteChannel to) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

import java.nio.channels.ReadableByteChannel;

import java.nio.channels.Selector;

import java.nio.channels.WritableByteChannel;

public class Main {
    private static final int BUF_SIZE = 0x1000;

    /**/*w  ww .  j  a v  a  2  s  .  co  m*/
     * Copies all bytes from the readable channel to the writable channel.
     * Does not close or flush either channel.
     *
     * @param from the readable channel to read from
     * @param to the writable channel to write to
     * @return the number of bytes copied
     * @throws IOException if an I/O error occurs
     */
    public static long copyTo(ReadableByteChannel from, WritableByteChannel to) throws IOException {
        return copyTo(from, to, false);
    }

    /**
     * Copies all bytes from the readable channel to the writable channel.
     * Will close both channels when done if specified.
     *
     * @param from the readable channel to read from
     * @param to the writable channel to write to
     * @param close - Close the channels when finished.
     * @return the number of bytes copied
     * @throws IOException if an I/O error occurs
     */
    public static long copyTo(ReadableByteChannel from, WritableByteChannel to, boolean close) throws IOException {
        ByteBuffer buf = ByteBuffer.allocate(BUF_SIZE);
        long total = 0;
        try {
            while (from.read(buf) != -1) {
                buf.flip();
                while (buf.hasRemaining()) {
                    total += to.write(buf);
                }
                buf.clear();
            }
        } finally {
            if (close) {
                close(from);
                close(to);
            }
        }
        return total;
    }

    /**
     * Shutdown the given channel.
     *
     * @param channel The channel to close.
     * @return True if successful, false if an error occurred
     */
    public static boolean close(Channel channel) {
        try {
            channel.close();
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * Shutdown the given selector.
     *
     * @param selector The selector to close.
     * @return True if successful, false if an error occurred
     */
    public static boolean close(Selector selector) {
        try {
            selector.close();
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
}

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. copy(ReadableByteChannel src, WritableByteChannel dest)
  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)
  9. fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest)