private int fillBuffer(byte[] buffer) throws IOException {
    int totalRead = 0;
    int currentRead;
    do {
      currentRead = inputStream.read(buffer, totalRead, buffer.length - totalRead);
      if (currentRead > 0) {
        totalRead += currentRead;
      }
    } while (totalRead < buffer.length && currentRead != -1);
    return totalRead;
  }
Fills the buffer from the {@link InputStream}. Waits untils the buffer is full or the {@link InputStream} is empty.
@param buffer the buffer to fill bytes to.
@return the number of bytes read from the {@link InputStream}.
@throws IOException if there were any problems reading the {@link InputStream}.
ambry-commons/src/main/java/com/github/ambry/commons/InputStreamReadableStreamChannel.java