-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEncodeUtils.java
More file actions
192 lines (175 loc) · 5.61 KB
/
EncodeUtils.java
File metadata and controls
192 lines (175 loc) · 5.61 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.dds.common.utils;
import android.os.Build;
import android.text.Html;
import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
public class EncodeUtils {
private EncodeUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* URL编码
* <p>若想自己指定字符集,可以使用{@link #urlEncode(String input, String charset)}方法</p>
*
* @param input 要编码的字符
* @return 编码为UTF-8的字符串
*/
public static String urlEncode(String input) {
return urlEncode(input, "UTF-8");
}
/**
* URL编码
* <p>若系统不支持指定的编码字符集,则直接将input原样返回</p>
*
* @param input 要编码的字符
* @param charset 字符集
* @return 编码为字符集的字符串
*/
public static String urlEncode(String input, String charset) {
try {
return URLEncoder.encode(input, charset);
} catch (UnsupportedEncodingException e) {
return input;
}
}
/**
* URL解码
* <p>若想自己指定字符集,可以使用 {@link #urlDecode(String input, String charset)}方法</p>
*
* @param input 要解码的字符串
* @return URL解码后的字符串
*/
public static String urlDecode(String input) {
return urlDecode(input, "UTF-8");
}
/**
* URL解码
* <p>若系统不支持指定的解码字符集,则直接将input原样返回</p>
*
* @param input 要解码的字符串
* @param charset 字符集
* @return URL解码为指定字符集的字符串
*/
public static String urlDecode(String input, String charset) {
try {
return URLDecoder.decode(input, charset);
} catch (UnsupportedEncodingException e) {
return input;
}
}
/**
* Base64编码
*
* @param input 要编码的字符串
* @return Base64编码后的字符串
*/
public static byte[] base64Encode(String input) {
return base64Encode(input.getBytes());
}
/**
* Base64编码
*
* @param input 要编码的字节数组
* @return Base64编码后的字符串
*/
public static byte[] base64Encode(byte[] input) {
return Base64.encode(input, Base64.NO_WRAP);
}
/**
* Base64编码
*
* @param input 要编码的字节数组
* @return Base64编码后的字符串
*/
public static String base64Encode2String(byte[] input) {
return Base64.encodeToString(input, Base64.NO_WRAP);
}
/**
* Base64解码
*
* @param input 要解码的字符串
* @return Base64解码后的字符串
*/
public static byte[] base64Decode(String input) {
return Base64.decode(input, Base64.NO_WRAP);
}
/**
* Base64解码
*
* @param input 要解码的字符串
* @return Base64解码后的字符串
*/
public static byte[] base64Decode(byte[] input) {
return Base64.decode(input, Base64.NO_WRAP);
}
/**
* Base64URL安全编码
* <p>将Base64中的URL非法字符�?,/=转为其他字符, 见RFC3548</p>
*
* @param input 要Base64URL安全编码的字符串
* @return Base64URL安全编码后的字符串
*/
public static byte[] base64UrlSafeEncode(String input) {
return Base64.encode(input.getBytes(), Base64.URL_SAFE);
}
/**
* Html编码
*
* @param input 要Html编码的字符串
* @return Html编码后的字符串
*/
public static String htmlEncode(CharSequence input) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return Html.escapeHtml(input);
} else {
// 参照Html.escapeHtml()中代码
StringBuilder out = new StringBuilder();
for (int i = 0, len = input.length(); i < len; i++) {
char c = input.charAt(i);
if (c == '<') {
out.append("<");
} else if (c == '>') {
out.append(">");
} else if (c == '&') {
out.append("&");
} else if (c >= 0xD800 && c <= 0xDFFF) {
if (c < 0xDC00 && i + 1 < len) {
char d = input.charAt(i + 1);
if (d >= 0xDC00 && d <= 0xDFFF) {
i++;
int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
out.append("&#").append(codepoint).append(";");
}
}
} else if (c > 0x7E || c < ' ') {
out.append("&#").append((int) c).append(";");
} else if (c == ' ') {
while (i + 1 < len && input.charAt(i + 1) == ' ') {
out.append(" ");
i++;
}
out.append(' ');
} else {
out.append(c);
}
}
return out.toString();
}
}
/**
* Html解码
*
* @param input 待解码的字符串
* @return Html解码后的字符串
*/
@SuppressWarnings("deprecation")
public static CharSequence htmlDecode(String input) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(input, Html.FROM_HTML_MODE_LEGACY);
} else {
return Html.fromHtml(input);
}
}
}