2017-02-01 15:57:48 +01:00
|
|
|
|
2015-11-20 20:54:27 +01:00
|
|
|
package io.moquette.spi.impl;
|
2015-01-30 00:53:51 +01:00
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
import io.netty.handler.codec.mqtt.MqttMessage;
|
|
|
|
|
import io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader;
|
2015-01-30 00:53:51 +01:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Utility static methods, like Map get with default value, or elvis operator.
|
|
|
|
|
*/
|
|
|
|
|
public class Utils {
|
2017-01-28 10:06:30 +01:00
|
|
|
|
2015-01-30 00:53:51 +01:00
|
|
|
public static <T, K> T defaultGet(Map<K, T> map, K key, T defaultValue) {
|
|
|
|
|
T value = map.get(key);
|
|
|
|
|
if (value != null) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
2017-01-28 10:06:30 +01:00
|
|
|
|
|
|
|
|
public static int messageId(MqttMessage msg) {
|
|
|
|
|
return ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte[] readBytesAndRewind(ByteBuf payload) {
|
|
|
|
|
byte[] payloadContent = new byte[payload.readableBytes()];
|
|
|
|
|
int mark = payload.readerIndex();
|
|
|
|
|
payload.readBytes(payloadContent);
|
|
|
|
|
payload.readerIndex(mark);
|
|
|
|
|
return payloadContent;
|
|
|
|
|
}
|
2015-01-30 00:53:51 +01:00
|
|
|
}
|