From a66dde7954fc56d62ea434cb0bb2f304e534b735 Mon Sep 17 00:00:00 2001 From: Jonas Meller Date: Sat, 1 Nov 2014 15:29:48 +0100 Subject: [PATCH 1/2] Add test case that verifies the functionality of BinaryEncode(binary, "hex"). --- tests/testcases/functions/BinaryEncode.cfc | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/testcases/functions/BinaryEncode.cfc diff --git a/tests/testcases/functions/BinaryEncode.cfc b/tests/testcases/functions/BinaryEncode.cfc new file mode 100644 index 0000000000..cc88ed41a4 --- /dev/null +++ b/tests/testcases/functions/BinaryEncode.cfc @@ -0,0 +1,13 @@ +component extends="org.railo.cfml.test.RailoTestCase" { + + public function setUp() { + variables.emptyBinary = CharsetDecode("", "utf-8"); + variables.binaryHello = CharsetDecode("Hello", "utf-8"); + } + + function testHex() { + $assert.isEqualWithCase("", BinaryEncode(variables.emptyBinary, "hex")); + $assert.isEqualWithCase("48656C6C6F", BinaryEncode(variables.binaryHello, "hex")); + } + +} From 7669a025cd456a04036ada724fe0408a3a262f8f Mon Sep 17 00:00:00 2001 From: Jonas Meller Date: Sat, 1 Nov 2014 15:39:52 +0100 Subject: [PATCH 2/2] Improve the performance of BinaryEncode(binary, "hex") by replacing the inefficient code with usage of Apache Commons Codec. --- .../src/railo/runtime/coder/HexCoder.java | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/railo-java/railo-core/src/railo/runtime/coder/HexCoder.java b/railo-java/railo-core/src/railo/runtime/coder/HexCoder.java index d947abca64..3944029bfe 100644 --- a/railo-java/railo-core/src/railo/runtime/coder/HexCoder.java +++ b/railo-java/railo-core/src/railo/runtime/coder/HexCoder.java @@ -1,5 +1,7 @@ package railo.runtime.coder; +import org.apache.commons.codec.binary.Hex; + import railo.commons.io.CharsetUtil; @@ -14,19 +16,7 @@ public final class HexCoder { * @return encoed String */ public static String encode(byte[] bytes) { - String retorno = ""; - if (bytes==null || bytes.length==0) { - return retorno; - } - for (int i=0; i> 4; - d2 += (d2 < 10) ? 48 : 55; - retorno = retorno + (char)d2 + (char)d1; - } - return retorno; + return Hex.encodeHexString(bytes).toUpperCase(); } /**