You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2014/09/16 15:55:09 UTC
svn commit: r1625289 - in /tomcat/trunk:
java/org/apache/tomcat/util/buf/HexUtils.java
test/org/apache/tomcat/util/buf/TestHexUtils.java
Author: markt
Date: Tue Sep 16 13:55:09 2014
New Revision: 1625289
URL: http://svn.apache.org/r1625289
Log:
Add hex decoding support
Modified:
tomcat/trunk/java/org/apache/tomcat/util/buf/HexUtils.java
tomcat/trunk/test/org/apache/tomcat/util/buf/TestHexUtils.java
Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/HexUtils.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/HexUtils.java?rev=1625289&r1=1625288&r2=1625289&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/buf/HexUtils.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/buf/HexUtils.java Tue Sep 16 13:55:09 2014
@@ -65,10 +65,12 @@ public final class HexUtils {
}
}
+
public static byte getHex(int index) {
return HEX[index];
}
+
public static String toHexString(byte[] bytes) {
if (null == bytes) {
return null;
@@ -84,4 +86,18 @@ public final class HexUtils {
return sb.toString();
}
+
+
+ public static byte[] fromHexString(String input) {
+ if (input == null) {
+ return null;
+ }
+
+ char[] inputChars = input.toCharArray();
+ byte[] result = new byte[input.length() >> 1];
+ for (int i = 0; i < result.length; i++) {
+ result[i] = (byte) ((getDec(inputChars[2*i]) << 4) + getDec(inputChars[2*i + 1]));
+ }
+ return result;
+ }
}
Modified: tomcat/trunk/test/org/apache/tomcat/util/buf/TestHexUtils.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/util/buf/TestHexUtils.java?rev=1625289&r1=1625288&r2=1625289&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/tomcat/util/buf/TestHexUtils.java (original)
+++ tomcat/trunk/test/org/apache/tomcat/util/buf/TestHexUtils.java Tue Sep 16 13:55:09 2014
@@ -17,8 +17,9 @@
package org.apache.tomcat.util.buf;
-import static org.junit.Assert.assertEquals;
+import java.nio.charset.StandardCharsets;
+import org.junit.Assert;
import org.junit.Test;
/**
@@ -26,17 +27,34 @@ import org.junit.Test;
*/
public class TestHexUtils {
+ private static final String TEST01_STRING = "Hello World";
+ private static final byte[] TEST01_BYTES = TEST01_STRING.getBytes(StandardCharsets.UTF_8);
+ private static final String TEST02_STRING = "foo";
+ private static final byte[] TEST02_BYTES = TEST02_STRING.getBytes(StandardCharsets.UTF_8);
+
@Test
public void testGetDec() {
- assertEquals(0, HexUtils.getDec('0'));
- assertEquals(9, HexUtils.getDec('9'));
- assertEquals(10, HexUtils.getDec('a'));
- assertEquals(15, HexUtils.getDec('f'));
- assertEquals(10, HexUtils.getDec('A'));
- assertEquals(15, HexUtils.getDec('F'));
- assertEquals(-1, HexUtils.getDec(0));
- assertEquals(-1, HexUtils.getDec('Z'));
- assertEquals(-1, HexUtils.getDec(255));
- assertEquals(-1, HexUtils.getDec(-60));
+ Assert.assertEquals(0, HexUtils.getDec('0'));
+ Assert.assertEquals(9, HexUtils.getDec('9'));
+ Assert.assertEquals(10, HexUtils.getDec('a'));
+ Assert.assertEquals(15, HexUtils.getDec('f'));
+ Assert.assertEquals(10, HexUtils.getDec('A'));
+ Assert.assertEquals(15, HexUtils.getDec('F'));
+ Assert.assertEquals(-1, HexUtils.getDec(0));
+ Assert.assertEquals(-1, HexUtils.getDec('Z'));
+ Assert.assertEquals(-1, HexUtils.getDec(255));
+ Assert.assertEquals(-1, HexUtils.getDec(-60));
+ }
+
+ @Test
+ public void testRoundTrip01() {
+ Assert.assertArrayEquals(TEST01_STRING, TEST01_BYTES,
+ HexUtils.fromHexString(HexUtils.toHexString(TEST01_BYTES)));
+ }
+
+ @Test
+ public void testRoundTrip02() {
+ Assert.assertArrayEquals(TEST02_STRING, TEST02_BYTES,
+ HexUtils.fromHexString(HexUtils.toHexString(TEST02_BYTES)));
}
-}
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org