Author: sebb
Date: Sun Mar 26 23:57:00 2017
New Revision: 1788792

URL: http://svn.apache.org/viewvc?rev=1788792&view=rev
Log:
Centralise conversion to hex digit

Modified:
    
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
    
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/URLCodec.java
    
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/Utils.java

Modified: 
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
URL: 
http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java?rev=1788792&r1=1788791&r2=1788792&view=diff
==============================================================================
--- 
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
 (original)
+++ 
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
 Sun Mar 26 23:57:00 2017
@@ -181,8 +181,8 @@ public class QuotedPrintableCodec implem
      */
     private static final int encodeQuotedPrintable(final int b, final 
ByteArrayOutputStream buffer) {
         buffer.write(ESCAPE_CHAR);
-        final char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 
0xF, 16));
-        final char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 
16));
+        final char hex1 = Utils.hexDigit(b >> 4);
+        final char hex2 = Utils.hexDigit(b);
         buffer.write(hex1);
         buffer.write(hex2);
         return 3;

Modified: 
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/URLCodec.java
URL: 
http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/URLCodec.java?rev=1788792&r1=1788791&r2=1788792&view=diff
==============================================================================
--- 
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/URLCodec.java
 (original)
+++ 
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/URLCodec.java
 Sun Mar 26 23:57:00 2017
@@ -48,11 +48,6 @@ import org.apache.commons.codec.binary.S
 public class URLCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, 
StringDecoder {
 
     /**
-     * Radix used in encoding and decoding.
-     */
-    static final int RADIX = 16;
-
-    /**
      * The default charset used for string decoding and encoding.
      *
      * @deprecated TODO: This field will be changed to a private final Charset 
in 2.0. (CODEC-126)
@@ -147,8 +142,8 @@ public class URLCodec implements BinaryE
                 buffer.write(b);
             } else {
                 buffer.write(ESCAPE_CHAR);
-                final char hex1 = Character.toUpperCase(Character.forDigit((b 
>> 4) & 0xF, RADIX));
-                final char hex2 = Character.toUpperCase(Character.forDigit(b & 
0xF, RADIX));
+                final char hex1 = Utils.hexDigit(b >> 4);
+                final char hex2 = Utils.hexDigit(b);
                 buffer.write(hex1);
                 buffer.write(hex2);
             }

Modified: 
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/Utils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/Utils.java?rev=1788792&r1=1788791&r2=1788792&view=diff
==============================================================================
--- 
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/Utils.java
 (original)
+++ 
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/Utils.java
 Sun Mar 26 23:57:00 2017
@@ -30,6 +30,11 @@ import org.apache.commons.codec.DecoderE
 class Utils {
 
     /**
+     * Radix used in encoding and decoding.
+     */
+    private static final int RADIX = 16;
+
+    /**
      * Returns the numeric value of the character <code>b</code> in radix 16.
      *
      * @param b
@@ -40,11 +45,21 @@ class Utils {
      *             Thrown when the byte is not valid per {@link 
Character#digit(char,int)}
      */
     static int digit16(final byte b) throws DecoderException {
-        final int i = Character.digit((char) b, URLCodec.RADIX);
+        final int i = Character.digit((char) b, RADIX);
         if (i == -1) {
-            throw new DecoderException("Invalid URL encoding: not a valid 
digit (radix " + URLCodec.RADIX + "): " + b);
+            throw new DecoderException("Invalid URL encoding: not a valid 
digit (radix " + RADIX + "): " + b);
         }
         return i;
     }
 
+    /**
+     * Returns the upper case hex digit of the lower 4 bits of the int.
+     *
+     * @param b the input int
+     * @return the upper case hex digit of the lower 4 bits of the int.
+     */
+    static char hexDigit(int b) {
+        return Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
+    }
+
 }


Reply via email to