public int tryClaim(final int msgTypeId, final int length)
    {
        checkTypeId(msgTypeId);
        checkMsgLength(length);

        final AtomicBuffer buffer = this.buffer;
        final int recordLength = length + HEADER_LENGTH;
        final int recordIndex = claimCapacity(buffer, recordLength);

        if (INSUFFICIENT_CAPACITY == recordIndex)
        {
            return recordIndex;
        }

        buffer.putIntOrdered(lengthOffset(recordIndex), -recordLength);
        MemoryAccess.releaseFence();
        buffer.putInt(typeOffset(recordIndex), msgTypeId);

        return encodedMsgOffset(recordIndex);
    }
@Test
    void tryClaimReturnsOffsetAtWhichMessageBodyCanBeWritten()
    {
        final int msgTypeId = MSG_TYPE_ID;
        final int length = 333;
        final int recordLength = HEADER_LENGTH + length;
        final int alignedRecordLength = align(recordLength, ALIGNMENT);

        final int index = ringBuffer.tryClaim(msgTypeId, length);

        assertEquals(HEADER_LENGTH, index);

        final InOrder inOrder = inOrder(buffer);
        inOrder.verify(buffer).putLongOrdered(TAIL_COUNTER_INDEX, alignedRecordLength);
        inOrder.verify(buffer).putLong(alignedRecordLength, 0L);
        inOrder.verify(buffer).putIntOrdered(lengthOffset(0), -recordLength);
        inOrder.verify(buffer).putInt(typeOffset(0), msgTypeId);
    }