private static byte[] getMimeType(final DirectBuffer directBuffer, final int fieldOffset) {
    final int length = directBuffer.getByte(fieldOffset);
    final byte[] bytes = new byte[length];

    directBuffer.getBytes(fieldOffset + 1, bytes);
    return bytes;
  }
  @Test
  public void shouldEncodeCorrectly() {
    final ByteBuffer encodedMsgBuffer = ByteBuffer.allocateDirect(MSG_BUFFER_CAPACITY);
    encodeTestMessage(encodedMsgBuffer);

    final DirectBuffer decodeBuffer = new UnsafeBuffer(encodedMsgBuffer);

    int offset = 0;
    assertThat(decodeBuffer.getShort(offset), is((short) 22));
    offset += BitUtil.SIZE_OF_SHORT;

    assertThat(decodeBuffer.getShort(offset), is((short) 1));
    offset += BitUtil.SIZE_OF_SHORT;

    assertThat(decodeBuffer.getShort(offset), is((short) 3));
    offset += BitUtil.SIZE_OF_SHORT;

    assertThat(decodeBuffer.getShort(offset), is((short) 0));
    offset += BitUtil.SIZE_OF_SHORT;

    assertThat(decodeBuffer.getByte(offset), is((byte) 10));
    offset += BitUtil.SIZE_OF_BYTE;

    assertThat(decodeBuffer.getByte(offset), is((byte) 42));
    offset += BitUtil.SIZE_OF_BYTE;

    assertThat(decodeBuffer.getInt(offset), is(0x00_01_00_00));
    offset += BitUtil.SIZE_OF_INT;

    assertThat(decodeBuffer.getLong(offset), is(101L));
    offset += BitUtil.SIZE_OF_LONG;

    assertThat(decodeBuffer.getLong(offset), is(202L));
  }
  public static int payloadOffset(final DirectBuffer directBuffer, final int offset) {
    int fieldOffset = offset + METADATA_MIME_TYPE_LENGTH_OFFSET;

    final int metadataMimeTypeLength = directBuffer.getByte(fieldOffset);
    fieldOffset += 1 + metadataMimeTypeLength;

    final int dataMimeTypeLength = directBuffer.getByte(fieldOffset);
    fieldOffset += 1 + dataMimeTypeLength;

    return fieldOffset;
  }
Example #4
0
  private static void pongHandler(
      final DirectBuffer buffer, final int offset, final int length, final Header header) {
    final long pingTimestamp = buffer.getLong(offset);
    final long rttNs = System.nanoTime() - pingTimestamp;

    HISTOGRAM.recordValue(rttNs);
  }
  public static String dataMimeType(final DirectBuffer directBuffer, final int offset) {
    int fieldOffset = offset + METADATA_MIME_TYPE_LENGTH_OFFSET;

    fieldOffset += 1 + directBuffer.getByte(fieldOffset);

    final byte[] bytes = getMimeType(directBuffer, fieldOffset);
    return new String(bytes, StandardCharsets.UTF_8);
  }
 public static int keepaliveInterval(final DirectBuffer directBuffer, final int offset) {
   return directBuffer.getInt(offset + KEEPALIVE_INTERVAL_FIELD_OFFSET, ByteOrder.BIG_ENDIAN);
 }
 public static int version(final DirectBuffer directBuffer, final int offset) {
   return directBuffer.getInt(offset + VERSION_FIELD_OFFSET, ByteOrder.BIG_ENDIAN);
 }
 public static int maxLifetime(final DirectBuffer directBuffer, final int offset) {
   return directBuffer.getInt(offset + MAX_LIFETIME_FIELD_OFFSET, ByteOrder.BIG_ENDIAN);
 }
 public void wrap(final DirectBuffer buffer, final int offset) {
   if (buffer.getInt(offset) != type().ordinal()) {
     throw new IllegalArgumentException("Buffer must contain message type " + type());
   }
   super.wrap(buffer, offset);
 }