Example usage for java.nio.channels ReadableByteChannel isOpen

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

Introduction

In this page you can find the example usage for java.nio.channels ReadableByteChannel 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.
 * //from  ww w .  jav a2  s  . c o m
 * @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:org.callimachusproject.server.AccessLog.java

InputStream logOnClose(final String addr, final String username, final String line, final int code,
        final long length, final Header referer, final Header agent, InputStream in) {
    final ReadableByteChannel delegate = ChannelUtil.newChannel(in);
    return ChannelUtil.newInputStream(new ReadableByteChannel() {
        private long size = 0;
        private boolean complete;
        private boolean error;

        public boolean isOpen() {
            return delegate.isOpen();
        }// ww  w  .  j  a  va2  s. c o  m

        public synchronized void close() throws IOException {
            delegate.close();
            if (!complete) {
                complete = true;
                if (error) {
                    log(addr, username, line, 599, size, referer, agent);
                } else if (size < length) {
                    log(addr, username, line, 499, size, referer, agent);
                } else {
                    log(addr, username, line, code, size, referer, agent);
                }
            }
        }

        public synchronized int read(ByteBuffer dst) throws IOException {
            error = true;
            int read = delegate.read(dst);
            if (read < 0) {
                complete = true;
                log(addr, username, line, code, size, referer, agent);
            } else {
                size += read;
            }
            error = false;
            return read;
        }
    });
}

From source file:org.darkware.wpman.security.ChecksumDatabase.java

/**
 * Perform a checksum calculation on the given {@link ReadableByteChannel}. Other code should not create
 * implementations which are dependant on any particular characteristics of the checksum, but the checksum
 * is very likely to be based on a cryptographic-strength hash. The results of the checksum are encoded as
 * a base64 {@code String}.//w  w w .  j a  v  a2  s.  com
 *
 * @param channel The {@code ReadableByteChannel} to read data from.
 * @return A Base64 encoded {@code String} representing the checksum.
 * @throws IOException If there was an error while reading data from the channel.
 * @see Base64#encodeBase64String(byte[])
 */
protected String doChecksum(ReadableByteChannel channel) throws IOException {
    Hasher hasher = Hashing.sha256().newHasher();

    final ByteBuffer block = ByteBuffer.allocate(4096);
    while (channel.isOpen()) {
        int bytesRead = channel.read(block);
        if (bytesRead > 0) {
            block.flip();
            hasher.putBytes(block.array(), 0, block.limit());
            block.clear();
        } else if (bytesRead == -1) {
            channel.close();
        }
    }

    return Base64.encodeBase64String(hasher.hash().asBytes());
}

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

/**
 * Copies the content of the source channel onto the destination channel.
 * /*from w w  w .  ja v a 2 s  . c  o  m*/
 * @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();

    }

}