Java Code Examples for org.agrona.concurrent.UnsafeBuffer#putShort()

The following examples show how to use org.agrona.concurrent.UnsafeBuffer#putShort() . 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: DataHeaderFlyweight.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Return an initialised default Data Frame Header.
 *
 * @param sessionId for the header
 * @param streamId  for the header
 * @param termId    for the header
 * @return byte array containing the header
 */
public static UnsafeBuffer createDefaultHeader(final int sessionId, final int streamId, final int termId)
{
    final UnsafeBuffer buffer = new UnsafeBuffer(
        BufferUtil.allocateDirectAligned(HEADER_LENGTH, CACHE_LINE_LENGTH));

    buffer.putByte(VERSION_FIELD_OFFSET, CURRENT_VERSION);
    buffer.putByte(FLAGS_FIELD_OFFSET, (byte)BEGIN_AND_END_FLAGS);
    buffer.putShort(TYPE_FIELD_OFFSET, (short)HDR_TYPE_DATA, LITTLE_ENDIAN);
    buffer.putInt(SESSION_ID_FIELD_OFFSET, sessionId, LITTLE_ENDIAN);
    buffer.putInt(STREAM_ID_FIELD_OFFSET, streamId, LITTLE_ENDIAN);
    buffer.putInt(TERM_ID_FIELD_OFFSET, termId, LITTLE_ENDIAN);
    buffer.putLong(RESERVED_VALUE_OFFSET, DEFAULT_RESERVE_VALUE);

    return buffer;
}
 
Example 2
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 3
Source File: MetaDataTest.java    From artio with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10_000L)
public void shouldUpdateWrittenSessionMetaDataTooBigForOldSlot()
{
    final UnsafeBuffer writeBuffer = new UnsafeBuffer(new byte[SIZE_OF_INT]);

    writeBuffer.putInt(0, META_DATA_WRONG_VALUE);
    writeMetaData(writeBuffer);

    final UnsafeBuffer bigWriteBuffer = new UnsafeBuffer(new byte[SIZE_OF_LONG]);
    bigWriteBuffer.putLong(0, META_DATA_VALUE);
    writeMetaData(bigWriteBuffer);

    UnsafeBuffer readBuffer = readSuccessfulMetaData(bigWriteBuffer);
    assertEquals(META_DATA_VALUE, readBuffer.getInt(0));

    final UnsafeBuffer updateBuffer = updateBuffer();

    final int bigUpdateOffset = SIZE_OF_LONG;
    final Reply<MetaDataStatus> reply = writeMetaData(updateBuffer, META_DATA_SESSION_ID, bigUpdateOffset);
    assertEquals(MetaDataStatus.OK, reply.resultIfPresent());

    final UnsafeBuffer aggregatedBuffer = new UnsafeBuffer(new byte[bigUpdateOffset + SIZE_OF_SHORT]);
    aggregatedBuffer.putLong(0, META_DATA_VALUE);
    aggregatedBuffer.putShort(bigUpdateOffset, UPDATE_VALUE);

    readBuffer = readSuccessfulMetaData(aggregatedBuffer);
    assertEquals(aggregatedBuffer, readBuffer);
}
 
Example 4
Source File: FrameDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Write the type field for a frame.
 *
 * @param buffer     containing the frame.
 * @param termOffset at which a frame begins.
 * @param type       type value for the frame.
 */
public static void frameType(final UnsafeBuffer buffer, final int termOffset, final int type)
{
    buffer.putShort(typeOffset(termOffset), (short)type, LITTLE_ENDIAN);
}