public static void readFullyWithHeapBuffer(InputStream in, ByteBuff out, int length)
    throws IOException {
    if (length < 0) {
      throw new IllegalArgumentException("Length must not be negative: " + length);
    }
    int heapBytesRead = 0;
    int remain = length, count;
    byte[] buffer = new byte[1024];
    try {
      while (remain > 0) {
        count = in.read(buffer, 0, Math.min(remain, buffer.length));
        if (count < 0) {
          throw new IOException(
            "Premature EOF from inputStream, but still need " + remain + " bytes");
        }
        out.put(buffer, 0, count);
        remain -= count;
        heapBytesRead += count;
      }
    } finally {
      final Span span = Span.current();
      span.addEvent("BlockIOUtils.readFullyWithHeapBuffer",
        getHeapBytesReadAttributes(span, heapBytesRead));
    }
  }
Copying bytes from InputStream to {@link ByteBuff} by using an temporary heap byte[] (default
size is 1024 now).
@param in     the InputStream to read
@param out    the destination {@link ByteBuff}
@param length to read
@throws IOException if any io error encountered.
hbase-common/src/main/java/org/apache/hadoop/hbase/io/util/BlockIOUtils.java