Skip to content

Commit 0b85765

Browse files
committed
see 08/27 log
1 parent 3b9326b commit 0b85765

File tree

7 files changed

+348
-40
lines changed

7 files changed

+348
-40
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
> - 正则相关常量
1919
2020
> - **转换相关→[ConvertUtils.java][convert.java][Test][convert.test]**
21-
> - 每1个byte转为2个hex字符 *bytes2HexString*
22-
> - 每2个hex字符转为1个byte *hexString2Bytes*
21+
> - byteArr转hexString *bytes2HexString*
22+
> - hexString转byteArr *hexString2Bytes*
2323
> - charArr转byteArr *chars2Bytes*
2424
> - byteArr转charArr *bytes2Chars*
25-
> - 将输入流转为字节数组 *inputStream2Bytes*
26-
> - 将字节数组转为输入流 *bytes2InputStream*
27-
> - 指定编码将输入流转为字符串 *inputStream2String*
28-
> - 指定编码将字符串转为输入流 *string2InputStream*
25+
> - inputStream转byteArr *inputStream2Bytes*
26+
> - byteArr转inputStream *bytes2InputStream*
27+
> - inputStream转string按编码 *inputStream2String*
28+
> - string转inputStream按编码 *string2InputStream*
29+
> - bitmap转byteArr *bitmap2Bytes*
30+
> - byteArr转bitmap *bytes2Bitmap*
31+
> - drawable转bitmap *drawable2Bitmap*
32+
> - bitmap转drawable *bitmap2Drawable*
2933
3034
> - **设备相关→[DeviceUtils.java][device.java]**
3135
> - 获取设备MAC地址 *getMacAddress*

utilcode/src/main/java/com/blankj/utilcode/utils/ConvertUtils.java

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.blankj.utilcode.utils;
22

3+
import android.content.res.Resources;
4+
import android.graphics.Bitmap;
5+
import android.graphics.BitmapFactory;
6+
import android.graphics.drawable.BitmapDrawable;
7+
import android.graphics.drawable.Drawable;
8+
39
import java.io.BufferedReader;
410
import java.io.ByteArrayInputStream;
511
import java.io.ByteArrayOutputStream;
@@ -21,24 +27,24 @@ public class ConvertUtils {
2127
static final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
2228

2329
/**
24-
* 每1个byte转为2个hex字符
30+
* byteArr转hexString
2531
* <p>例如:</p>
2632
* bytes2HexString(new byte[] { 0, (byte) 0xa8 }) returns 00A8
2733
*
28-
* @param src byte数组
34+
* @param bytes byte数组
2935
* @return 16进制大写字符串
3036
*/
31-
public static String bytes2HexString(byte[] src) {
32-
char[] res = new char[src.length << 1];
33-
for (int i = 0, j = 0; i < src.length; i++) {
34-
res[j++] = hexDigits[src[i] >>> 4 & 0x0f];
35-
res[j++] = hexDigits[src[i] & 0x0f];
37+
public static String bytes2HexString(byte[] bytes) {
38+
char[] res = new char[bytes.length << 1];
39+
for (int i = 0, j = 0; i < bytes.length; i++) {
40+
res[j++] = hexDigits[bytes[i] >>> 4 & 0x0f];
41+
res[j++] = hexDigits[bytes[i] & 0x0f];
3642
}
3743
return new String(res);
3844
}
3945

4046
/**
41-
* 每2个hex字符转为1个byte
47+
* hexString转byteArr
4248
* <p>例如:</p>
4349
* hexString2Bytes("00A8") returns { 0, (byte) 0xA8 }
4450
*
@@ -59,7 +65,7 @@ public static byte[] hexString2Bytes(String hexString) {
5965
}
6066

6167
/**
62-
* 单个hex字符转为10进制
68+
* hexChar转int
6369
*
6470
* @param hexChar hex单个字节
6571
* @return 0..15
@@ -105,7 +111,7 @@ public static char[] bytes2Chars(byte[] bytes) {
105111
}
106112

107113
/**
108-
* 将输入流转为字节数组
114+
* inputStream转byteArr
109115
*
110116
* @param is 输入流
111117
* @return 字节数组
@@ -129,7 +135,7 @@ public static byte[] inputStream2Bytes(InputStream is) {
129135
}
130136

131137
/**
132-
* 将字节数组转为输入流
138+
* byteArr转inputStream
133139
*
134140
* @param bytes 字节数组
135141
* @return 输入流
@@ -139,7 +145,7 @@ public static InputStream bytes2InputStream(byte[] bytes) {
139145
}
140146

141147
/**
142-
* 指定编码将输入流转为字符串
148+
* inputStream转string按编码
143149
*
144150
* @param is 输入流
145151
* @param charsetName 编码格式
@@ -170,7 +176,7 @@ public static String inputStream2String(InputStream is, String charsetName) {
170176
}
171177

172178
/**
173-
* 指定编码将字符串转为输入流
179+
* string转inputStream按编码
174180
*
175181
* @param string 字符串
176182
* @param charsetName 编码格式
@@ -185,4 +191,49 @@ public static InputStream string2InputStream(String string, String charsetName)
185191
return null;
186192
}
187193
}
194+
195+
/**
196+
* bitmap转byteArr
197+
*
198+
* @param bitmap bitmap对象
199+
* @param format 图片格式
200+
* @return 字节数组
201+
*/
202+
public static byte[] bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat format) {
203+
if (bitmap == null) return null;
204+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
205+
bitmap.compress(format, 100, baos);
206+
return baos.toByteArray();
207+
}
208+
209+
/**
210+
* byteArr转bitmap
211+
*
212+
* @param bytes 字节数组
213+
* @return bitmap对象
214+
*/
215+
public static Bitmap bytes2Bitmap(byte[] bytes) {
216+
return (bytes == null || bytes.length == 0) ? null : BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
217+
}
218+
219+
/**
220+
* drawable转bitmap
221+
*
222+
* @param drawable drawable对象
223+
* @return bitmap对象
224+
*/
225+
public static Bitmap drawable2Bitmap(Drawable drawable) {
226+
return drawable == null ? null : ((BitmapDrawable) drawable).getBitmap();
227+
}
228+
229+
/**
230+
* bitmap转drawable
231+
*
232+
* @param resources resources对象
233+
* @param bitmap bitmap对象
234+
* @return drawable对象
235+
*/
236+
public static Drawable bitmap2Drawable(Resources resources, Bitmap bitmap) {
237+
return bitmap == null ? null : new BitmapDrawable(resources, bitmap);
238+
}
188239
}

utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,7 @@ public static boolean writeFileFromIS(File file, InputStream is, boolean append)
655655
e.printStackTrace();
656656
return false;
657657
} finally {
658-
closeIO(is);
659-
closeIO(os);
658+
closeIO(is, os);
660659
}
661660
}
662661

@@ -961,12 +960,16 @@ public static double getFileSize(File file, MemoryUnit unit) {
961960
/**
962961
* 关闭IO
963962
*
964-
* @param closeable closeable
963+
* @param closeables closeable
965964
*/
966-
public static void closeIO(Closeable closeable) {
967-
if (closeable == null) return;
965+
public static void closeIO(Closeable... closeables) {
966+
if (closeables == null) return;
968967
try {
969-
closeable.close();
968+
for (Closeable closeable : closeables) {
969+
if (closeable != null) {
970+
closeable.close();
971+
}
972+
}
970973
} catch (IOException e) {
971974
e.printStackTrace();
972975
}

utilcode/src/main/java/com/blankj/utilcode/utils/SDCardUtils.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import java.io.File;
77

8+
import static com.blankj.utilcode.utils.ConstUtils.*;
9+
810
/**
911
* <pre>
1012
* author: Blankj
@@ -61,14 +63,14 @@ public static String getRootDirectoryPath() {
6163
* 计算SD卡的剩余空间
6264
*
6365
* @param unit <ul>
64-
* <li>{@link ConstUtils#BYTE}: 字节</li>
65-
* <li>{@link ConstUtils#KB} : 千字节</li>
66-
* <li>{@link ConstUtils#MB} : 兆</li>
67-
* <li>{@link ConstUtils#GB} : GB</li>
66+
* <li>{@link MemoryUnit#BYTE}: 字节</li>
67+
* <li>{@link MemoryUnit#KB} : 千字节</li>
68+
* <li>{@link MemoryUnit#MB} : 兆</li>
69+
* <li>{@link MemoryUnit#GB} : GB</li>
6870
* </ul>
6971
* @return 返回-1,说明SD卡不可用,否则返回SD卡剩余空间
7072
*/
71-
public static double getFreeSpace(int unit) {
73+
public static double getFreeSpace(MemoryUnit unit) {
7274
if (isSDCardEnable()) {
7375
try {
7476
StatFs stat = new StatFs(getSDCardPath());

utilcode/src/main/java/com/blankj/utilcode/utils/ThreadPoolUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public void execute(Runnable command) {
7676

7777
/**
7878
* 在未来某个时间执行给定的命令链表
79+
* <p>该命令可能在新的线程、已入池的线程或者正调用的线程中执行,这由 Executor 实现决定。</p>
7980
*
8081
* @param commands 命令链表
8182
*/

0 commit comments

Comments
 (0)