createFrom static method

MqttMessage createFrom(
  1. MqttByteBuffer messageStream
)

Creates a new instance of an MQTT Message based on a raw message stream.

Implementation

static MqttMessage createFrom(MqttByteBuffer messageStream) {
  try {
    var header = MqttHeader();
    // Pass the input stream sequentially through the component
    // deserialization(create) methods to build a full MqttMessage.
    header = MqttHeader.fromByteBuffer(messageStream);
    //expected position after reading payload
    final expectedPos = messageStream.position + header.messageSize;

    if (messageStream.availableBytes < header.messageSize) {
      messageStream.reset();
      throw InvalidMessageException(
          'Available bytes is less than the message size');
    }
    final message = MqttMessageFactory.getMessage(header, messageStream);

    if (messageStream.position < expectedPos) {
      messageStream.skipBytes = expectedPos - messageStream.position;
    }

    return message;
  } on Exception catch (e) {
    throw InvalidMessageException(
        'The data provided in the message stream was not a '
        'valid MQTT Message, '
        'exception is $e');
  }
}