Skip to content

Commit 5179c5e

Browse files
committed
Forward port PR AsyncHttpClient#357.
1 parent b414b6d commit 5179c5e

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

api/src/main/java/org/asynchttpclient/util/UTF8UrlEncoder.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
* (as per RFC-3986, see [http://www.ietf.org/rfc/rfc3986.txt]).
2121
*/
2222
public class UTF8UrlEncoder {
23-
private static final boolean encodeSpaceUsingPlus = System.getProperty("com.UTF8UrlEncoder.encodeSpaceUsingPlus") == null ? false : true;
23+
private static final boolean encodeSpaceUsingPlus =
24+
System.getProperty("com.UTF8UrlEncoder.encodeSpaceUsingPlus") != null;
2425

2526
/**
2627
* Encoding table used for figuring out ascii characters that must be escaped
27-
* (all non-Ascii characers need to be encoded anyway)
28+
* (all non-Ascii characters need to be encoded anyway)
2829
*/
2930
private final static int[] SAFE_ASCII = new int[128];
3031

@@ -58,11 +59,11 @@ public static String encode(String input) {
5859
public static StringBuilder appendEncoded(StringBuilder sb, String input) {
5960
final int[] safe = SAFE_ASCII;
6061

61-
for (int i = 0, len = input.length(); i < len; ++i) {
62-
char c = input.charAt(i);
62+
for (int c, i = 0, len = input.length(); i < len; i+= Character.charCount(c)) {
63+
c = input.codePointAt(i);
6364
if (c <= 127) {
6465
if (safe[c] != 0) {
65-
sb.append(c);
66+
sb.append((char) c);
6667
} else {
6768
appendSingleByteEncoded(sb, c);
6869
}
@@ -73,7 +74,7 @@ public static StringBuilder appendEncoded(StringBuilder sb, String input) {
7374
return sb;
7475
}
7576

76-
private final static void appendSingleByteEncoded(StringBuilder sb, int value) {
77+
private static void appendSingleByteEncoded(StringBuilder sb, int value) {
7778

7879
if (encodeSpaceUsingPlus && value == 32) {
7980
sb.append('+');
@@ -85,15 +86,19 @@ private final static void appendSingleByteEncoded(StringBuilder sb, int value) {
8586
sb.append(HEX[value & 0xF]);
8687
}
8788

88-
private final static void appendMultiByteEncoded(StringBuilder sb, int value) {
89-
// two or three bytes? (ignoring surrogate pairs for now, which would yield 4 bytes)
89+
private static void appendMultiByteEncoded(StringBuilder sb, int value) {
9090
if (value < 0x800) {
9191
appendSingleByteEncoded(sb, (0xc0 | (value >> 6)));
9292
appendSingleByteEncoded(sb, (0x80 | (value & 0x3f)));
93-
} else {
93+
} else if (value < 0x10000) {
9494
appendSingleByteEncoded(sb, (0xe0 | (value >> 12)));
9595
appendSingleByteEncoded(sb, (0x80 | ((value >> 6) & 0x3f)));
9696
appendSingleByteEncoded(sb, (0x80 | (value & 0x3f)));
97+
} else {
98+
appendSingleByteEncoded(sb, (0xf0 | (value >> 18)));
99+
appendSingleByteEncoded(sb, (0x80 | (value >> 12) & 0x3f));
100+
appendSingleByteEncoded(sb, (0x80 | (value >> 6) & 0x3f));
101+
appendSingleByteEncoded(sb, (0x80 | (value & 0x3f)));
97102
}
98103
}
99104

0 commit comments

Comments
 (0)