Permalink
Browse files

Merge pull request #35 from wgallagher/hex

more efficient hex percent encoding
  • Loading branch information...
2 parents e9bbdfd + b6220d8 commit 7bea87395b29e1bec0d0989fb51ef44dae908ea6 Manoj Cheenath committed Feb 17, 2015
Showing with 7 additions and 3 deletions.
  1. +7 −3 src/main/java/com/twitter/joauth/UrlCodec.java
@@ -36,6 +36,10 @@
private static final String ENCODED_OPEN_BRACKET = "%5B";
private static final String ENCODED_CLOSE_BRACKET = "%5D";
+ private final static char[] HEX_DIGITS = {
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
+ };
+
private static boolean isUnreserved(byte b) {
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') ||
(b >= '0' && b <= '9') || b == '.' || b == '-' || b == '_' || b == '~';
@@ -77,9 +81,9 @@ public static String encode(String s) {
} else {
// turn the Byte into an int into the hex string, but be sure to mask out the unneeded bits
// to avoid nastiness with converting to a negative int
- sb.append("%")
- .append(Integer.toHexString((bite >> 4) & 0xF).toUpperCase())
- .append(Integer.toHexString(bite & 0xF).toUpperCase());
+ sb.append('%');
+ sb.append(HEX_DIGITS[(bite >> 4) & 0x0F]);
+ sb.append(HEX_DIGITS[bite & 0x0F]);
}
}
}

0 comments on commit 7bea873

Please sign in to comment.