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:IOUtilities.java

/**
 * Copy ALL available data from one stream into another
 * @param in//from   w ww  .j  ava  2  s. co m
 * @param out
 * @throws IOException
 */
public static void copy(InputStream in, OutputStream out) throws IOException {
    ReadableByteChannel source = Channels.newChannel(in);
    WritableByteChannel target = Channels.newChannel(out);

    ByteBuffer buffer = ByteBuffer.allocate(16 * 1024);
    while (source.read(buffer) != -1) {
        buffer.flip(); // Prepare the buffer to be drained
        while (buffer.hasRemaining()) {
            target.write(buffer);
        }
        buffer.clear(); // Empty buffer to get ready for filling
    }

    source.close();
    target.close();

}

From source file:Main.java

public static void copy(ReadableByteChannel in, WritableByteChannel out) throws IOException {
    // First, we need a buffer to hold blocks of copied bytes.
    ByteBuffer buffer = ByteBuffer.allocateDirect(32 * 1024);

    // Now loop until no more bytes to read and the buffer is empty
    while (in.read(buffer) != -1 || buffer.position() > 0) {
        // The read() call leaves the buffer in "fill mode". To prepare
        // to write bytes from the bufferwe have to put it in "drain mode"
        // by flipping it: setting limit to position and position to zero
        buffer.flip();/*  w  ww  .j av a 2 s  .co m*/

        // Now write some or all of the bytes out to the output channel
        out.write(buffer);

        // Compact the buffer by discarding bytes that were written,
        // and shifting any remaining bytes. This method also
        // prepares the buffer for the next call to read() by setting the
        // position to the limit and the limit to the buffer capacity.
        buffer.compact();
    }
}

From source file:com.thinkberg.webdav.Util.java

public static long copyStream(final InputStream is, final OutputStream os) throws IOException {
    ReadableByteChannel rbc = Channels.newChannel(is);
    WritableByteChannel wbc = Channels.newChannel(os);

    int bytesWritten = 0;
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
    while (rbc.read(buffer) != -1) {
        buffer.flip();//from  w  w  w  . ja  v a  2  s  . c  om
        bytesWritten += wbc.write(buffer);
        buffer.compact();
    }
    buffer.flip();
    while (buffer.hasRemaining()) {
        bytesWritten += wbc.write(buffer);
    }

    rbc.close();
    wbc.close();

    return bytesWritten;
}

From source file:org.springframework.boot.devtools.tunnel.payload.HttpTunnelPayload.java

/**
 * Return the payload data for the given source {@link ReadableByteChannel} or null if
 * the channel timed out whilst reading.
 * @param channel the source channel/*from   ww  w. j  av  a  2 s  .  c o  m*/
 * @return payload data or {@code null}
 * @throws IOException in case of I/O errors
 */
public static ByteBuffer getPayloadData(ReadableByteChannel channel) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
    try {
        int amountRead = channel.read(buffer);
        Assert.state(amountRead != -1, "Target server connection closed");
        buffer.flip();
        return buffer;
    } catch (InterruptedIOException ex) {
        return null;
    }
}

From source file:com.github.neoio.nio.util.NIOUtils.java

public static int readToBuffer(ReadableByteChannel channel, ByteBuffer buffer) throws NetIOException {
    try {//from w ww. j a  v  a 2  s  .  c  om
        return channel.read(buffer);
    } catch (IOException e) {
        return -1;
    }
}

From source file:org.springframework.boot.devtools.tunnel.payload.HttpTunnelPayload.java

/**
 * Return the {@link HttpTunnelPayload} for the given message or {@code null} if there
 * is no payload./*w  w w  . j av a2  s .  com*/
 * @param message the HTTP message
 * @return the payload or {@code null}
 * @throws IOException in case of I/O errors
 */
public static HttpTunnelPayload get(HttpInputMessage message) throws IOException {
    long length = message.getHeaders().getContentLength();
    if (length <= 0) {
        return null;
    }
    String seqHeader = message.getHeaders().getFirst(SEQ_HEADER);
    Assert.state(StringUtils.hasLength(seqHeader), "Missing sequence header");
    ReadableByteChannel body = Channels.newChannel(message.getBody());
    ByteBuffer payload = ByteBuffer.allocate((int) length);
    while (payload.hasRemaining()) {
        body.read(payload);
    }
    body.close();
    payload.flip();
    return new HttpTunnelPayload(Long.valueOf(seqHeader), payload);
}

From source file:com.msr.dnsdemo.network.DownloadFile.java

public static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
        throws IOException, NullPointerException {
    if (src != null && dest != null) {
        final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
        while (src.read(buffer) != -1) {
            // prepare the buffer to be drained
            buffer.flip();// w  w w. j  av  a 2  s.co  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);
        }
    }
}

From source file:org.apache.hadoop.hdfs.protocol.datatransfer.PacketReceiver.java

private static void readChannelFully(ReadableByteChannel ch, ByteBuffer buf) throws IOException {
    while (buf.remaining() > 0) {
        int n = ch.read(buf);
        if (n < 0) {
            throw new IOException("Premature EOF reading from " + ch);
        }//from   w  w w  .  jav a 2  s .  c o m
    }
}

From source file:org.carlspring.strongbox.storage.metadata.nuget.TempNupkgFile.java

/**
 * Copies data from one channel to another
 *
 * @param src// w  w w . j  a v a  2 s  .  c o  m
 *            channel source
 * @param dest
 *            destination channel
 * @throws IOException
 *             input / output error
 */
private static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
        throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);

    while (src.read(buffer) != -1) {
        buffer.flip();
        dest.write(buffer);
        buffer.compact();
    }

    buffer.flip();

    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}

From source file:io.uploader.drive.util.FileUtils.java

public static String readAllAndgetMD5(InputStream in) throws IOException {
    com.google.common.hash.HashingInputStream his = null;
    try {//from w w  w  . j  a va 2s  .co m
        his = new com.google.common.hash.HashingInputStream(Hashing.md5(), in);

        final int bufferSize = 2097152;
        final ReadableByteChannel inputChannel = Channels.newChannel(his);
        final ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
        while (inputChannel.read(buffer) != -1) {
            buffer.clear();
        }
        /*
        byte[] bytesBuffer = new byte[bufferSize] ;
        int r = his.read(bytesBuffer, 0, bufferSize) ;
        while (r != -1)
           r = his.read(bytesBuffer) ;
        */
        HashCode hc = his.hash();
        return (hc != null) ? (hc.toString()) : (null);
    } finally {
        if (his != null)
            his.close();
    }
}