Java FileChannel Read readFully(FileChannel channel, ByteBuffer buffer, long ptr)

Here you can find the source of readFully(FileChannel channel, ByteBuffer buffer, long ptr)

Description

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

License

Open Source License

Parameter

Parameter Description
channel The channel.
buffer The buffer.
ptr The initial position in the channel.

Exception

Parameter Description
IOException if an I/O error occurs.
EOFException if the end of the file was reached and the buffercould not be completely populated.

Declaration

public static void readFully(FileChannel channel, ByteBuffer buffer, long ptr) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    /**/*from ww  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

  1. readFully(FileChannel channel, ByteBuffer buffer)
  2. readFully(FileChannel channel, ByteBuffer dst)
  3. readFully(FileChannel channel, long offset, ByteBuffer buf)
  4. readFully(FileChannel fileChannel, long filePosition, ByteBuffer buffer)