public static int encode(
      final MutableDirectBuffer mutableDirectBuffer,
      final int offset,
      int flags,
      final int keepaliveInterval,
      final int maxLifetime,
      final String metadataMimeType,
      final String dataMimeType,
      final ByteBuffer metadata,
      final ByteBuffer data) {
    final int frameLength =
        computeFrameLength(metadataMimeType, dataMimeType, metadata.remaining(), data.remaining());

    int length =
        FrameHeaderFlyweight.encodeFrameHeader(
            mutableDirectBuffer, offset, frameLength, flags, FrameType.SETUP, 0);

    mutableDirectBuffer.putInt(
        offset + VERSION_FIELD_OFFSET, CURRENT_VERSION, ByteOrder.BIG_ENDIAN);
    mutableDirectBuffer.putInt(
        offset + KEEPALIVE_INTERVAL_FIELD_OFFSET, keepaliveInterval, ByteOrder.BIG_ENDIAN);
    mutableDirectBuffer.putInt(
        offset + MAX_LIFETIME_FIELD_OFFSET, maxLifetime, ByteOrder.BIG_ENDIAN);

    length += BitUtil.SIZE_OF_INT * 3;

    length += putMimeType(mutableDirectBuffer, offset + length, metadataMimeType);
    length += putMimeType(mutableDirectBuffer, offset + length, dataMimeType);

    length +=
        FrameHeaderFlyweight.encodeMetadata(mutableDirectBuffer, offset, offset + length, metadata);
    length += FrameHeaderFlyweight.encodeData(mutableDirectBuffer, offset + length, data);

    return length;
  }
  private static int putMimeType(
      final MutableDirectBuffer mutableDirectBuffer, final int fieldOffset, final String mimeType) {
    byte[] bytes = mimeType.getBytes(StandardCharsets.UTF_8);

    mutableDirectBuffer.putByte(fieldOffset, (byte) bytes.length);
    mutableDirectBuffer.putBytes(fieldOffset + 1, bytes);

    return 1 + bytes.length;
  }
 private long encodeApplicationMessageWithFrame(ByteBuffer buffer, byte[] message) {
   int offset = 0;
   mutableBuffer.wrap(buffer);
   frameEncoder.wrap(buffer, offset).encodeFrameHeader();
   offset += frameEncoder.getHeaderLength();
   messageHeaderEncoder.wrap(mutableBuffer, offset);
   messageHeaderEncoder
       .blockLength(message.length)
       .templateId(templateId)
       .schemaId(schemaId)
       .version(schemaVersion);
   offset += MessageHeaderEncoder.ENCODED_LENGTH;
   buffer.position(offset);
   buffer.put(message, 0, message.length);
   frameEncoder.setMessageLength(message.length + MessageHeaderEncoder.ENCODED_LENGTH);
   frameEncoder.encodeFrameTrailer();
   return frameEncoder.getEncodedLength();
 }
Пример #4
0
 public void wrap(final MutableDirectBuffer buffer, final int offset) {
   super.wrap(buffer, offset);
   buffer.putInt(offset, type().ordinal());
 }