Fixes GitHub issue #25: DATA_INDICATION encode error with STUN(TURN).

This commit is contained in:
Lyubomir Marinov 2015-07-28 09:16:33 -05:00
parent 1960ab615a
commit 469f045b9b

View File

@ -38,12 +38,12 @@ public class DataAttribute
* *
* Some dialect does not add (and support) padding (GTalk). * Some dialect does not add (and support) padding (GTalk).
*/ */
private boolean padding = true; private final boolean padding;
/** /**
* Constructor. * Constructor.
*/ */
protected DataAttribute () protected DataAttribute()
{ {
this(true); this(true);
} }
@ -51,9 +51,10 @@ public class DataAttribute
/** /**
* Constructor. * Constructor.
*/ */
protected DataAttribute (boolean padding) protected DataAttribute(boolean padding)
{ {
super(DATA); super(DATA);
this.padding = padding; this.padding = padding;
} }
@ -80,22 +81,26 @@ public class DataAttribute
*/ */
public byte[] encode() public byte[] encode()
{ {
char dataLength = getDataLength();
char type = getAttributeType(); char type = getAttributeType();
byte binValue[] = new byte[HEADER_LENGTH + getDataLength() + byte binary[]
(padding ? (getDataLength() % 4) : 0)]; = new byte[
HEADER_LENGTH
+ dataLength
+ (padding ? ((4 - dataLength % 4) % 4) : 0)];
//Type //Type
binValue[0] = (byte)(type >> 8); binary[0] = (byte)(type >> 8);
binValue[1] = (byte)(type & 0x00FF); binary[1] = (byte)(type & 0x00FF);
//Length //Length
binValue[2] = (byte)(getDataLength() >> 8); binary[2] = (byte)(dataLength >> 8);
binValue[3] = (byte)(getDataLength() & 0x00FF); binary[3] = (byte)(dataLength & 0x00FF);
//data //data
System.arraycopy(data, 0, binValue, 4, getDataLength()); System.arraycopy(data, 0, binary, 4, dataLength);
return binValue; return binary;
} }
/** /**
@ -169,4 +174,4 @@ public class DataAttribute
return true; return true;
} }
} }