ChannelData: rework padding
getDataLength() was returning data length without padding, so keep current behavior and update the javadoc. (spotted with findbugs because length in getDataLength was a dead store) Introduce padTo4() to round up a number to the next multiple of 4. Deprecate encode() and introduce encode(boolean pad) so we can let the caller decide if he wants padding (mandatory when using tcp or tls) See https://tools.ietf.org/html/rfc5766#section-11.5 Signed-off-by: Etienne CHAMPETIER <champetier.etienne@gmail.com>
This commit is contained in:
parent
249ebb913a
commit
d33d8b87e4
@ -100,27 +100,26 @@ public class ChannelData
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the data length.
|
* Get the data length (without padding).
|
||||||
*
|
*
|
||||||
* @return data length
|
* @return data length
|
||||||
*/
|
*/
|
||||||
public char getDataLength()
|
public char getDataLength()
|
||||||
{
|
{
|
||||||
char length = 0;
|
|
||||||
if(data == null)
|
if(data == null)
|
||||||
{
|
return 0;
|
||||||
return length;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* pad to a multiple of four */
|
|
||||||
if((length % 4) > 0)
|
|
||||||
{
|
|
||||||
length += (4 - (length % 4));
|
|
||||||
}
|
|
||||||
|
|
||||||
return (char)data.length;
|
return (char)data.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return num padded to 4
|
||||||
|
*/
|
||||||
|
private static int padTo4(int num)
|
||||||
|
{
|
||||||
|
return (num + 3) & ~3;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines whether a specific channel number is in the valid channel
|
* Determines whether a specific channel number is in the valid channel
|
||||||
* number range defined by the TURN RFC.
|
* number range defined by the TURN RFC.
|
||||||
@ -136,13 +135,27 @@ public class ChannelData
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a binary representation of this message.
|
* Returns a non padded binary representation of this message.
|
||||||
* @return a binary representation of this message.
|
* @return a non padded binary representation of this message.
|
||||||
* @throws StunException if the channel number is invalid
|
* @throws StunException if the channel number is invalid
|
||||||
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
public byte[] encode() throws StunException
|
public byte[] encode() throws StunException
|
||||||
{
|
{
|
||||||
char dataLength = getDataLength();
|
return encode(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a binary representation of this message.
|
||||||
|
* @param pad determine if we pad this message
|
||||||
|
* @return a binary representation of this message.
|
||||||
|
* @throws StunException if the channel number is invalid
|
||||||
|
*/
|
||||||
|
public byte[] encode(boolean pad) throws StunException
|
||||||
|
{
|
||||||
|
int dataLength = getDataLength();
|
||||||
|
if (pad)
|
||||||
|
dataLength = padTo4(dataLength);
|
||||||
byte binMsg[] = new byte[HEADER_LENGTH + dataLength];
|
byte binMsg[] = new byte[HEADER_LENGTH + dataLength];
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user