isMessageAvailable method

bool isMessageAvailable()

Message available

Implementation

bool isMessageAvailable() {
  if (availableBytes == 0) {
    return false;
  }

  // If we do not have 2 bytes we do not have a complete header, so no
  // message is available.
  if (length < 2) {
    return false;
  }

  // read the message size by peeking in to the header and return true only
  // if the whole message is available.

  // If the first byte of the header is 0 then skip past it.
  if (peekByte() == 0) {
    MqttLogger.log(
        'MqttByteBuffer:isMessageAvailable - first header byte is zero, skipping');
    _position++;
    shrink();
  }

  // Assume we now have a valid header
  MqttLogger.log(
      'MqttByteBuffer:isMessageAvailable - assumed valid header, value is ${peekByte()}');
  // Save the position
  var position = _position;
  var header = MqttHeader.fromByteBuffer(this);
  // Restore the position
  final avibytes = availableBytes; // should same in MqttMessage.createFrom
  _position = position;
  if (avibytes < header.messageSize) {
    MqttLogger.log(
        'MqttByteBuffer:isMessageAvailable - Available bytes($avibytes) is less than the message size'
        ' ${header.messageSize}');

    return false;
  }

  return true;
}