Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 dc-square GmbH
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.dcsquare.hivemq.spi.util;
 
 
 import java.util.List;

This util can be used to determine the size of each MQTT command message.

Author(s):
Christian Goetz
Since:
1.4
 
 public class MqttMessageEstimationUtil {

    
Length of the fixed header in bytes
 
     private static final int FIXED_HEADER_BYTES = 2;
    
Length of the message id in bytes
 
     private static final int MESSAGE_ID_BYTES = 2;

    
Estimates the size of a com.dcsquare.hivemq.spi.message.CONNECT message

Parameters:
connect message, which should be estimated
Returns:
message size in bytes
 
     public static int estimateConnectSize(final CONNECT connect) {
         int size = 0;
         size += ;
 
         size += 12;
 
         if (connect != null) {
             size += estimateUTF8StringSize(connect.getClientIdentifier());
             if (connect.isWill()) {
                 size += estimateUTF8StringSize(connect.getWillTopic());
                 size += estimateUTF8StringSize(connect.getWillMessage());
             }
             if (connect.isUsernameRequired()) {
                 size += estimateUTF8StringSize(connect.getUsername());
             }
             if (connect.isPasswordRequired()) {
                 size += estimateUTF8StringSize(connect.getPassword());
             }
         } else {
             return 0;
         }
 
         return size;
 
     }

    
Estimates the size of a com.dcsquare.hivemq.spi.message.CONNACK message

com.dcsquare.hivemq.spi.message.CONNACK object is not needed, because the message has the same size all the time.

Returns:
message size in bytes
 
     public static int estimateConnackSize() {
         int size = 0;
         size += ;
         // Variable Header: ConnectReturnCode & Topic Name Compression Response
         size += 2;
         return size;
 
     }

    
/** Estimates the size of a com.dcsquare.hivemq.spi.message.PUBLISH message

Parameters:
publish message, which should be estimated
Returns:
message size in bytes
 
     public static int estimatePublishSize(final PUBLISH publish) {
         int size = 0;
         size += ;
         if (publish != null) {
            if (publish.getQoS() == . || publish.getQoS() == .) {
                size += ;
            }
            if (publish.getTopic() != null) {
                size += estimateUTF8StringSize(publish.getTopic());
            }
            if (publish.getPayload() != null) {
                size += publish.getPayload().length;
            }
        } else {
            return 0;
        }
        return size;
    }

    
Estimates the size of a com.dcsquare.hivemq.spi.message.PUBACK message

com.dcsquare.hivemq.spi.message.PUBACK object is not needed, because the message has the same size all the time.

Returns:
message size in bytes
    public static int estimatePubackSize() {
        int size = 0;
        size += ;
        size += ;
        return size;
    }

    
Estimates the size of a com.dcsquare.hivemq.spi.message.PUBREC message

com.dcsquare.hivemq.spi.message.PUBREC object is not needed, because the message has the same size all the time.

Returns:
message size in bytes
    public static int estimatePubrecSize() {
        int size = 0;
        size += ;
        size += ;
        return size;
    }

    
Estimates the size of a com.dcsquare.hivemq.spi.message.PUBREL message

com.dcsquare.hivemq.spi.message.PUBREL object is not needed, because the message has the same size all the time.

Returns:
message size in bytes
    public static int estimatePubrelSize() {
        int size = 0;
        size += ;
        size += ;
        return size;
    }

    
Estimates the size of a com.dcsquare.hivemq.spi.message.PUBCOMP message

com.dcsquare.hivemq.spi.message.PUBCOMP object is not needed, because the message has the same size all the time.

Returns:
message size in bytes
    public static int estimatePubcompSize() {
        int size = 0;
        size += ;
        size += ;
        return size;
    }

    
Estimates the size of a com.dcsquare.hivemq.spi.message.SUBSCRIBE message

Parameters:
subscribe message, which should be estimated
Returns:
message size in bytes
    public static int estimateSubscribeSize(final SUBSCRIBE subscribe) {
        int size = 0;
        size += ;
        size += ;
        if (subscribe != null) {
            final List<Topictopics = subscribe.getTopics();
            for (final Topic topic : topics) {
                size += estimateUTF8StringSize(topic.getTopic());
                //QoS uses 1 Byte
                size += 1;
            }
        } else {
            return 0;
        }
        return size;
    }

    
Estimates the size of a com.dcsquare.hivemq.spi.message.SUBACK message

Parameters:
suback message, which should be estimated
Returns:
message size in bytes
    public static int estimateSubackSize(final SUBACK suback) {
        int size = 0;
        size += ;
        size += ;
        if (suback != null) {
            size += suback.getGrantedQos().size();
        } else {
            return 0;
        }
        return size;
    }

    
Estimates the size of a com.dcsquare.hivemq.spi.message.UNSUBSCRIBE message

Parameters:
unsubscribe message, which should be estimated
Returns:
message size in bytes
    public static int estimateUnsubscribeSize(final UNSUBSCRIBE unsubscribe) {
        int size = 0;
        size += ;
        size += ;
        final List<Stringtopics = unsubscribe.getTopics();
        for (final String topic : topics) {
            size += estimateUTF8StringSize(topic);
        }
        return size;
    }

    
Estimates the size of a com.dcsquare.hivemq.spi.message.UNSUBACK message

com.dcsquare.hivemq.spi.message.UNSUBACK object is not needed, because the message has the same size all the time.

Returns:
message size in bytes
    public static int estimateUnsubackSize() {
        int size = 0;
        size += ;
        size += ;
        return size;
    }

    
Estimates the size of a com.dcsquare.hivemq.spi.message.PINGREQ message

com.dcsquare.hivemq.spi.message.PINGREQ object is not needed, because the message has the same size all the time.

Returns:
message size in bytes
    public static int estimatePingReqSize() {
        int size = 0;
        size += ;
        return size;
    }

    
Estimates the size of a com.dcsquare.hivemq.spi.message.PINGRESP message

com.dcsquare.hivemq.spi.message.PINGRESP object is not needed, because the message has the same size all the time.

Returns:
message size in bytes
    public static int estimatePingRespSize() {
        int size = 0;
        size += ;
        return size;
    }

    
Estimates the size of a com.dcsquare.hivemq.spi.message.DISCONNECT message

com.dcsquare.hivemq.spi.message.DISCONNECT object is not needed, because the message has the same size all the time.

Returns:
message size in bytes
    public static int estimateDisconnectSize() {
        int size = 0;
        size += ;
        return size;
    }

    
Estimates the size of a string used in a MQTT message

Returns:
string size in bytes
    private static int estimateUTF8StringSize(final String s) {
        if (s != null) {
            int size = 0;
            size += s.getBytes().length;
            //definition of length in mqtt
            size += 2;
            return size;
        } else {
            return 0;
        }
    }
New to GrepCode? Check out our FAQ X