From d33d8b87e47b7d34ac196b64ae814d051bb2fc4a Mon Sep 17 00:00:00 2001 From: Etienne CHAMPETIER Date: Mon, 18 Apr 2016 17:13:54 +0200 Subject: [PATCH] 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 --- .../java/org/ice4j/message/ChannelData.java | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/ice4j/message/ChannelData.java b/src/main/java/org/ice4j/message/ChannelData.java index 5e9454e..c3b3bb3 100644 --- a/src/main/java/org/ice4j/message/ChannelData.java +++ b/src/main/java/org/ice4j/message/ChannelData.java @@ -100,27 +100,26 @@ public class ChannelData } /** - * Get the data length. + * Get the data length (without padding). * * @return data length */ public char getDataLength() { - char length = 0; if(data == null) - { - return length; - } - - /* pad to a multiple of four */ - if((length % 4) > 0) - { - length += (4 - (length % 4)); - } + return 0; 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 * number range defined by the TURN RFC. @@ -136,13 +135,27 @@ public class ChannelData } /** - * Returns a binary representation of this message. - * @return a binary representation of this message. + * Returns a non padded binary representation of this message. + * @return a non padded binary representation of this message. * @throws StunException if the channel number is invalid + * @deprecated */ 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]; int offset = 0;