Example usage for java.nio.channels WritableByteChannel isOpen

List of usage examples for java.nio.channels WritableByteChannel isOpen

Introduction

In this page you can find the example usage for java.nio.channels WritableByteChannel isOpen.

Prototype

public boolean isOpen();

Source Link

Document

Tells whether or not this channel is open.

Usage

From source file:it.geosolutions.tools.io.file.IOUtils.java

/**
 * Copies the content of the source channel onto the destination channel.
 * //w w  w. j a  v  a 2 s . com
 * @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 {

    Objects.notNull(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();

    }

}

From source file:com.rogiel.httpchannel.service.channel.LinkedUploadChannelContentBody.java

@Override
public void writeTo(OutputStream out) throws IOException {
    final WritableByteChannel outputChannel = Channels.newChannel(out);
    channel.linkChannel(outputChannel);/*from   www. jav a 2 s  .  c  o  m*/
    while (channel.isOpen() && outputChannel.isOpen()) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        }
    }
}

From source file:org.geoserver.rest.util.IOUtils.java

/**
 * Copies the content of the source channel onto the destination channel.
 * /*w ww .j  ava2  s  .  c om*/
 * @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();

    }

}