protected final void ensureCapacity(final int index, final int length)
    {
        if (index < 0 || length < 0)
        {
            throw new IndexOutOfBoundsException("negative value: index=" + index + " length=" + length);
        }

        final long resultingPosition = index + (long)length;
        if (resultingPosition > capacity)
        {
            if (resultingPosition > MAX_ARRAY_LENGTH)
            {
                throw new IndexOutOfBoundsException(
                    "index=" + index + " length=" + length + " maxCapacity=" + MAX_ARRAY_LENGTH);
            }

            final int newCapacity = calculateExpansion(capacity, resultingPosition);
            byteArray = Arrays.copyOf(byteArray, newCapacity);
            capacity = newCapacity;
        }
    }
@Test
    void ensureCapacityIsANoOpIfExistingCapacityIsEnough()
    {
        final int index = 1;
        final int capacity = 5;
        final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer(capacity);

        buffer.ensureCapacity(index, capacity - index);

        assertEquals(capacity, buffer.capacity());
    }