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

public static void main(String[] args) throws IOException {

    FileInputStream fin = new FileInputStream(args[0]);
    GZIPInputStream gzin = new GZIPInputStream(fin);
    ReadableByteChannel in = Channels.newChannel(gzin);

    WritableByteChannel out = Channels.newChannel(System.out);
    ByteBuffer buffer = ByteBuffer.allocate(65536);
    while (in.read(buffer) != -1) {
        buffer.flip();//from  w  w  w  .j  a  v a 2 s. com
        out.write(buffer);
        buffer.clear();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ReadableByteChannel channel = new FileInputStream("infile").getChannel();

    ByteBuffer buf = ByteBuffer.allocateDirect(10);

    int numRead = 0;
    while (numRead >= 0) {
        buf.rewind();/*from w w w  .  j  av  a  2 s  . c o m*/

        numRead = channel.read(buf);

        buf.rewind();

        for (int i = 0; i < numRead; i++) {
            byte b = buf.get();
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ReadableByteChannel channel = new FileInputStream("infile").getChannel();

    ByteBuffer buf = ByteBuffer.allocateDirect(10);

    int numRead = 0;
    while (numRead >= 0) {
        buf.rewind();//  w w  w. j  av  a 2  s .co  m

        numRead = channel.read(buf);

        buf.rewind();

        // Read bytes from ByteBuffer; see also
        for (int i = 0; i < numRead; i++) {
            byte b = buf.get();
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ReadableByteChannel channel = new FileInputStream("infile.dat").getChannel();

    ByteBuffer buf = ByteBuffer.allocateDirect(10);

    int numRead = 0;
    while (numRead >= 0) {
        buf.rewind();//w  w w  . j a  v a2s. co m

        numRead = channel.read(buf);

        buf.rewind();

        for (int i = 0; i < numRead; i++) {
            byte b = buf.get();
        }
    }
}

From source file:com.eatnumber1.util.io.IOUtils.java

public static void read(@NotNull ReadableByteChannel channel, @NotNull ByteBuffer dst, int expected)
        throws IOException {
    if (channel.read(dst) != expected)
        throw new IOException("Did not read expected amount of data");
}

From source file:Main.java

public static void copy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocateDirect(CAPACITY);
    while (src.read(buffer) != -1) {
        buffer.flip();//from  w ww  .ja  v  a 2s.com
        dest.write(buffer);
        buffer.compact();
    }

    buffer.flip();

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

From source file:Main.java

public static void transfer(ReadableByteChannel in, WritableByteChannel out) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocate(4096);
    while (in.read(buffer) != -1) {
        buffer.flip();/*w  w w .java  2 s. co  m*/
        while (buffer.hasRemaining()) {
            out.write(buffer);
        }
        buffer.clear();
    }
}

From source file:Main.java

public static int readFully(final ReadableByteChannel channel, final ByteBuffer buf, final int length)
        throws IOException {
    int n, count = 0;
    while (-1 != (n = channel.read(buf))) {
        count += n;/*w  ww.j a va 2 s .c o m*/
        if (count == length) {
            break;
        }
    }
    if (n == -1) {
        throw new EOFException("End of file. No more boxes.");
    }
    return count;
}

From source file:Main.java

public static int readFully(ReadableByteChannel paramReadableByteChannel, ByteBuffer paramByteBuffer,
        int paramInt) throws IOException {
    int i = 0;/*from   w w w .ja v  a2  s. c o  m*/
    int j;
    do {
        j = paramReadableByteChannel.read(paramByteBuffer);
        if (-1 == j)
            break;
        i += j;
    } while (i != paramInt);
    if (j == -1)
        throw new EOFException("End of file. No more boxes.");
    return i;
}

From source file:Main.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();//from w w  w. j  ava  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 recycle()
        buffer.compact();
    }
    // EOF will leave buffer in fill state
    buffer.flip();
    // make sure the buffer is fully drained.
    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}