Reads as much as possible from the channel into the buffer. - Java java.nio.channels

Java examples for java.nio.channels:FileChannel

Description

Reads as much as possible from the channel into the buffer.

Demo Code


//package com.java2s;
import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    /**/* w  w  w. j  a v a 2  s.co  m*/
     * Reads as much as possible from the channel into the buffer.
     * @param channel The channel.
     * @param buffer The buffer.
     * @param ptr The initial position in the channel.
     * @throws IOException if an I/O error occurs.
     * @throws EOFException if the end of the file was reached and the buffer
     * could not be completely populated.
     */
    public static void readFully(FileChannel channel, ByteBuffer buffer,
            long ptr) throws IOException {
        while (buffer.remaining() > 0) {
            long read = channel.read(buffer, ptr);
            if (read == -1) {
                throw new EOFException();
            } else {
                ptr += read;
            }
        }
    }
}

Related Tutorials