20
20
* (as per RFC-3986, see [http://www.ietf.org/rfc/rfc3986.txt]).
21
21
*/
22
22
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 ;
24
25
25
26
/**
26
27
* 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)
28
29
*/
29
30
private final static int [] SAFE_ASCII = new int [128 ];
30
31
@@ -58,11 +59,11 @@ public static String encode(String input) {
58
59
public static StringBuilder appendEncoded (StringBuilder sb , String input ) {
59
60
final int [] safe = SAFE_ASCII ;
60
61
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 );
63
64
if (c <= 127 ) {
64
65
if (safe [c ] != 0 ) {
65
- sb .append (c );
66
+ sb .append (( char ) c );
66
67
} else {
67
68
appendSingleByteEncoded (sb , c );
68
69
}
@@ -73,7 +74,7 @@ public static StringBuilder appendEncoded(StringBuilder sb, String input) {
73
74
return sb ;
74
75
}
75
76
76
- private final static void appendSingleByteEncoded (StringBuilder sb , int value ) {
77
+ private static void appendSingleByteEncoded (StringBuilder sb , int value ) {
77
78
78
79
if (encodeSpaceUsingPlus && value == 32 ) {
79
80
sb .append ('+' );
@@ -85,15 +86,19 @@ private final static void appendSingleByteEncoded(StringBuilder sb, int value) {
85
86
sb .append (HEX [value & 0xF ]);
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)
89
+ private static void appendMultiByteEncoded (StringBuilder sb , int value ) {
90
90
if (value < 0x800 ) {
91
91
appendSingleByteEncoded (sb , (0xc0 | (value >> 6 )));
92
92
appendSingleByteEncoded (sb , (0x80 | (value & 0x3f )));
93
- } else {
93
+ } else if ( value < 0x10000 ) {
94
94
appendSingleByteEncoded (sb , (0xe0 | (value >> 12 )));
95
95
appendSingleByteEncoded (sb , (0x80 | ((value >> 6 ) & 0x3f )));
96
96
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 )));
97
102
}
98
103
}
99
104
0 commit comments