0

I am new to the MQTT and Android Open Accessory "AOA". while reading a tutorial I realized that, before any attempt to write to the variable of the type ByteArrayOutputStream,however, 0 or 0x00 should be written to that variable first.

Is this some kind of initialisation? Below is an example of that:

EX_1

variableHeader.write(0x00);
variableHeader.write(PROTOCOL_NAME.getBytes("UTF-8").length);
variableHeader.write(PROTOCOL_NAME.getBytes("UTF-8"));

EX_2

public static byte[] connect() throws UnsupportedEncodingException, IOException {
   String identifier = "android";
   ByteArrayOutputStream payload = new ByteArrayOutputStream();
   payload.write(0);
   payload.write(identifier.length());
}
Amrmsmb
  • 1
  • 27
  • 104
  • 226

1 Answers1

2

This is not any kind of initialization needed by the ByteArrayOutputStream. Calling write(0) simply inserts a 0-byte as a the first byte in the byte array.

Instead, the byte must have meaning to the MQTT protocol. I'm not familiar with it, but a quick look at the MQTT protocol specification reveals that strings are encoded by writing the string bytes in UTF-8, prefixed by a 2-byte length field, upper byte first.

In both the examples you give, strings are being written, but it is only writing one length byte. The 0 byte, then, must be the other length byte. I'm sure that's what it is. The code is a bit sloppy: it assumes that the strings in your case are less than 256 bytes long, so it can always assume the upper length byte is 0.

If there is any possibility of the "protocol name" being 256 bytes or longer, then the proper way to write this code:

variableHeader.write(0x00);
variableHeader.write(PROTOCOL_NAME.getBytes("UTF-8").length);
variableHeader.write(PROTOCOL_NAME.getBytes("UTF-8"));

would be:

byte[] stringBytes = PROTOCOL_NAME.getBytes("UTF-8");
variableHeader.write(stringBytes.length >> 8); // upper length byte
variableHeader.write(stringBytes.length & 0xFF); // lower length byte
variableHeader.write(stringBytes); // actual data
Boann
  • 48,794
  • 16
  • 117
  • 146
  • thank you fr the answe, but please let me know, why the right shift is used followed by And peration over 0xFF – Amrmsmb Nov 02 '14 at 14:06
  • 1
    `length >> 8` writes the upper byte by shifting the number down by 8 bits, so that the bits from positions 8 to 15 are now in the positions of bits 0 to 7. The bits that were at positions 0 to 7 are removed. (For positive numbers, this is equivalent to dividing the number by 2 to the 8th power; i.e., 256.) For the lower length byte, bits 0 to 7 are already in the wanted position, but we need to turn off the other bits of the number, which is done by ANDing with 0xFF, which is a number having only bits 0 to 7 set. – Boann Nov 02 '14 at 14:12
  • thank you. i had another question related to the same topic, please have a look if you d not mind http://stackoverflow.com/questions/26689636/regarding-the-mqtt-fixed-header-structure – Amrmsmb Nov 02 '14 at 14:22