private void expandBuffer(int remainingRequired) {
        int expandSize = Math.max((int) (buffer.limit() * REALLOCATION_FACTOR), buffer.position() + remainingRequired);
        ByteBuffer temp = ByteBuffer.allocate(expandSize);
        int limit = limit();
        buffer.flip();
        temp.put(buffer);
        buffer.limit(limit);
        // reset the old buffer's position so that the partial data in the new buffer cannot be mistakenly consumed
        // we should ideally only do this for the original buffer, but the additional complexity doesn't seem worth it
        buffer.position(initialPosition);
        buffer = temp;
    }
Ensure there is enough space to write some number of bytes, expanding the underlying buffer if necessary.
This can be used to avoid incremental expansions through calls to {@link #write(int)} when you know how
many total bytes are needed.

@param remainingBytesRequired The number of bytes required
clients/src/main/java/org/apache/kafka/common/utils/ByteBufferOutputStream.java