-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEncodeUtils.java
74 lines (58 loc) · 2.35 KB
/
EncodeUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package me.shouheng.utils.data;
import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* @author Shouheng Wang (shouheng2020@gmail.com)
* @version 2019/5/12 15:50
*/
public final class EncodeUtils {
/*---------------------------------- URL --------------------------------------*/
public static String urlEncode(final String input) {
return urlEncode(input, "UTF-8");
}
public static String urlEncode(final String input, final String charsetName) {
if (input == null || input.length() == 0) return "";
try {
return URLEncoder.encode(input, charsetName);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
public static String urlDecode(final String input) {
return urlDecode(input, "UTF-8");
}
public static String urlDecode(final String input, final String charsetName) {
if (input == null || input.length() == 0) return "";
try {
return URLDecoder.decode(input, charsetName);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
/*---------------------------------- Base64 --------------------------------------*/
public static byte[] base64Encode(final String input) {
return base64Encode(input.getBytes());
}
public static byte[] base64Encode(final byte[] input) {
if (input == null || input.length == 0) return new byte[0];
return Base64.encode(input, Base64.NO_WRAP);
}
public static String base64Encode2String(final byte[] input) {
if (input == null || input.length == 0) return "";
return Base64.encodeToString(input, Base64.NO_WRAP);
}
public static byte[] base64Decode(final String input) {
if (input == null || input.length() == 0) return new byte[0];
return Base64.decode(input, Base64.NO_WRAP);
}
public static byte[] base64Decode(final byte[] input) {
if (input == null || input.length == 0) return new byte[0];
return Base64.decode(input, Base64.NO_WRAP);
}
/*---------------------------------- 内部方法 --------------------------------------*/
private EncodeUtils() {
throw new UnsupportedOperationException("u can't initialize me!");
}
}