public static void readFully(InputStream stream, int length, ByteBuffer bb) throws IOException {
    byte[] b = null;
    int offset = 0;
    if (bb.hasArray()) {
      b = bb.array();
      offset = bb.arrayOffset() + bb.position();
    } else {
      b = new byte[bb.remaining()];
    }
    int fullLen = length;
    while (length > 0) {
      int result = stream.read(b, offset, length);
      if (result < 0) {
        throw new EOFException("Reading " + fullLen + " bytes");
      }
      offset += result;
      length -= result;
    }
    if (!bb.hasArray()) {
      bb.put(b);
    } else {
      bb.position(bb.position() + fullLen);
    }
  }
Reads length bytes of data from the stream into the byte buffer.
@param stream Stream to read from.
@param length The number of bytes to read.
@param bb The buffer to read into; the data is written at current position and then the
          position is incremented by length.
@throws EOFException the length bytes cannot be read. The buffer position is not modified.
common/src/java/org/apache/hadoop/hive/common/FileUtils.java