Example usage for java.nio.channels ReadableByteChannel read

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

Introduction

In this page you can find the example usage for java.nio.channels ReadableByteChannel read.

Prototype

public int read(ByteBuffer dst) throws IOException;

Source Link

Document

Reads a sequence of bytes from this channel into the given buffer.

Usage

From source file:com.linkedin.databus.core.DbusEventBuffer.java

/**
 * Read events from a channel for readEvents().
 *
 * @param readChannel      the channel to read from
 * @param readBuffer       the buffer to read into
 * @param logDebugEnabled  if debug logging is enabled
 * @return true the read succeeded//from w w  w  .  ja v a  2  s.  co m
 */
private boolean readEventsFromChannel(ReadableByteChannel readChannel, ByteBuffer readBuffer,
        boolean logDebugEnabled) {
    if (logDebugEnabled)
        _log.debug("reading events from channel to " + readBuffer);
    boolean success = true;
    long oneread = 1;
    while (success && oneread > 0) {
        try {
            oneread = readChannel.read(readBuffer);
            if (logDebugEnabled)
                _log.debug("Read " + oneread + " bytes");
        } catch (IOException e) {
            _log.error("readEvents error: " + e.getMessage(), e);
            success = false;
        }
    }
    if (logDebugEnabled)
        _log.debug("read events from channel success=" + success + " to " + readBuffer);

    return success;
}

From source file:com.sonicle.webtop.mail.Service.java

public static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
        throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
    while (src.read(buffer) != -1) {
        // prepare the buffer to be drained
        buffer.flip();// w w w  . ja  va  2  s  .c o m
        // write to the channel, may block
        dest.write(buffer);
        // If partial transfer, shift remainder down
        // If buffer is empty, same as doing clear()
        buffer.compact();
    }
    // EOF will leave buffer in fill state
    buffer.flip();
    // make sure the buffer is fully drained.
    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}