void growCatalog(final long maxCatalogCapacity, final int frameLength)
    {
        final long oldCapacity = capacity;
        final long recordingOffset = nextRecordingDescriptorOffset;
        final long targetCapacity = recordingOffset + frameLength;
        if (targetCapacity > maxCatalogCapacity)
        {
            if (maxCatalogCapacity == oldCapacity)
            {
                throw new ArchiveException("catalog is full, max capacity reached: " + maxCatalogCapacity);
            }
            else
            {
                throw new ArchiveException(
                    "recording is too big: total recording length is " + frameLength + " bytes," +
                    " available space is " + (maxCatalogCapacity - recordingOffset) + " bytes");
            }
        }

        long newCapacity = oldCapacity;
        while (newCapacity < targetCapacity)
        {
            newCapacity = min(newCapacity + (newCapacity >> 1), maxCatalogCapacity);
        }

        final MappedByteBuffer mappedByteBuffer;
        try
        {
            unmapAndCloseChannel();
            catalogChannel = FileChannel.open(catalogFile.toPath(), READ, WRITE, SPARSE);
            mappedByteBuffer = catalogChannel.map(READ_WRITE, 0, newCapacity);
        }
        catch (final Exception ex)
        {
            close();
            LangUtil.rethrowUnchecked(ex);
            return;
        }

        capacity = newCapacity;
        initBuffers(mappedByteBuffer);

        final UnsafeBuffer catalogHeaderBuffer = new UnsafeBuffer(catalogByteBuffer);
        catalogHeaderDecoder.wrap(
            catalogHeaderBuffer, 0, CatalogHeaderDecoder.BLOCK_LENGTH, CatalogHeaderDecoder.SCHEMA_VERSION);
        catalogHeaderEncoder.wrap(catalogHeaderBuffer, 0);

        catalogResized(oldCapacity, newCapacity);
    }
@Test
    void growCatalogThrowsArchiveExceptionIfCatalogIsFull()
    {
        try (Catalog catalog = new Catalog(archiveDir, null, 0, CAPACITY, clock, null, segmentFileBuffer))
        {
            final ArchiveException exception = assertThrows(
                ArchiveException.class, () -> catalog.growCatalog(CAPACITY, (int)(CAPACITY + 1)));
            assertEquals("ERROR - catalog is full, max capacity reached: " + CAPACITY, exception.getMessage());
        }
    }