This commit is contained in:
@@ -173,7 +173,7 @@ public class ConnectDecoder extends DemuxDecoder {
|
||||
|
||||
//Decode willMessage
|
||||
if (willFlag) {
|
||||
String willMessage = Utils.decodeString(in);
|
||||
byte[] willMessage = Utils.readFixedLengthContent(in);
|
||||
if (willMessage == null) {
|
||||
in.resetReaderIndex();
|
||||
return;
|
||||
@@ -207,7 +207,7 @@ public class ConnectDecoder extends DemuxDecoder {
|
||||
|
||||
//Decode password
|
||||
if (passwordFlag) {
|
||||
String password = Utils.decodeString(in);
|
||||
byte[] password = Utils.readFixedLengthContent(in);
|
||||
if (password == null) {
|
||||
in.resetReaderIndex();
|
||||
return;
|
||||
|
||||
@@ -65,12 +65,12 @@ public class ConnectEncoder extends DemuxEncoder<ConnectMessage> {
|
||||
variableHeaderBuff.writeBytes(Utils.encodeString(message.getClientID()));
|
||||
if (message.isWillFlag()) {
|
||||
variableHeaderBuff.writeBytes(Utils.encodeString(message.getWillTopic()));
|
||||
variableHeaderBuff.writeBytes(Utils.encodeString(message.getWillMessage()));
|
||||
variableHeaderBuff.writeBytes(Utils.encodeFixedLengthContent(message.getWillMessage()));
|
||||
}
|
||||
if (message.isUserFlag() && message.getUsername() != null) {
|
||||
variableHeaderBuff.writeBytes(Utils.encodeString(message.getUsername()));
|
||||
if (message.isPasswordFlag() && message.getPassword() != null) {
|
||||
variableHeaderBuff.writeBytes(Utils.encodeString(message.getPassword()));
|
||||
variableHeaderBuff.writeBytes(Utils.encodeFixedLengthContent(message.getPassword()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,10 +117,16 @@ public class Utils {
|
||||
* @return the decoded string or null if NEED_DATA
|
||||
*/
|
||||
static String decodeString(ByteBuf in) throws UnsupportedEncodingException {
|
||||
return new String(readFixedLengthContent(in), "UTF-8");
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a byte array from the buffer, use two bytes as length information followed by length bytes.
|
||||
* */
|
||||
static byte[] readFixedLengthContent(ByteBuf in) throws UnsupportedEncodingException {
|
||||
if (in.readableBytes() < 2) {
|
||||
return null;
|
||||
}
|
||||
//int strLen = Utils.readWord(in);
|
||||
int strLen = in.readUnsignedShort();
|
||||
if (in.readableBytes() < strLen) {
|
||||
return null;
|
||||
@@ -128,16 +134,14 @@ public class Utils {
|
||||
byte[] strRaw = new byte[strLen];
|
||||
in.readBytes(strRaw);
|
||||
|
||||
return new String(strRaw, "UTF-8");
|
||||
return strRaw;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Return the IoBuffer with string encoded as MSB, LSB and UTF-8 encoded
|
||||
* string content.
|
||||
*/
|
||||
public static ByteBuf encodeString(String str) {
|
||||
ByteBuf out = Unpooled.buffer(2);
|
||||
byte[] raw;
|
||||
try {
|
||||
raw = str.getBytes("UTF-8");
|
||||
@@ -147,12 +151,19 @@ public class Utils {
|
||||
LoggerFactory.getLogger(Utils.class).error(null, ex);
|
||||
return null;
|
||||
}
|
||||
//Utils.writeWord(out, raw.length);
|
||||
out.writeShort(raw.length);
|
||||
out.writeBytes(raw);
|
||||
return encodeFixedLengthContent(raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the IoBuffer with string encoded as MSB, LSB and bytes array content.
|
||||
*/
|
||||
public static ByteBuf encodeFixedLengthContent(byte[] content) {
|
||||
ByteBuf out = Unpooled.buffer(2);
|
||||
out.writeShort(content.length);
|
||||
out.writeBytes(content);
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the number of bytes to encode the given remaining length value
|
||||
*/
|
||||
|
||||
+3
-3
@@ -142,7 +142,7 @@ public class ConnectDecoderTest {
|
||||
verifyBaseHeader(message);
|
||||
assertEquals("ABCDEFGH", message.getClientID());
|
||||
assertEquals("Topic", message.getWillTopic());
|
||||
assertEquals("Message", message.getWillMessage());
|
||||
assertEquals("Message", new String(message.getWillMessage()));
|
||||
}
|
||||
|
||||
|
||||
@@ -167,9 +167,9 @@ public class ConnectDecoderTest {
|
||||
assertEquals(AbstractMessage.CONNECT, message.getMessageType());
|
||||
assertEquals("ABCDEFGH", message.getClientID());
|
||||
assertEquals("Topic", message.getWillTopic());
|
||||
assertEquals("Message", message.getWillMessage());
|
||||
assertEquals("Message", new String(message.getWillMessage()));
|
||||
assertEquals("Fakeuser", message.getUsername());
|
||||
assertEquals("pwd", message.getPassword());
|
||||
assertEquals("pwd", new String(message.getPassword()));
|
||||
}
|
||||
|
||||
@Test(expected = CorruptedFrameException.class)
|
||||
|
||||
+3
-3
@@ -75,7 +75,7 @@ public class ConnectEncoderTest {
|
||||
//variable part
|
||||
msg.setClientID("ABCDEF");
|
||||
msg.setWillTopic("Topic");
|
||||
msg.setWillMessage("Message");
|
||||
msg.setWillMessage("Message".getBytes());
|
||||
|
||||
ByteBuf out = Unpooled.buffer();
|
||||
|
||||
@@ -111,9 +111,9 @@ public class ConnectEncoderTest {
|
||||
//variable part
|
||||
msg.setClientID("ABCDEF");
|
||||
msg.setWillTopic("Topic");
|
||||
msg.setWillMessage("Message");
|
||||
msg.setWillMessage("Message".getBytes());
|
||||
msg.setUsername("Pablo");
|
||||
msg.setPassword("PBL");
|
||||
msg.setPassword("PBL".getBytes());
|
||||
|
||||
ByteBuf out = Unpooled.buffer();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user