public static ByteBuffer ensureCapacity(ByteBuffer existingBuffer, int newLength) {
    if (newLength > existingBuffer.capacity()) {
      ByteBuffer newBuffer =
          existingBuffer.isDirect() ? ByteBuffer.allocateDirect(newLength) : ByteBuffer.allocate(newLength);
      existingBuffer.flip();
      newBuffer.put(existingBuffer);
      return newBuffer;
    }
    return existingBuffer;
  }
Make sure that the ByteBuffer capacity is equal to or greater than the expected length.
If not, create a new ByteBuffer of expected length and copy contents from previous ByteBuffer to the new one
@param existingBuffer ByteBuffer capacity to check
@param newLength new length for the ByteBuffer.
returns ByteBuffer with a minimum capacity of new length
ambry-utils/src/main/java/com/github/ambry/utils/Utils.java