Skip to content

Commit 9e8eec7

Browse files
committed
Centralise conversion to hex digit
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1788792 13f79535-47bb-0310-9956-ffa450edef68
1 parent 5ef5bd1 commit 9e8eec7

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ public QuotedPrintableCodec(final String charsetName)
181181
*/
182182
private static final int encodeQuotedPrintable(final int b, final ByteArrayOutputStream buffer) {
183183
buffer.write(ESCAPE_CHAR);
184-
final char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
185-
final char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
184+
final char hex1 = Utils.hexDigit(b >> 4);
185+
final char hex2 = Utils.hexDigit(b);
186186
buffer.write(hex1);
187187
buffer.write(hex2);
188188
return 3;

src/main/java/org/apache/commons/codec/net/URLCodec.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@
4747
*/
4848
public class URLCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder {
4949

50-
/**
51-
* Radix used in encoding and decoding.
52-
*/
53-
static final int RADIX = 16;
54-
5550
/**
5651
* The default charset used for string decoding and encoding.
5752
*
@@ -147,8 +142,8 @@ public static final byte[] encodeUrl(BitSet urlsafe, final byte[] bytes) {
147142
buffer.write(b);
148143
} else {
149144
buffer.write(ESCAPE_CHAR);
150-
final char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
151-
final char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
145+
final char hex1 = Utils.hexDigit(b >> 4);
146+
final char hex2 = Utils.hexDigit(b);
152147
buffer.write(hex1);
153148
buffer.write(hex2);
154149
}

src/main/java/org/apache/commons/codec/net/Utils.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
*/
3030
class Utils {
3131

32+
/**
33+
* Radix used in encoding and decoding.
34+
*/
35+
private static final int RADIX = 16;
36+
3237
/**
3338
* Returns the numeric value of the character <code>b</code> in radix 16.
3439
*
@@ -40,11 +45,21 @@ class Utils {
4045
* Thrown when the byte is not valid per {@link Character#digit(char,int)}
4146
*/
4247
static int digit16(final byte b) throws DecoderException {
43-
final int i = Character.digit((char) b, URLCodec.RADIX);
48+
final int i = Character.digit((char) b, RADIX);
4449
if (i == -1) {
45-
throw new DecoderException("Invalid URL encoding: not a valid digit (radix " + URLCodec.RADIX + "): " + b);
50+
throw new DecoderException("Invalid URL encoding: not a valid digit (radix " + RADIX + "): " + b);
4651
}
4752
return i;
4853
}
4954

55+
/**
56+
* Returns the upper case hex digit of the lower 4 bits of the int.
57+
*
58+
* @param b the input int
59+
* @return the upper case hex digit of the lower 4 bits of the int.
60+
*/
61+
static char hexDigit(int b) {
62+
return Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
63+
}
64+
5065
}

0 commit comments

Comments
 (0)