16
16
/**
17
17
* URLEncoding is the alternate base64 encoding defined in RFC 4648. It is
18
18
* typically used in URLs and file names.
19
- *
19
+ *
20
20
*/
21
21
public class EncodeUtils {
22
22
23
23
public static byte [] urlsafeEncodeBytes (byte [] src ) {
24
- if (src .length % 3 == 0 ) {
25
- return encodeBase64Ex (src );
26
- }
27
-
28
- byte [] b = encodeBase64Ex (src );
29
- if (b .length % 4 == 0 ) {
30
- return b ;
31
- }
32
-
33
- int pad = 4 - b .length % 4 ;
34
- byte [] b2 = new byte [b .length + pad ];
35
- System .arraycopy (b , 0 , b2 , 0 , b .length );
36
- b2 [b .length ] = '=' ;
37
- if (pad > 1 ) {
38
- b2 [b .length + 1 ] = '=' ;
39
- }
40
- return b2 ;
24
+ return encodeBase64Ex (src );
41
25
}
42
-
26
+
43
27
public static byte [] urlsafeBase64Decode (String encoded ){
44
28
byte [] rawbs = toByte (encoded );
45
29
for (int i =0 ;i <rawbs .length ;i ++){
@@ -51,7 +35,7 @@ public static byte[] urlsafeBase64Decode(String encoded){
51
35
}
52
36
return Base64 .decodeBase64 (rawbs );
53
37
}
54
-
38
+
55
39
public static String urlsafeEncodeString (byte [] src ) {
56
40
return toString (urlsafeEncodeBytes (src ));
57
41
}
@@ -91,15 +75,15 @@ public static String encodeParams(Object params) {
91
75
}
92
76
return null ;
93
77
}
94
-
78
+
95
79
public static byte [] toByte (String s ){
96
80
try {
97
81
return s .getBytes (Config .CHARSET );
98
82
} catch (UnsupportedEncodingException e ) {
99
83
throw new RuntimeException (e );
100
84
}
101
85
}
102
-
86
+
103
87
public static String toString (byte [] bs ){
104
88
try {
105
89
return new String (bs , Config .CHARSET );
0 commit comments