Java Code Examples for org.agrona.DirectBuffer#byteBuffer()

The following examples show how to use org.agrona.DirectBuffer#byteBuffer() . 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: SenderEndPoint.java    From artio with Apache License 2.0 6 votes vote down vote up
private int writeFramedMessage(
    final DirectBuffer directBuffer,
    final int offset,
    final int length,
    final long timeInMs)
    throws IOException
{
    final ByteBuffer buffer = directBuffer.byteBuffer();
    final int startLimit = buffer.limit();
    final int startPosition = buffer.position();

    ByteBufferUtil.limit(buffer, offset + length);
    ByteBufferUtil.position(buffer, offset);

    final int written = channel.write(buffer);
    if (written > 0)
    {
        ByteBufferUtil.position(buffer, offset);
        DebugLogger.log(FIX_MESSAGE_TCP, "Written  ", buffer, written);
        updateSendingTimeoutTimeInMs(timeInMs, written);

        buffer.limit(startLimit).position(startPosition);
    }

    return written;
}
 
Example 2
Source File: MessageDumper.java    From artio with Apache License 2.0 5 votes vote down vote up
public static String print(
    final JsonPrinter dumper, final DirectBuffer buffer, final int offset, final int length)
{
    final ByteBuffer byteBuffer = buffer.byteBuffer();
    final int originalPosition = byteBuffer.position();
    final int originalLimit = byteBuffer.limit();
    byteBuffer.limit(length + offset).position(offset);
    final ByteBuffer slice = byteBuffer.slice();
    byteBuffer.limit(originalLimit).position(originalPosition);

    return dumper.print(slice);
}
 
Example 3
Source File: NDArrayMessageChunkTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testChunkSerialization() {
    NDArrayMessage message = NDArrayMessage.wholeArrayUpdate(Nd4j.ones(1000));
    int chunkSize = 128;
    int numChunks = NDArrayMessage.numChunksForMessage(message, chunkSize);
    NDArrayMessageChunk[] chunks = NDArrayMessage.chunks(message, chunkSize);
    assertEquals(numChunks, chunks.length);
    for (int i = 1; i < numChunks; i++) {
        assertEquals(chunks[0].getMessageType(), chunks[i].getMessageType());
        assertEquals(chunks[0].getId(), chunks[i].getId());
        assertEquals(chunks[0].getChunkSize(), chunks[i].getChunkSize());
        assertEquals(chunks[0].getNumChunks(), chunks[i].getNumChunks());
    }

    ByteBuffer[] concat = new ByteBuffer[chunks.length];
    for (int i = 0; i < concat.length; i++)
        concat[i] = chunks[i].getData();


    DirectBuffer buffer = NDArrayMessage.toBuffer(message);
    //test equality of direct byte buffer contents vs chunked
    ByteBuffer byteBuffer = buffer.byteBuffer();
    ByteBuffer concatAll = BufferUtil.concat(concat, buffer.capacity());
    byte[] arrays = new byte[byteBuffer.capacity()];
    byteBuffer.rewind();
    byteBuffer.get(arrays);
    byte[] arrays2 = new byte[concatAll.capacity()];
    concatAll.rewind();
    concatAll.get(arrays2);
    assertArrayEquals(arrays, arrays2);
    NDArrayMessage message1 = NDArrayMessage.fromChunks(chunks);
    assertEquals(message, message1);

}
 
Example 4
Source File: NDArrayMessageChunkTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testChunkSerialization() {
    NDArrayMessage message = NDArrayMessage.wholeArrayUpdate(Nd4j.ones(1000));
    int chunkSize = 128;
    int numChunks = NDArrayMessage.numChunksForMessage(message, chunkSize);
    NDArrayMessageChunk[] chunks = NDArrayMessage.chunks(message, chunkSize);
    assertEquals(numChunks, chunks.length);
    for (int i = 1; i < numChunks; i++) {
        assertEquals(chunks[0].getMessageType(), chunks[i].getMessageType());
        assertEquals(chunks[0].getId(), chunks[i].getId());
        assertEquals(chunks[0].getChunkSize(), chunks[i].getChunkSize());
        assertEquals(chunks[0].getNumChunks(), chunks[i].getNumChunks());
    }

    ByteBuffer[] concat = new ByteBuffer[chunks.length];
    for (int i = 0; i < concat.length; i++)
        concat[i] = chunks[i].getData();


    DirectBuffer buffer = NDArrayMessage.toBuffer(message);
    //test equality of direct byte buffer contents vs chunked
    ByteBuffer byteBuffer = buffer.byteBuffer();
    ByteBuffer concatAll = BufferUtil.concat(concat, buffer.capacity());
    byte[] arrays = new byte[byteBuffer.capacity()];
    byteBuffer.rewind();
    byteBuffer.get(arrays);
    byte[] arrays2 = new byte[concatAll.capacity()];
    concatAll.rewind();
    concatAll.get(arrays2);
    assertArrayEquals(arrays, arrays2);
    NDArrayMessage message1 = NDArrayMessage.fromChunks(chunks);
    assertEquals(message, message1);

}
 
Example 5
Source File: ILink3SenderEndPoint.java    From artio with Apache License 2.0 4 votes vote down vote up
public Action onMessage(final DirectBuffer directBuffer, final int offset)
{
    final int messageSize = SimpleOpenFramingHeader.readSofhMessageSize(directBuffer, offset);
    final int reattemptBytesWritten = this.reattemptBytesWritten;

    final ByteBuffer buffer = directBuffer.byteBuffer();
    final int startLimit = buffer.limit();
    final int startPosition = buffer.position();

    ByteBufferUtil.limit(buffer, offset + messageSize);
    ByteBufferUtil.position(buffer, reattemptBytesWritten + offset);

    try
    {
        final int written = channel.write(buffer);
        if (written > 0)
        {
            ByteBufferUtil.position(buffer, offset);
            DebugLogger.logBytes(FIX_MESSAGE_TCP, "Written  ", buffer, startPosition, written);

            buffer.limit(startLimit).position(startPosition);
        }
        final int totalWritten = reattemptBytesWritten + written;
        if (totalWritten < messageSize)
        {
            this.reattemptBytesWritten = totalWritten;

            return ABORT;
        }
        else
        {
            this.reattemptBytesWritten = NO_REATTEMPT;
        }
    }
    catch (final IOException e)
    {
        errorHandler.onError(e);
    }

    return CONTINUE;
}
 
Example 6
Source File: SenderEndPoint.java    From artio with Apache License 2.0 4 votes vote down vote up
private Action attemptSlowMessage(
    final DirectBuffer directBuffer,
    final int offsetAfterHeader,
    final int length,
    final long position,
    final int bodyLength,
    final long timeInMs,
    final StreamTracker tracker,
    final int metaDataLength,
    final int sequenceNumber)
{
    if (!isSlowConsumer())
    {
        return CONTINUE;
    }

    // Skip all messages beyond the skip position, since this endpoint has been blocked but others
    // Scanning forward.
    final long skipPosition = tracker.skipPosition;
    if (position > skipPosition)
    {
        return CONTINUE;
    }

    // Skip messages where the end point has become a slow consumer, but
    // the slow consumer stream hasn't polled up to update with the regular stream
    final long sentPosition = tracker.sentPosition;
    if (position <= sentPosition)
    {
        return CONTINUE;
    }

    if (partiallySentOtherStream(tracker))
    {
        return blockPosition(position, length, tracker);
    }

    try
    {
        final long startOfMessage = position - length;
        final int remainingLength;
        final int bytesPreviouslySent;

        // You've complete the stream and there's another message in between.
        if (sentPosition < startOfMessage)
        {
            remainingLength = bodyLength;
            bytesPreviouslySent = 0;
        }
        else
        {
            remainingLength = (int)(position - sentPosition);
            bytesPreviouslySent = bodyLength - remainingLength;
        }

        final int dataOffset = offsetAfterHeader + FRAME_SIZE + metaDataLength + bytesPreviouslySent;
        final ByteBuffer buffer = directBuffer.byteBuffer();

        ByteBufferUtil.limit(buffer, dataOffset + remainingLength);
        ByteBufferUtil.position(buffer, dataOffset);

        final int written = channel.write(buffer);
        bytesInBuffer.getAndAddOrdered(-written);

        updateSendingTimeoutTimeInMs(timeInMs, written);

        if (bodyLength > (written + bytesPreviouslySent))
        {
            tracker.moveSentPosition(written);
            return blockPosition(position, length, tracker);
        }
        else
        {
            tracker.sentPosition = position;
            tracker.partiallySentMessage = false;
            tracker.skipPosition = Long.MAX_VALUE;

            if (sequenceNumber != REPLAY_MESSAGE && messageTimingHandler != null)
            {
                messageTimingHandler.onMessage(sequenceNumber, connectionId);
            }

            if (!isSlowConsumer())
            {
                becomeNormalConsumer();
            }
        }
    }
    catch (final IOException ex)
    {
        onError(ex);
    }

    return CONTINUE;
}
 
Example 7
Source File: AeronNDArraySerde.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * Get the direct byte buffer from the given direct buffer
 * @param directBuffer
 * @return
 */
public static ByteBuffer getDirectByteBuffer(DirectBuffer directBuffer) {
    return directBuffer.byteBuffer() == null
                    ? ByteBuffer.allocateDirect(directBuffer.capacity()).put(directBuffer.byteArray())
                    : directBuffer.byteBuffer();
}
 
Example 8
Source File: AeronNDArraySerde.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * Get the direct byte buffer from the given direct buffer
 * @param directBuffer
 * @return
 */
public static ByteBuffer getDirectByteBuffer(DirectBuffer directBuffer) {
    return directBuffer.byteBuffer() == null
                    ? ByteBuffer.allocateDirect(directBuffer.capacity()).put(directBuffer.byteArray())
                    : directBuffer.byteBuffer();
}