Java Code Examples for org.agrona.BitUtil#align()

The following examples show how to use org.agrona.BitUtil#align() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TermBlockScannerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadBlockOfOneMessageThenPadding()
{
    final int offset = 0;
    final int limit = termBuffer.capacity();
    final int messageLength = 50;
    final int alignedMessageLength = BitUtil.align(messageLength, FRAME_ALIGNMENT);

    when(termBuffer.getIntVolatile(lengthOffset(offset))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(offset))).thenReturn((short)HDR_TYPE_DATA);
    when(termBuffer.getIntVolatile(lengthOffset(alignedMessageLength))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(alignedMessageLength))).thenReturn((short)HDR_TYPE_PAD);

    final int firstOffset = TermBlockScanner.scan(termBuffer, offset, limit);
    assertEquals(alignedMessageLength, firstOffset);

    final int secondOffset = TermBlockScanner.scan(termBuffer, firstOffset, limit);
    assertEquals(alignedMessageLength * 2, secondOffset);
}
 
Example 2
Source File: TermBlockScannerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadBlockOfTwoMessagesBecauseOfLimit()
{
    final int offset = 0;
    final int messageLength = 50;
    final int alignedMessageLength = BitUtil.align(messageLength, FRAME_ALIGNMENT);
    final int limit = (alignedMessageLength * 2) + 1;

    when(termBuffer.getIntVolatile(lengthOffset(offset))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(offset))).thenReturn((short)HDR_TYPE_DATA);
    when(termBuffer.getIntVolatile(lengthOffset(alignedMessageLength))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(alignedMessageLength))).thenReturn((short)HDR_TYPE_DATA);
    when(termBuffer.getIntVolatile(lengthOffset(alignedMessageLength * 2))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(alignedMessageLength * 2))).thenReturn((short)HDR_TYPE_DATA);

    final int newOffset = TermBlockScanner.scan(termBuffer, offset, limit);
    assertEquals(alignedMessageLength * 2, newOffset);
}
 
Example 3
Source File: TermBlockScannerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadBlockOfThreeMessagesThatFillBuffer()
{
    final int offset = 0;
    final int limit = termBuffer.capacity();
    final int messageLength = 50;
    final int alignedMessageLength = BitUtil.align(messageLength, FRAME_ALIGNMENT);
    final int thirdMessageLength = limit - (alignedMessageLength * 2);

    when(termBuffer.getIntVolatile(lengthOffset(offset))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(offset))).thenReturn((short)HDR_TYPE_DATA);
    when(termBuffer.getIntVolatile(lengthOffset(alignedMessageLength))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(alignedMessageLength))).thenReturn((short)HDR_TYPE_DATA);
    when(termBuffer.getIntVolatile(lengthOffset(alignedMessageLength * 2))).thenReturn(thirdMessageLength);
    when(termBuffer.getShort(typeOffset(alignedMessageLength * 2))).thenReturn((short)HDR_TYPE_DATA);

    final int newOffset = TermBlockScanner.scan(termBuffer, offset, limit);
    assertEquals(limit, newOffset);
}
 
Example 4
Source File: TermBlockScannerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReadBlockOfTwoMessages()
{
    final int offset = 0;
    final int limit = termBuffer.capacity();
    final int messageLength = 50;
    final int alignedMessageLength = BitUtil.align(messageLength, FRAME_ALIGNMENT);

    when(termBuffer.getIntVolatile(lengthOffset(offset))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(offset))).thenReturn((short)HDR_TYPE_DATA);
    when(termBuffer.getIntVolatile(lengthOffset(alignedMessageLength))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(alignedMessageLength))).thenReturn((short)HDR_TYPE_DATA);

    final int newOffset = TermBlockScanner.scan(termBuffer, offset, limit);
    assertEquals(alignedMessageLength * 2, newOffset);
}
 
Example 5
Source File: TermRebuilderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFillGapButNotMoveTailOrHwm()
{
    final int frameLength = 50;
    final int alignedFrameLength = BitUtil.align(frameLength, FRAME_ALIGNMENT);
    final int srcOffset = 0;
    final UnsafeBuffer packet = new UnsafeBuffer(ByteBuffer.allocateDirect(alignedFrameLength));
    final int termOffset = alignedFrameLength * 2;

    TermRebuilder.insert(termBuffer, termOffset, packet, alignedFrameLength);

    verify(termBuffer).putBytes(
        (alignedFrameLength * 2) + HEADER_LENGTH,
        packet,
        srcOffset + HEADER_LENGTH,
        alignedFrameLength - HEADER_LENGTH);
}
 
Example 6
Source File: TermRebuilderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFillAfterAGap()
{
    final int frameLength = 50;
    final int alignedFrameLength = BitUtil.align(frameLength, FRAME_ALIGNMENT);
    final int srcOffset = 0;
    final UnsafeBuffer packet = new UnsafeBuffer(ByteBuffer.allocateDirect(alignedFrameLength));
    final int termOffset = alignedFrameLength * 2;

    TermRebuilder.insert(termBuffer, termOffset, packet, alignedFrameLength);

    verify(termBuffer).putBytes(
        (alignedFrameLength * 2) + HEADER_LENGTH,
        packet,
        srcOffset + HEADER_LENGTH,
        alignedFrameLength - HEADER_LENGTH);
}
 
Example 7
Source File: TermRebuilderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInsertLastFrameIntoBuffer()
{
    final int frameLength = BitUtil.align(256, FRAME_ALIGNMENT);
    final int srcOffset = 0;
    final int tail = TERM_BUFFER_CAPACITY - frameLength;
    final int termOffset = tail;
    final UnsafeBuffer packet = new UnsafeBuffer(ByteBuffer.allocateDirect(frameLength));
    packet.putShort(typeOffset(srcOffset), (short)PADDING_FRAME_TYPE, LITTLE_ENDIAN);
    packet.putInt(srcOffset, frameLength, LITTLE_ENDIAN);

    TermRebuilder.insert(termBuffer, termOffset, packet, frameLength);

    verify(termBuffer).putBytes(
        tail + HEADER_LENGTH, packet, srcOffset + HEADER_LENGTH, frameLength - HEADER_LENGTH);
}
 
Example 8
Source File: TermBlockScannerTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReadOneMessageOnLimit()
{
    final int offset = 0;
    final int messageLength = 50;
    final int alignedMessageLength = BitUtil.align(messageLength, FRAME_ALIGNMENT);

    when(termBuffer.getIntVolatile(lengthOffset(offset))).thenReturn(messageLength);
    when(termBuffer.getShort(typeOffset(offset))).thenReturn((short)HDR_TYPE_DATA);

    final int newOffset = TermBlockScanner.scan(termBuffer, offset, alignedMessageLength);
    assertEquals(alignedMessageLength, newOffset);
}
 
Example 9
Source File: TermRebuilderTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFillSingleGap()
{
    final int frameLength = 50;
    final int alignedFrameLength = BitUtil.align(frameLength, FRAME_ALIGNMENT);
    final int srcOffset = 0;
    final int tail = alignedFrameLength;
    final int termOffset = tail;
    final UnsafeBuffer packet = new UnsafeBuffer(ByteBuffer.allocateDirect(alignedFrameLength));

    TermRebuilder.insert(termBuffer, termOffset, packet, alignedFrameLength);

    verify(termBuffer).putBytes(
        tail + HEADER_LENGTH, packet, srcOffset + HEADER_LENGTH, alignedFrameLength - HEADER_LENGTH);
}
 
Example 10
Source File: BroadcastTransmitter.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Transmit a message to {@link BroadcastReceiver}s via the broadcast buffer.
 *
 * @param msgTypeId type of the message to be transmitted.
 * @param srcBuffer containing the encoded message to be transmitted.
 * @param srcIndex  srcIndex in the source buffer at which the encoded message begins.
 * @param length    in bytes of the encoded message.
 * @throws IllegalArgumentException of the msgTypeId is not valid,
 * or if the message length is greater than {@link #maxMsgLength()}.
 */
public void transmit(final int msgTypeId, final DirectBuffer srcBuffer, final int srcIndex, final int length)
{
    checkTypeId(msgTypeId);
    checkMessageLength(length);

    final AtomicBuffer buffer = this.buffer;
    long currentTail = buffer.getLong(tailCounterIndex);
    int recordOffset = (int)currentTail & (capacity - 1);
    final int recordLength = HEADER_LENGTH + length;
    final int recordLengthAligned = BitUtil.align(recordLength, RECORD_ALIGNMENT);
    final long newTail = currentTail + recordLengthAligned;

    final int toEndOfBuffer = capacity - recordOffset;
    if (toEndOfBuffer < recordLengthAligned)
    {
        signalTailIntent(buffer, newTail + toEndOfBuffer);
        insertPaddingRecord(buffer, recordOffset, toEndOfBuffer);

        currentTail += toEndOfBuffer;
        recordOffset = 0;
    }
    else
    {
        signalTailIntent(buffer, newTail);
    }

    buffer.putInt(lengthOffset(recordOffset), recordLength);
    buffer.putInt(typeOffset(recordOffset), msgTypeId);

    buffer.putBytes(msgOffset(recordOffset), srcBuffer, srcIndex, length);

    buffer.putLong(latestCounterIndex, currentTail);
    buffer.putLongOrdered(tailCounterIndex, currentTail + recordLengthAligned);
}
 
Example 11
Source File: PubAndSubTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("channels")
@Timeout(10)
public void shouldReceivePublishedMessageViaPollFile(final String channel)
{
    launch(channel);

    publishMessage();

    while (true)
    {
        final long bytes = subscription.rawPoll(rawBlockHandler, Integer.MAX_VALUE);
        if (bytes > 0)
        {
            break;
        }

        Tests.yield();
    }

    final long expectedOffset = 0L;
    final int expectedLength = BitUtil.align(HEADER_LENGTH + SIZE_OF_INT, FRAME_ALIGNMENT);

    final ArgumentCaptor<FileChannel> channelArgumentCaptor = ArgumentCaptor.forClass(FileChannel.class);
    verify(rawBlockHandler).onBlock(
        channelArgumentCaptor.capture(),
        eq(expectedOffset),
        any(UnsafeBuffer.class),
        eq((int)expectedOffset),
        eq(expectedLength),
        anyInt(),
        anyInt());

    assertTrue(channelArgumentCaptor.getValue().isOpen(), "File Channel is closed");
}
 
Example 12
Source File: RecordingPos.java    From aeron with Apache License 2.0 5 votes vote down vote up
public static Counter allocate(
    final Aeron aeron,
    final UnsafeBuffer tempBuffer,
    final long recordingId,
    final int sessionId,
    final int streamId,
    final String strippedChannel,
    final String sourceIdentity)
{
    tempBuffer.putLong(RECORDING_ID_OFFSET, recordingId);
    tempBuffer.putInt(SESSION_ID_OFFSET, sessionId);

    final int sourceIdentityLength = Math.min(sourceIdentity.length(), MAX_KEY_LENGTH - SOURCE_IDENTITY_OFFSET);
    tempBuffer.putStringAscii(SOURCE_IDENTITY_LENGTH_OFFSET, sourceIdentity);
    final int keyLength = SOURCE_IDENTITY_OFFSET + sourceIdentityLength;

    final int labelOffset = BitUtil.align(keyLength, SIZE_OF_INT);
    int labelLength = 0;
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset, NAME + ": ");
    labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, recordingId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
    labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, sessionId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
    labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, streamId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
    labelLength += tempBuffer.putStringWithoutLengthAscii(
        labelOffset + labelLength, strippedChannel, 0, MAX_LABEL_LENGTH - labelLength);

    return aeron.addCounter(
        RECORDING_POSITION_TYPE_ID, tempBuffer, 0, keyLength, tempBuffer, labelOffset, labelLength);
}
 
Example 13
Source File: LossReport.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new entry for recording loss on a given stream.
 * <p>
 * If not space is remaining in the error report then null is returned.
 *
 * @param initialBytesLost on the stream.
 * @param timestampMs      at which the first loss was observed.
 * @param sessionId        for the stream.
 * @param streamId         for the stream.
 * @param channel          for the stream.
 * @param source           of the stream.
 * @return a new record or null if the error log has insufficient space.
 */
public ReportEntry createEntry(
    final long initialBytesLost,
    final long timestampMs,
    final int sessionId,
    final int streamId,
    final String channel,
    final String source)
{
    ReportEntry reportEntry = null;

    final int requiredCapacity =
        CHANNEL_OFFSET +
        BitUtil.align(SIZE_OF_INT + channel.length(), SIZE_OF_INT) +
        SIZE_OF_INT + source.length();

    if (requiredCapacity <= (buffer.capacity() - nextRecordOffset))
    {
        final int offset = nextRecordOffset;

        buffer.putLong(offset + TOTAL_BYTES_LOST_OFFSET, initialBytesLost);
        buffer.putLong(offset + FIRST_OBSERVATION_OFFSET, timestampMs);
        buffer.putLong(offset + LAST_OBSERVATION_OFFSET, timestampMs);
        buffer.putInt(offset + SESSION_ID_OFFSET, sessionId);
        buffer.putInt(offset + STREAM_ID_OFFSET, streamId);

        final int encodedChannelLength = buffer.putStringAscii(offset + CHANNEL_OFFSET, channel);

        buffer.putStringAscii(
            offset + CHANNEL_OFFSET + BitUtil.align(encodedChannelLength, SIZE_OF_INT), source);

        buffer.putLongOrdered(offset + OBSERVATION_COUNT_OFFSET, 1);

        reportEntry = new ReportEntry(buffer, offset);
        nextRecordOffset += BitUtil.align(requiredCapacity, ENTRY_ALIGNMENT);
    }

    return reportEntry;
}
 
Example 14
Source File: ImageBuffersReadyFlyweight.java    From aeron with Apache License 2.0 4 votes vote down vote up
private int sourceIdentityOffset()
{
    final int alignedLength = BitUtil.align(buffer.getInt(offset + LOG_FILE_NAME_OFFSET), SIZE_OF_INT);

    return LOG_FILE_NAME_OFFSET + SIZE_OF_INT + alignedLength;
}
 
Example 15
Source File: StreamCounter.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static int allocateCounterId(
    final MutableDirectBuffer tempBuffer,
    final String name,
    final int typeId,
    final CountersManager countersManager,
    final long registrationId,
    final int sessionId,
    final int streamId,
    final String channel)
{
    tempBuffer.putLong(REGISTRATION_ID_OFFSET, registrationId);
    tempBuffer.putInt(SESSION_ID_OFFSET, sessionId);
    tempBuffer.putInt(STREAM_ID_OFFSET, streamId);

    final int channelLength = tempBuffer.putStringWithoutLengthAscii(
        CHANNEL_OFFSET + SIZE_OF_INT, channel, 0, MAX_CHANNEL_LENGTH);
    tempBuffer.putInt(CHANNEL_OFFSET, channelLength);
    final int keyLength = CHANNEL_OFFSET + SIZE_OF_INT + channelLength;

    final int labelOffset = BitUtil.align(keyLength, SIZE_OF_INT);
    int labelLength = 0;
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, name);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, ": ");
    labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, registrationId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
    labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, sessionId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
    labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, streamId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
    labelLength += tempBuffer.putStringWithoutLengthAscii(
        labelOffset + labelLength, channel, 0, MAX_LABEL_LENGTH - labelLength);

    return countersManager.allocate(
        typeId,
        tempBuffer,
        0,
        keyLength,
        tempBuffer,
        labelOffset,
        labelLength);
}
 
Example 16
Source File: PublishFromArbitraryPositionTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
@Timeout(10)
public void shouldPublishFromArbitraryJoinPosition() throws InterruptedException
{
    final Random rnd = new Random();
    rnd.setSeed(seed);

    final int termLength = 1 << (16 + rnd.nextInt(10)); // 64k to 64M
    final int mtu = 1 << (10 + rnd.nextInt(3)); // 1024 to 8096
    final int initialTermId = rnd.nextInt(1234);
    final int termOffset = BitUtil.align(rnd.nextInt(termLength), FrameDescriptor.FRAME_ALIGNMENT);
    final int termId = initialTermId + rnd.nextInt(1000);
    final String channelUri = new ChannelUriStringBuilder()
        .endpoint("localhost:24325")
        .termLength(termLength)
        .initialTermId(initialTermId)
        .termId(termId)
        .termOffset(termOffset)
        .mtu(mtu)
        .media("udp")
        .build();

    final int expectedNumberOfFragments = 10 + rnd.nextInt(10000);

    try (Subscription subscription = aeron.addSubscription(channelUri, STREAM_ID);
        ExclusivePublication publication = aeron.addExclusivePublication(channelUri, STREAM_ID))
    {
        Tests.awaitConnected(publication);

        final Thread t = new Thread(
            () ->
            {
                int totalFragmentsRead = 0;
                do
                {
                    int fragmentsRead = subscription.poll(mockFragmentHandler, FRAGMENT_COUNT_LIMIT);
                    while (0 == fragmentsRead)
                    {
                        Thread.yield();
                        fragmentsRead = subscription.poll(mockFragmentHandler, FRAGMENT_COUNT_LIMIT);
                    }

                    totalFragmentsRead += fragmentsRead;
                }
                while (totalFragmentsRead < expectedNumberOfFragments);

                assertEquals(expectedNumberOfFragments, totalFragmentsRead);
            });

        t.setDaemon(true);
        t.setName("image-consumer");
        t.start();

        for (int i = 0; i < expectedNumberOfFragments; i++)
        {
            publishMessage(srcBuffer, publication, rnd);
        }

        t.join();
    }
}
 
Example 17
Source File: SelectorAndTransportTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
@Timeout(10)
public void shouldSendMultipleDataFramesPerDatagramUnicastFromSourceToReceiver()
{
    final MutableInteger dataHeadersReceived = new MutableInteger(0);

    doAnswer(
        (invocation) ->
        {
            dataHeadersReceived.value++;
            return null;
        })
        .when(mockDispatcher).onDataPacket(
        any(ReceiveChannelEndpoint.class),
        any(DataHeaderFlyweight.class),
        any(UnsafeBuffer.class),
        anyInt(),
        any(InetSocketAddress.class),
        anyInt());

    receiveChannelEndpoint = new ReceiveChannelEndpoint(
        RCV_DST, mockDispatcher, mockReceiveStatusIndicator, context);
    sendChannelEndpoint = new SendChannelEndpoint(SRC_DST, mockSendStatusIndicator, context);

    receiveChannelEndpoint.openDatagramChannel(mockReceiveStatusIndicator);
    receiveChannelEndpoint.registerForRead(dataTransportPoller);
    sendChannelEndpoint.openDatagramChannel(mockSendStatusIndicator);
    sendChannelEndpoint.registerForRead(controlTransportPoller);

    encodeDataHeader.wrap(buffer);
    encodeDataHeader
        .version(HeaderFlyweight.CURRENT_VERSION)
        .flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS)
        .headerType(HeaderFlyweight.HDR_TYPE_DATA)
        .frameLength(FRAME_LENGTH);
    encodeDataHeader
        .sessionId(SESSION_ID)
        .streamId(STREAM_ID)
        .termId(TERM_ID);

    final int alignedFrameLength = BitUtil.align(FRAME_LENGTH, FrameDescriptor.FRAME_ALIGNMENT);
    encodeDataHeader.wrap(buffer, alignedFrameLength, buffer.capacity() - alignedFrameLength);
    encodeDataHeader
        .version(HeaderFlyweight.CURRENT_VERSION)
        .flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS)
        .headerType(HeaderFlyweight.HDR_TYPE_DATA)
        .frameLength(24);
    encodeDataHeader
        .sessionId(SESSION_ID)
        .streamId(STREAM_ID)
        .termId(TERM_ID);

    byteBuffer.position(0).limit(2 * alignedFrameLength);

    processLoop(dataTransportPoller, 5);
    sendChannelEndpoint.send(byteBuffer);
    while (dataHeadersReceived.get() < 1)
    {
        processLoop(dataTransportPoller, 1);
    }

    assertEquals(1, dataHeadersReceived.get());
}
 
Example 18
Source File: RecordingReader.java    From aeron with Apache License 2.0 4 votes vote down vote up
int poll(final SimpleFragmentHandler fragmentHandler, final int fragmentLimit)
{
    int fragments = 0;

    while (replayPosition < replayLimit && fragments < fragmentLimit)
    {
        if (termOffset == termLength)
        {
            nextTerm();
        }

        final int frameOffset = termOffset;
        final UnsafeBuffer termBuffer = this.termBuffer;
        final int frameLength = FrameDescriptor.frameLength(termBuffer, frameOffset);
        if (frameLength <= 0)
        {
            isDone = true;
            closeRecordingSegment();
            break;
        }

        final int frameType = FrameDescriptor.frameType(termBuffer, frameOffset);
        final byte flags = FrameDescriptor.frameFlags(termBuffer, frameOffset);
        final long reservedValue = termBuffer.getLong(frameOffset + RESERVED_VALUE_OFFSET, LITTLE_ENDIAN);

        final int alignedLength = BitUtil.align(frameLength, FRAME_ALIGNMENT);
        final int dataOffset = frameOffset + DataHeaderFlyweight.HEADER_LENGTH;
        final int dataLength = frameLength - DataHeaderFlyweight.HEADER_LENGTH;

        fragmentHandler.onFragment(termBuffer, dataOffset, dataLength, frameType, flags, reservedValue);

        replayPosition += alignedLength;
        termOffset += alignedLength;
        fragments++;

        if (replayPosition >= replayLimit)
        {
            isDone = true;
            closeRecordingSegment();
            break;
        }
    }

    return fragments;
}
 
Example 19
Source File: ArchiveDescriptor.java    From artio with Apache License 2.0 4 votes vote down vote up
public static int alignTerm(final int frameLength)
{
    return BitUtil.align(frameLength, FRAME_ALIGNMENT);
}
 
Example 20
Source File: LossReportReader.java    From aeron with Apache License 2.0 4 votes vote down vote up
/**
 * Read a {@link LossReport} contained in the buffer. This can be done concurrently.
 *
 * @param buffer        containing the loss report.
 * @param entryConsumer to be called to accept each entry in the report.
 * @return the number of entries read.
 */
public static int read(final AtomicBuffer buffer, final EntryConsumer entryConsumer)
{
    final int capacity = buffer.capacity();

    int recordsRead = 0;
    int offset = 0;

    while (offset < capacity)
    {
        final long observationCount = buffer.getLongVolatile(offset + OBSERVATION_COUNT_OFFSET);
        if (observationCount <= 0)
        {
            break;
        }

        ++recordsRead;

        final String channel = buffer.getStringAscii(offset + CHANNEL_OFFSET);
        final String source = buffer.getStringAscii(
            offset + CHANNEL_OFFSET + BitUtil.align(SIZE_OF_INT + channel.length(), SIZE_OF_INT));

        entryConsumer.accept(
            observationCount,
            buffer.getLong(offset + TOTAL_BYTES_LOST_OFFSET),
            buffer.getLong(offset + FIRST_OBSERVATION_OFFSET),
            buffer.getLong(offset + LAST_OBSERVATION_OFFSET),
            buffer.getInt(offset + SESSION_ID_OFFSET),
            buffer.getInt(offset + STREAM_ID_OFFSET),
            channel,
            source);

        final int recordLength =
            CHANNEL_OFFSET +
            BitUtil.align(SIZE_OF_INT + channel.length(), SIZE_OF_INT) +
            SIZE_OF_INT + source.length();
        offset += BitUtil.align(recordLength, ENTRY_ALIGNMENT);
    }

    return recordsRead;
}