code: public static int readFileChannelFully(FileChannel fileChannel, byte buf[], 
        int off, int len, long offset, boolean throwOnEof) throws IOException {
      ByteBuffer byteBuffer = ByteBuffer.wrap(buf, off, len);
      int toRead = len;
      int dataRead = 0;
      while (toRead > 0) {
        int ret = fileChannel.read(byteBuffer, offset);
        if (ret < 0) {
          if (throwOnEof) {
            throw new IOException( "Premeture EOF from inputStream");
          } else {
            return dataRead;
          }
        }
        toRead -= ret;
        offset += ret;
        dataRead += ret;
      }
      return dataRead;
    }
docs: Reads len bytes in a loop using the channel of the stream