public static ByteBuffer readFully(FileChannel channel,long fileOffset,int bytesToRead) throws IOException {
  ByteBuffer buffer=ByteBuffer.allocate(bytesToRead);
  int totalBytesRead=0;
  while (totalBytesRead < bytesToRead) {
    int bytesRead=channel.read(buffer,fileOffset + totalBytesRead);
    if (bytesRead < 0) {
      throw new EOFException("Expected to read " + bytesToRead + " bytes; only read "+ totalBytesRead+ " before EOF");
    }
    totalBytesRead+=bytesRead;
  }
  buffer.flip();
  return buffer;
}

public static ByteBuffer readFully ( FileChannel channel , long fileOffset , int bytesToRead ) throws IOException { ByteBuffer buffer = ByteBuffer . allocate ( bytesToRead ) ; int totalBytesRead = 0 ; while ( totalBytesRead < bytesToRead ) { int bytesRead = channel . read ( buffer , fileOffset + totalBytesRead ) ; if ( bytesRead < 0 ) { throw new EOFException ( "Expected to read " + bytesToRead + " bytes; only read " + totalBytesRead + " before EOF" ) ; } totalBytesRead += bytesRead ; } buffer . flip ( ) ; return buffer ; }
reads a sequence of bytes from a channel into a newly allocated buffer , starting at the given file position . this method does not modify the channel position or close the channel .
