-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
EncodeUtils.java
217 lines (200 loc) · 6.5 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.blankj.utilcode.util;
import android.os.Build;
import android.text.Html;
import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/08/07
* desc : utils about encode
* </pre>
*/
public final class EncodeUtils {
private EncodeUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return the urlencoded string.
*
* @param input The input.
* @return the urlencoded string
*/
public static String urlEncode(final String input) {
return urlEncode(input, "UTF-8");
}
/**
* Return the urlencoded string.
*
* @param input The input.
* @param charsetName The name of charset.
* @return the urlencoded string
*/
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);
}
}
/**
* Return the string of decode urlencoded string.
*
* @param input The input.
* @return the string of decode urlencoded string
*/
public static String urlDecode(final String input) {
return urlDecode(input, "UTF-8");
}
/**
* Return the string of decode urlencoded string.
*
* @param input The input.
* @param charsetName The name of charset.
* @return the string of decode urlencoded string
*/
public static String urlDecode(final String input, final String charsetName) {
if (input == null || input.length() == 0) return "";
try {
String safeInput = input.replaceAll("%(?![0-9a-fA-F]{2})", "%25").replaceAll("\\+", "%2B");
return URLDecoder.decode(safeInput, charsetName);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
/**
* Return Base64-encode bytes.
*
* @param input The input.
* @return Base64-encode bytes
*/
public static byte[] base64Encode(final String input) {
return base64Encode(input.getBytes());
}
/**
* Return Base64-encode bytes.
*
* @param input The input.
* @return Base64-encode bytes
*/
public static byte[] base64Encode(final byte[] input) {
if (input == null || input.length == 0) return new byte[0];
return Base64.encode(input, Base64.NO_WRAP);
}
/**
* Return Base64-encode string.
*
* @param input The input.
* @return Base64-encode string
*/
public static String base64Encode2String(final byte[] input) {
if (input == null || input.length == 0) return "";
return Base64.encodeToString(input, Base64.NO_WRAP);
}
/**
* Return the bytes of decode Base64-encode string.
*
* @param input The input.
* @return the string of decode Base64-encode string
*/
public static byte[] base64Decode(final String input) {
if (input == null || input.length() == 0) return new byte[0];
return Base64.decode(input, Base64.NO_WRAP);
}
/**
* Return the bytes of decode Base64-encode bytes.
*
* @param input The input.
* @return the bytes of decode Base64-encode bytes
*/
public static byte[] base64Decode(final byte[] input) {
if (input == null || input.length == 0) return new byte[0];
return Base64.decode(input, Base64.NO_WRAP);
}
/**
* Return html-encode string.
*
* @param input The input.
* @return html-encode string
*/
public static String htmlEncode(final CharSequence input) {
if (input == null || input.length() == 0) return "";
StringBuilder sb = new StringBuilder();
char c;
for (int i = 0, len = input.length(); i < len; i++) {
c = input.charAt(i);
switch (c) {
case '<':
sb.append("<"); //$NON-NLS-1$
break;
case '>':
sb.append(">"); //$NON-NLS-1$
break;
case '&':
sb.append("&"); //$NON-NLS-1$
break;
case '\'':
//http://www.w3.org/TR/xhtml1
// The named character reference ' (the apostrophe, U+0027) was
// introduced in XML 1.0 but does not appear in HTML. Authors should
// therefore use ' instead of ' to work as expected in HTML 4
// user agents.
sb.append("'"); //$NON-NLS-1$
break;
case '"':
sb.append("""); //$NON-NLS-1$
break;
default:
sb.append(c);
}
}
return sb.toString();
}
/**
* Return the string of decode html-encode string.
*
* @param input The input.
* @return the string of decode html-encode string
*/
public static CharSequence htmlDecode(final String input) {
if (input == null || input.length() == 0) return "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(input, Html.FROM_HTML_MODE_LEGACY);
} else {
return Html.fromHtml(input);
}
}
/**
* Return the binary encoded string padded with one space
*
* @param input The input.
* @return binary string
*/
public static String binaryEncode(final String input) {
if (input == null || input.length() == 0) return "";
StringBuilder sb = new StringBuilder();
for (char i : input.toCharArray()) {
sb.append(Integer.toBinaryString(i)).append(" ");
}
return sb.deleteCharAt(sb.length() - 1).toString();
}
/**
* Return UTF-8 String from binary
*
* @param input binary string
* @return UTF-8 String
*/
public static String binaryDecode(final String input) {
if (input == null || input.length() == 0) return "";
String[] splits = input.split(" ");
StringBuilder sb = new StringBuilder();
for (String split : splits) {
sb.append(((char) Integer.parseInt(split, 2)));
}
return sb.toString();
}
}