commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1701126 - in /commons/proper/codec/trunk/src: changes/changes.xml main/java/org/apache/commons/codec/binary/Hex.java test/java/org/apache/commons/codec/binary/Base64Test.java test/java/org/apache/commons/codec/binary/HexTest.java
Date Thu, 03 Sep 2015 20:16:12 GMT
Author: ggregory
Date: Thu Sep  3 20:16:11 2015
New Revision: 1701126

URL: http://svn.apache.org/r1701126
Log:
[CODEC-203] Add convenience method decodeHex(String).

Modified:
    commons/proper/codec/trunk/src/changes/changes.xml
    commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java
    commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64Test.java
    commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java

Modified: commons/proper/codec/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/changes/changes.xml?rev=1701126&r1=1701125&r2=1701126&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/changes/changes.xml (original)
+++ commons/proper/codec/trunk/src/changes/changes.xml Thu Sep  3 20:16:11 2015
@@ -49,7 +49,8 @@ The <action> type attribute can be add,u
       <action dev="ggregory" type="add" issue="CODEC-195" due-to="Gary Gregory">Support
SHA-224 in DigestUtils on Java 8</action>         
       <action dev="ggregory" type="add" issue="CODEC-194" due-to="Gary Gregory">Support
java.nio.ByteBuffer in org.apache.commons.codec.binary.Hex</action>         
       <action dev="ggregory" type="add" issue="CODEC-193" due-to="Michael Donaghy">Support
java.nio.ByteBuffer in DigestUtils</action>         
-      <action dev="ggregory" type="add" issue="CODEC-202" due-to="Oleg Kalnichevski">Add
BaseNCodec.encode(byte[], int, int) input with offset and length parameters for Base64 and
Base32.</action>         
+      <action dev="ggregory" type="add" issue="CODEC-202" due-to="Oleg Kalnichevski">Add
BaseNCodec.encode(byte[], int, int) input with offset and length parameters for Base64 and
Base32.</action>
+      <action dev="ggregory" type="add" issue="CODEC-203" due-to="Gary Gregory">Add
convenience method decodeHex(String).</action>
     </release>   
     <release version="1.10" date="5 November 2014" description="Feature and fix release.">
       <action dev="ggregory" type="add" issue="CODEC-192" due-to="Thomas Neidhart">Add
Daitch-Mokotoff Soundex</action>   

Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java?rev=1701126&r1=1701125&r2=1701126&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java (original)
+++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java Thu
Sep  3 20:16:11 2015
@@ -65,6 +65,22 @@ public class Hex implements BinaryEncode
         {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 
     /**
+     * Converts a String representing hexadecimal values into an array of bytes of those
same values. The
+     * returned array will be half the length of the passed String, as it takes two characters
to represent any given
+     * byte. An exception is thrown if the passed String has an odd number of elements.
+     *
+     * @param data
+     *            A String containing hexadecimal digits
+     * @return A byte array containing binary data decoded from the supplied char array.
+     * @throws DecoderException
+     *             Thrown if an odd number or illegal of characters is supplied
+     * @since 1.11
+     */
+    public static byte[] decodeHex(String data) throws DecoderException {
+        return decodeHex(data.toCharArray());
+    }
+
+    /**
      * Converts an array of characters representing hexadecimal values into an array of bytes
of those same values. The
      * returned array will be half the length of the passed array, as it takes two characters
to represent any given
      * byte. An exception is thrown if the passed char array has an odd number of elements.

Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64Test.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64Test.java?rev=1701126&r1=1701125&r2=1701126&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64Test.java
(original)
+++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64Test.java
Thu Sep  3 20:16:11 2015
@@ -1154,17 +1154,17 @@ public class Base64Test {
 		final byte[][] ids = new byte[4][];
 
 		// ids[0] was chosen so that it encodes with at least one +.
-		ids[0] = Hex.decodeHex("94ed8d0319e4493399560fb67404d370".toCharArray());
+		ids[0] = Hex.decodeHex("94ed8d0319e4493399560fb67404d370");
 
 		// ids[1] was chosen so that it encodes with both / and +.
-		ids[1] = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090".toCharArray());
+		ids[1] = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090");
 
 		// ids[2] was chosen so that it encodes with at least one /.
-		ids[2] = Hex.decodeHex("64be154b6ffa40258d1a01288e7c31ca".toCharArray());
+		ids[2] = Hex.decodeHex("64be154b6ffa40258d1a01288e7c31ca");
 
 		// ids[3] was chosen so that it encodes with both / and +, with /
 		// right at the beginning.
-		ids[3] = Hex.decodeHex("ff7f8fc01cdb471a8c8b5a9306183fe8".toCharArray());
+		ids[3] = Hex.decodeHex("ff7f8fc01cdb471a8c8b5a9306183fe8");
 
 		final byte[][] standard = new byte[4][];
 		standard[0] = StringUtils.getBytesUtf8("lO2NAxnkSTOZVg+2dATTcA==");

Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java?rev=1701126&r1=1701125&r2=1701126&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java
(original)
+++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java
Thu Sep  3 20:16:11 2015
@@ -96,9 +96,6 @@ public class HexTest {
         }
     }
 
-    /**
-     * @param data
-     */
     private void checkDecodeHexCharArrayOddCharacters(final char[] data) {
         try {
             Hex.decodeHex(data);
@@ -108,6 +105,15 @@ public class HexTest {
         }
     }
 
+    private void checkDecodeHexCharArrayOddCharacters(String data) {
+        try {
+            Hex.decodeHex(data);
+            fail("An exception wasn't thrown when trying to decode an odd number of characters");
+        } catch (final DecoderException e) {
+            // Expected exception
+        }
+    }
+
     private void log(final String s) {
         if (LOG) {
             System.out.println(s);
@@ -243,11 +249,16 @@ public class HexTest {
     }
 
     @Test
-    public void testDecodeCharArrayEmpty() throws DecoderException {
+    public void testDecodeHexCharArrayEmpty() throws DecoderException {
         assertTrue(Arrays.equals(new byte[0], Hex.decodeHex(new char[0])));
     }
 
     @Test
+    public void testDecodeHexStringEmpty() throws DecoderException {
+        assertTrue(Arrays.equals(new byte[0], Hex.decodeHex("")));
+    }
+
+    @Test
     public void testDecodeClassCastException() {
         try {
             new Hex().decode(new int[] { 65 });
@@ -263,6 +274,11 @@ public class HexTest {
     }
 
     @Test
+    public void testDecodeHexStringOddCharacters1() {
+        checkDecodeHexCharArrayOddCharacters("A");
+    }
+
+    @Test
     public void testDecodeHexCharArrayOddCharacters3() {
         checkDecodeHexCharArrayOddCharacters(new char[] { 'A', 'B', 'C' });
     }
@@ -318,7 +334,7 @@ public class HexTest {
     }
 
     @Test
-    public void testEncodeDecodeRandom() throws DecoderException, EncoderException {
+    public void testEncodeDecodeHexCharArrayRandom() throws DecoderException, EncoderException
{
         final Random random = new Random();
 
         final Hex hex = new Hex();



Mime
View raw message