public static ByteBuffer getByteBufferFromInputStream(InputStream stream, int dataSize) throws IOException {
    if (dataSize < 0) {
      throw new IllegalArgumentException("Input size of stream is " + dataSize);
    }
    ByteBuffer output = ByteBuffer.allocate(dataSize);
    int read = 0;
    ReadableByteChannel readableByteChannel = Channels.newChannel(stream);
    while (read < dataSize) {
      int sizeRead = readableByteChannel.read(output);
      if (sizeRead == 0 || sizeRead == -1) {
        throw new IOException("Total size read " + read + " is less than the size to be read " + dataSize);
      }
      read += sizeRead;
    }
    output.flip();
    return output;
  }
A helper method to return {@link ByteBuffer} from given {@link InputStream} at the given size.
@param stream The {@link InputStream} to read {@link ByteBuffer} out.
@param dataSize The size of {@link ByteBuffer}.
@return The {@link ByteBuffer}
@throws IOException Unexpected IO errors.
ambry-utils/src/main/java/com/github/ambry/utils/Utils.java