Skip to content

Commit 5c03146

Browse files
committed
see 08/13 log
1 parent 11b6267 commit 5c03146

15 files changed

+911
-348
lines changed

README.md

+13-5
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,27 @@
1616
> - 获取设备MAC地址 *getMacAddress*
1717
> - 获取设备厂商,如Xiaomi *getManufacturer*
1818
> - 获取设备型号,如MI2SC *getModel*
19+
1920
> - 获取设备SD卡是否可用 *isSDCardEnable*
2021
> - 获取设备SD卡路径 *getSDCardPath*
2122
2223
> - [编码解码相关][encode.md][EncodeUtils.java][encode.java]
23-
> - 以UTF-8编码字符串 *encodeUTF8*
24-
> - 字符编码 *encode*
25-
> - 以UTF-8解码字符串 *decodeUTF8*
26-
> - 字符解码 *decode*
24+
> - URL编码 *urlEncode*
25+
> - URL解码 *urlDecode*
26+
> - Base64编码 *base64Encode*
27+
> - Base64解码 *base64Decode*
28+
> - Base64URL安全编码 *base64UrlSafeEncode*
29+
> - Html编码 *htmlEncode*
30+
> - Html解码 *htmlDecode*
2731
2832
> - [加解密相关][encrypt.md][EncryptUtils.java][encrypt.java]
29-
> - MD5加密 *getMD5* *encryptMD5* *getMD5File*
33+
> - MD5加密 *getMD5* *encryptMD5*
34+
> - 获取文件的MD5校验码 *getMD5File*
3035
> - SHA加密 *getSHA* *encryptSHA*
3136
37+
> - [文件相关][file.md][FileUtils.java][file.java]
38+
> - 这几天完善ing
39+
3240
> - [键盘相关][keyboard.md][KeyboardUtils.java][keyboard.java]
3341
> - 避免输入法面板遮挡
3442
> - 动态隐藏软键盘 *hideSoftInput*

md/about_device.md

-19
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,5 @@ public class DeviceUtils {
8383
}
8484
return model;
8585
}
86-
87-
/**
88-
* 获取设备SD卡是否可用
89-
*
90-
* @return true : 可用<br>false : 不可用
91-
*/
92-
public static boolean isSDCardEnable() {
93-
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
94-
}
95-
96-
/**
97-
* 获取设备SD卡路径
98-
* <p>一般是/storage/emulated/0/</p>
99-
*
100-
* @return SD卡路径
101-
*/
102-
public static String getSDCardPath() {
103-
return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
104-
}
10586
}
10687
```

md/about_encode.md

+124-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# 编码解码相关
22
``` java
3+
import android.os.Build;
4+
import android.text.Html;
5+
import android.util.Base64;
6+
37
import java.io.UnsupportedEncodingException;
48
import java.net.URLDecoder;
59
import java.net.URLEncoder;
@@ -19,57 +23,153 @@ public class EncodeUtils {
1923
}
2024

2125
/**
22-
* 以UTF-8编码字符串
23-
* <p>若想自己指定字符集,可以使用encode(String string, String charset)方法</p>
26+
* URL编码
27+
* <p>若想自己指定字符集,可以使用{@link #urlEncode(String input, String charset)}方法</p>
2428
*
25-
* @param string 要编码的字符
29+
* @param input 要编码的字符
2630
* @return 编码为UTF-8的字符串
2731
*/
28-
public static String encodeUTF8(String string) {
29-
return encode(string, "UTF-8");
32+
public static String urlEncode(String input) {
33+
return urlEncode(input, "UTF-8");
3034
}
3135

3236
/**
33-
* 字符编码
34-
* <p>若系统不支持指定的编码字符集,则直接将string原样返回</p>
37+
* URL编码
38+
* <p>若系统不支持指定的编码字符集,则直接将input原样返回</p>
3539
*
36-
* @param string 要编码的字符
40+
* @param input 要编码的字符
3741
* @param charset 字符集
3842
* @return 编码为字符集的字符串
3943
*/
40-
public static String encode(String string, String charset) {
44+
public static String urlEncode(String input, String charset) {
4145
try {
42-
return URLEncoder.encode(string, charset);
46+
return URLEncoder.encode(input, charset);
4347
} catch (UnsupportedEncodingException e) {
44-
return string;
48+
return input;
4549
}
4650
}
4751

4852
/**
49-
* 以UTF-8解码字符串
50-
* <p>若想自己指定字符集,可以使用# {decode(String string, String charset)}方法</p>
53+
* URL解码
54+
* <p>若想自己指定字符集,可以使用 {@link #urlDecode(String input, String charset)}方法</p>
5155
*
52-
* @param string 要解码的字符
53-
* @return 解码为UTF-8的字符串
56+
* @param input 要解码的字符串
57+
* @return URL解码后的字符串
5458
*/
55-
public static String decodeUTF8(String string) {
56-
return decode(string, "UTF-8");
59+
public static String urlDecode(String input) {
60+
return urlDecode(input, "UTF-8");
5761
}
5862

5963
/**
60-
* 字符解码
61-
* <p>若系统不支持指定的解码字符集,则直接将string原样返回</p>
64+
* URL解码
65+
* <p>若系统不支持指定的解码字符集,则直接将input原样返回</p>
6266
*
63-
* @param string 要解码的字符
67+
* @param input 要解码的字符串
6468
* @param charset 字符集
65-
* @return 解码为字符集的字符串
69+
* @return URL解码为指定字符集的字符串
6670
*/
67-
public static String decode(String string, String charset) {
71+
public static String urlDecode(String input, String charset) {
6872
try {
69-
return URLDecoder.decode(string, charset);
73+
return URLDecoder.decode(input, charset);
7074
} catch (UnsupportedEncodingException e) {
71-
return string;
75+
return input;
76+
}
77+
}
78+
79+
/**
80+
* Base64编码
81+
*
82+
* @param input 要编码的字符串
83+
* @return Base64编码后的字符串
84+
*/
85+
public static String base64Encode(String input) {
86+
return base64Encode(input.getBytes());
87+
}
88+
89+
/**
90+
* Base64编码
91+
*
92+
* @param input 要编码的字节数组
93+
* @return Base64编码后的字符串
94+
*/
95+
public static String base64Encode(byte[] input) {
96+
return Base64.encodeToString(input, Base64.DEFAULT);
97+
}
98+
99+
/**
100+
* Base64解码
101+
*
102+
* @param input 要解码的字符串
103+
* @return Base64解码后的字符串
104+
*/
105+
public static String base64Decode(String input) {
106+
return new String(Base64.decode(input, Base64.DEFAULT));
107+
}
108+
109+
/**
110+
* Base64URL安全编码
111+
* <p>将Base64中的URL非法字符�?,/=转为其他字符, 见RFC3548</p>
112+
*
113+
* @param input 要Base64URL安全编码的字符串
114+
* @return Base64URL安全编码后的字符串
115+
*/
116+
public static String base64UrlSafeEncode(String input) {
117+
return Base64.encodeToString(input.getBytes(), Base64.URL_SAFE);
118+
}
119+
120+
/**
121+
* Html编码
122+
*
123+
* @param input 要Html编码的字符串
124+
* @return Html编码后的字符串
125+
*/
126+
public static String htmlEncode(String input) {
127+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
128+
return Html.escapeHtml(input);
129+
} else {
130+
// 参照Html.escapeHtml()中代码
131+
StringBuilder out = new StringBuilder();
132+
for (int i = 0, len = input.length(); i < len; i++) {
133+
char c = input.charAt(i);
134+
if (c == '<') {
135+
out.append("&lt;");
136+
} else if (c == '>') {
137+
out.append("&gt;");
138+
} else if (c == '&') {
139+
out.append("&amp;");
140+
} else if (c >= 0xD800 && c <= 0xDFFF) {
141+
if (c < 0xDC00 && i + 1 < len) {
142+
char d = input.charAt(i + 1);
143+
if (d >= 0xDC00 && d <= 0xDFFF) {
144+
i++;
145+
int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
146+
out.append("&#").append(codepoint).append(";");
147+
}
148+
}
149+
} else if (c > 0x7E || c < ' ') {
150+
out.append("&#").append((int) c).append(";");
151+
} else if (c == ' ') {
152+
while (i + 1 < len && input.charAt(i + 1) == ' ') {
153+
out.append("&nbsp;");
154+
i++;
155+
}
156+
out.append(' ');
157+
} else {
158+
out.append(c);
159+
}
160+
}
161+
return out.toString();
72162
}
73163
}
164+
165+
/**
166+
* Html解码
167+
*
168+
* @param input 待解码的字符串
169+
* @return Html解码后的字符串
170+
*/
171+
public static String htmlDecode(String input) {
172+
return Html.fromHtml(input).toString();
173+
}
74174
}
75175
```

md/about_file.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 编码解码相关
2+
``` java
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2016/8/11
8+
* desc : 文件相关的工具类
9+
* </pre>
10+
*/
11+
public class FileUtils {
12+
13+
private FileUtils() {
14+
throw new UnsupportedOperationException("u can't fuck me...");
15+
}
16+
17+
18+
}
19+
```

md/update_log.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 更新Log
2-
#### 16/08/12 新增Base64和Html编码解码及他们的单元测试,新增TimeUtils单元测试
2+
#### 16/08/13 新增MD2,SHA224,SHA256,SHA384,SHA512加密及单元测试,正折腾DES加密
3+
#### 16/08/12 新增Base64和Html编码解码及他们的单元测试,新增TimeUtils单元测试,更新md
34
#### 16/08/11 新增SDCardUtils,UnitUtils,单元测试慢慢完善中
45
#### 16/08/09 目录排版更新,新增Download,Proguard和License
56
#### 16/08/08 新增Shell工具类,已传jcenter()遇到好多坑,javaDoc惹的祸,注释一定要规范
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.blankj.utilcode.utils;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2016/8/13
8+
* desc : 转换相关工具类
9+
* </pre>
10+
*/
11+
public class ConvertUtils {
12+
13+
static final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
14+
15+
/**
16+
* 每1个byte转为2个hex字符
17+
* <p>例如:</p>
18+
* bytes2HexString(new byte[] { 0, (byte) 0xa8 }) returns 00A8
19+
*
20+
* @param src byte数组
21+
* @return 16进制大写字符串
22+
*/
23+
public static String bytes2HexString(byte[] src) {
24+
char[] res = new char[src.length << 1];
25+
for (int i = 0, j = 0; i < src.length; i++) {
26+
res[j++] = hexDigits[src[i] >>> 4 & 0x0f];
27+
res[j++] = hexDigits[src[i] & 0x0f];
28+
}
29+
return new String(res);
30+
}
31+
32+
/**
33+
* 每2个hex字符转为1个byte
34+
* <p>例如:</p>
35+
* bytes2HexString("00A8") returns { 0, (byte) 0xA8 }
36+
*
37+
* @param hexString 十六进制字符串
38+
* @return byte数组
39+
*/
40+
public static byte[] hexString2Bytes(String hexString) {
41+
int len = hexString.length();
42+
if (len % 2 != 0) {
43+
throw new IllegalArgumentException("长度不是偶数");
44+
}
45+
char[] hexBytes = hexString.toUpperCase().toCharArray();
46+
byte[] res = new byte[len / 2];
47+
for (int i = 0; i < len; i += 2) {
48+
res[i >> 1] = (byte) (hex2Dec(hexBytes[i]) << 4 | hex2Dec(hexBytes[i + 1]));
49+
}
50+
return res;
51+
}
52+
53+
/**
54+
* 单个hex字符转为10进制
55+
*
56+
* @param hexChar hex单个字节
57+
* @return 0..15
58+
*/
59+
private static int hex2Dec(char hexChar) {
60+
if (hexChar >= '0' && hexChar <= '9') {
61+
return hexChar - '0';
62+
} else if (hexChar >= 'A' && hexChar <= 'E') {
63+
return hexChar - 'A' + 10;
64+
} else {
65+
throw new IllegalArgumentException();
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)