@@ -24,7 +24,7 @@ public class UTF8UrlEncoder {
24
24
25
25
/**
26
26
* Encoding table used for figuring out ascii characters that must be escaped
27
- * (all non-Ascii characers need to be encoded anyway)
27
+ * (all non-Ascii characters need to be encoded anyway)
28
28
*/
29
29
private final static int [] SAFE_ASCII = new int [128 ];
30
30
@@ -58,11 +58,11 @@ public static String encode(String input) {
58
58
public static StringBuilder appendEncoded (StringBuilder sb , String input ) {
59
59
final int [] safe = SAFE_ASCII ;
60
60
61
- for (int i = 0 , len = input .length (); i < len ; ++ i ) {
62
- char c = input .charAt (i );
61
+ for (int c , i = 0 , len = input .length (); i < len ; i += Character . charCount ( c ) ) {
62
+ c = input .codePointAt (i );
63
63
if (c <= 127 ) {
64
64
if (safe [c ] != 0 ) {
65
- sb .append (c );
65
+ sb .append (( char ) c );
66
66
} else {
67
67
appendSingleByteEncoded (sb , c );
68
68
}
@@ -86,14 +86,18 @@ private final static void appendSingleByteEncoded(StringBuilder sb, int value) {
86
86
}
87
87
88
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)
90
89
if (value < 0x800 ) {
91
90
appendSingleByteEncoded (sb , (0xc0 | (value >> 6 )));
92
91
appendSingleByteEncoded (sb , (0x80 | (value & 0x3f )));
93
- } else {
92
+ } else if ( value < 0x10000 ) {
94
93
appendSingleByteEncoded (sb , (0xe0 | (value >> 12 )));
95
94
appendSingleByteEncoded (sb , (0x80 | ((value >> 6 ) & 0x3f )));
96
95
appendSingleByteEncoded (sb , (0x80 | (value & 0x3f )));
96
+ } else {
97
+ appendSingleByteEncoded (sb , (0xf0 | (value >> 18 )));
98
+ appendSingleByteEncoded (sb , (0x80 | (value >> 12 ) & 0x3f ));
99
+ appendSingleByteEncoded (sb , (0x80 | (value >> 6 ) & 0x3f ));
100
+ appendSingleByteEncoded (sb , (0x80 | (value & 0x3f )));
97
101
}
98
102
}
99
103
0 commit comments