Java ReadableByteChannel Read readFully(ReadableByteChannel channel, ByteBuffer b)

Here you can find the source of readFully(ReadableByteChannel channel, ByteBuffer b)

Description

Same as the normal channel.read(b), but tries to ensure that the entire len number of bytes is read.

License

Apache License

Declaration

public static int readFully(ReadableByteChannel channel, ByteBuffer b) throws IOException 

Method Source Code

//package com.java2s;
/* NOTICE: This file has been changed by Plutext Pty Ltd for use in docx4j.
 * The package name has been changed; there may also be other changes.
 * //from   w ww.  ja va  2  s .c om
 * This notice is included to meet the condition in clause 4(b) of the License. 
 */

import java.io.IOException;
import java.io.InputStream;

import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;

public class Main {
    /**
     * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt>
     */
    public static int readFully(InputStream in, byte[] b) throws IOException {
        return readFully(in, b, 0, b.length);
    }

    /**
     * Same as the normal <tt>in.read(b, off, len)</tt>, but tries to ensure
     * that the entire len number of bytes is read.
     * <p>
     * If the end of file is reached before any bytes are read, returns -1. If
     * the end of the file is reached after some bytes are read, returns the
     * number of bytes read. If the end of the file isn't reached before len
     * bytes have been read, will return len bytes.
     */
    public static int readFully(InputStream in, byte[] b, int off, int len) throws IOException {
        int total = 0;
        while (true) {
            int got = in.read(b, off + total, len - total);
            if (got < 0) {
                return (total == 0) ? -1 : total;
            }
            total += got;
            if (total == len) {
                return total;
            }
        }
    }

    /**
     * Same as the normal <tt>channel.read(b)</tt>, but tries to ensure
     * that the entire len number of bytes is read.
     * <p>
     * If the end of file is reached before any bytes are read, returns -1. If
     * the end of the file is reached after some bytes are read, returns the
     * number of bytes read. If the end of the file isn't reached before len
     * bytes have been read, will return len bytes.
     */
    public static int readFully(ReadableByteChannel channel, ByteBuffer b) throws IOException {
        int total = 0;
        while (true) {
            int got = channel.read(b);
            if (got < 0) {
                return (total == 0) ? -1 : total;
            }
            total += got;
            if (total == b.capacity() || b.position() == b.capacity()) {
                return total;
            }
        }
    }
}

Related

  1. readFromAnerisChannel(ReadableByteChannel chan, ByteBuffer buffer)
  2. readFromChannel(ReadableByteChannel channel, ByteBuffer buffer)
  3. readFull(ReadableByteChannel ch, ByteBuffer dst)
  4. readFully(final ReadableByteChannel channel, final ByteBuffer buf)
  5. readFully(final ReadableByteChannel channel, final ByteBuffer buf)
  6. readFully(ReadableByteChannel channel, ByteBuffer buffer)
  7. readLengthAndString(ReadableByteChannel channel, ByteBuffer buffer)
  8. readMap(ReadableByteChannel channel, ByteBuffer buffer)
  9. readObjFromChannel(ReadableByteChannel chan, ByteBuffer buffer, AtomicBoolean endPointCrashed)