1616/**
1717 * URLEncoding is the alternate base64 encoding defined in RFC 4648. It is
1818 * typically used in URLs and file names.
19- *
19+ *
2020 */
2121public class EncodeUtils {
2222
2323 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 );
4125 }
42-
26+
4327 public static byte [] urlsafeBase64Decode (String encoded ){
4428 byte [] rawbs = toByte (encoded );
4529 for (int i =0 ;i <rawbs .length ;i ++){
@@ -51,7 +35,7 @@ public static byte[] urlsafeBase64Decode(String encoded){
5135 }
5236 return Base64 .decodeBase64 (rawbs );
5337 }
54-
38+
5539 public static String urlsafeEncodeString (byte [] src ) {
5640 return toString (urlsafeEncodeBytes (src ));
5741 }
@@ -91,15 +75,15 @@ public static String encodeParams(Object params) {
9175 }
9276 return null ;
9377 }
94-
78+
9579 public static byte [] toByte (String s ){
9680 try {
9781 return s .getBytes (Config .CHARSET );
9882 } catch (UnsupportedEncodingException e ) {
9983 throw new RuntimeException (e );
10084 }
10185 }
102-
86+
10387 public static String toString (byte [] bs ){
10488 try {
10589 return new String (bs , Config .CHARSET );
0 commit comments