Skip to content

Commit 1eedde3

Browse files
committed
see 09/12 log
1 parent b7507ca commit 1eedde3

File tree

14 files changed

+332
-225
lines changed

14 files changed

+332
-225
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,14 @@ public static OutputStream string2OutputStream(String string, String charsetName
273273
*/
274274
public static byte[] bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat format) {
275275
if (bitmap == null) return null;
276-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
277-
bitmap.compress(format, 100, baos);
278-
return baos.toByteArray();
276+
ByteArrayOutputStream baos = null;
277+
try {
278+
baos = new ByteArrayOutputStream();
279+
bitmap.compress(format, 100, baos);
280+
return baos.toByteArray();
281+
} finally {
282+
FileUtils.closeIO(baos);
283+
}
279284
}
280285

281286
/**

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import android.net.wifi.WifiInfo;
55
import android.net.wifi.WifiManager;
66
import android.os.Build;
7-
import android.os.Environment;
87

9-
import java.io.File;
108
import java.io.IOException;
119
import java.io.InputStreamReader;
1210
import java.io.LineNumberReader;
@@ -33,36 +31,38 @@ private DeviceUtils() {
3331
* @return MAC地址
3432
*/
3533
public static String getMacAddress(Context context) {
36-
WifiManager wifi = (WifiManager) context
37-
.getSystemService(Context.WIFI_SERVICE);
34+
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
3835
WifiInfo info = wifi.getConnectionInfo();
39-
String macAddress = info.getMacAddress().replace(":", "");
40-
return macAddress == null ? "" : macAddress;
36+
if (info != null) {
37+
String macAddress = info.getMacAddress();
38+
if (macAddress != null) {
39+
return macAddress.replace(":", "");
40+
}
41+
}
42+
return null;
4143
}
4244

4345
/**
4446
* 获取设备MAC地址
45-
*
47+
* <p/>
4648
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>}</p>
4749
*
4850
* @return MAC地址
4951
*/
52+
5053
public static String getMacAddress() {
5154
String macAddress = null;
52-
LineNumberReader reader = null;
55+
LineNumberReader lnr = null;
56+
InputStreamReader isr = null;
5357
try {
5458
Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
55-
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
56-
reader = new LineNumberReader(ir);
57-
macAddress = reader.readLine().replace(":", "");
58-
} catch (IOException ex) {
59-
ex.printStackTrace();
59+
isr = new InputStreamReader(pp.getInputStream());
60+
lnr = new LineNumberReader(isr);
61+
macAddress = lnr.readLine().replace(":", "");
62+
} catch (IOException e) {
63+
e.printStackTrace();
6064
} finally {
61-
try {
62-
if (reader != null) reader.close();
63-
} catch (IOException e) {
64-
e.printStackTrace();
65-
}
65+
FileUtils.closeIO(lnr, isr);
6666
}
6767
return macAddress == null ? "" : macAddress;
6868
}
@@ -90,4 +90,4 @@ public static String getModel() {
9090
}
9191
return model;
9292
}
93-
}
93+
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,14 @@ private ImageUtils() {
6363
*/
6464
public static byte[] bitmap2Bytes(Bitmap bitmap, CompressFormat format) {
6565
if (bitmap == null) return null;
66-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
67-
bitmap.compress(format, 100, baos);
68-
return baos.toByteArray();
66+
ByteArrayOutputStream baos = null;
67+
try {
68+
baos = new ByteArrayOutputStream();
69+
bitmap.compress(format, 100, baos);
70+
return baos.toByteArray();
71+
} finally {
72+
FileUtils.closeIO(baos);
73+
}
6974
}
7075

7176
/**
@@ -699,15 +704,17 @@ public static Bitmap addText(Bitmap src, String text, int textSize, int color, f
699704
* @param unit 最大值单位
700705
* @return 压缩过的图片
701706
*/
702-
private Bitmap compress(Bitmap src, CompressFormat format, long topLimit, ConstUtils.MemoryUnit unit) {
707+
private Bitmap compress(Bitmap src, CompressFormat format, long topLimit, ConstUtils.MemoryUnit unit, boolean
708+
recycle) {
709+
if (format == CompressFormat.PNG) return src;
703710
ByteArrayOutputStream os = new ByteArrayOutputStream();
704711
src.compress(format, 100, os);
705712
long upperSize = FileUtils.size2Byte(topLimit, unit);
706713
while (os.toByteArray().length > upperSize) {
707714
os.reset();
708715
src.compress(format, 50, os);
709716
}
710-
if (!src.isRecycled()) src.recycle();
717+
if (recycle && !src.isRecycled()) src.recycle();
711718
return BitmapFactory.decodeStream(new ByteArrayInputStream(os.toByteArray()));
712719
}
713720

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

Lines changed: 120 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
import java.io.BufferedInputStream;
5-
import java.io.BufferedOutputStream;
65
import java.io.File;
76
import java.io.FileInputStream;
87
import java.io.FileOutputStream;
@@ -18,7 +17,7 @@
1817
import java.util.zip.ZipFile;
1918
import java.util.zip.ZipOutputStream;
2019

21-
import static com.blankj.utilcode.utils.ConstUtils.*;
20+
import static com.blankj.utilcode.utils.ConstUtils.MB;
2221

2322
/**
2423
* <pre>
@@ -37,62 +36,152 @@ private ZipUtils() {
3736
/**
3837
* 批量压缩文件
3938
*
40-
* @param resFileList 要压缩的文件列表
41-
* @param zipFile 生成的压缩文件
39+
* @param resFiles 要压缩的文件列表
40+
* @param zipFilePath 压缩文件路径
41+
* @throws IOException IO错误时抛出
4242
*/
43-
public static void zipFiles(Collection<File> resFileList, File zipFile)
43+
public static void zipFiles(Collection<File> resFiles, String zipFilePath)
4444
throws IOException {
45-
zipFiles(resFileList, zipFile, null);
45+
zipFiles(resFiles, zipFilePath, null);
4646
}
4747

4848
/**
4949
* 批量压缩文件
5050
*
51-
* @param resFileList 要压缩的文件列表
52-
* @param zipFile 生成的压缩文件
51+
* @param resFiles 要压缩的文件列表
52+
* @param zipFilePath 压缩文件路径
5353
* @param comment 压缩文件的注释
54+
* @throws IOException IO错误时抛出
5455
*/
55-
public static void zipFiles(Collection<File> resFileList, File zipFile, String comment) {
56-
try {
57-
for (File resFile : resFileList) {
58-
zipFile(resFile, "", zipFile, comment);
59-
}
60-
} catch (IOException e) {
61-
e.printStackTrace();
56+
public static void zipFiles(Collection<File> resFiles, String zipFilePath, String comment)
57+
throws IOException {
58+
zipFiles(resFiles, FileUtils.getFileByPath(zipFilePath), comment);
59+
}
60+
61+
/**
62+
* 批量压缩文件
63+
*
64+
* @param resFiles 要压缩的文件列表
65+
* @param zipFile 生成的压缩文件
66+
* @throws IOException IO错误时抛出
67+
*/
68+
public static void zipFiles(Collection<File> resFiles, File zipFile)
69+
throws IOException {
70+
zipFiles(resFiles, zipFile, null);
71+
}
72+
73+
/**
74+
* 批量压缩文件
75+
*
76+
* @param resFiles 要压缩的文件列表
77+
* @param zipFile 压缩文件
78+
* @param comment 压缩文件的注释
79+
* @throws IOException IO错误时抛出
80+
*/
81+
public static void zipFiles(Collection<File> resFiles, File zipFile, String comment)
82+
throws IOException {
83+
if (resFiles == null || zipFile == null) return;
84+
zipFiles(resFiles, new ZipOutputStream(new FileOutputStream(zipFile)), comment);
85+
}
86+
87+
/**
88+
* 批量压缩文件
89+
*
90+
* @param resFiles 要压缩的文件列表
91+
* @param zos 压缩文件输出流
92+
* @param comment 压缩文件的注释
93+
* @throws IOException IO错误时抛出
94+
*/
95+
public static void zipFiles(Collection<File> resFiles, ZipOutputStream zos, String comment)
96+
throws IOException {
97+
for (File resFile : resFiles) {
98+
zipFile(resFile, "", zos, comment);
6299
}
63100
}
64101

65102
/**
66103
* 压缩文件
67104
*
68-
* @param resDirPath 需要压缩的文件
69-
* @param rootPath 相对于压缩文件的路径
70-
* @param zipFile 压缩的目的文件
71-
* @throws IOException 当压缩过程出错时抛出
105+
* @param resFilePath 需要压缩的文件路径
106+
* @param zipFilePath 压缩文件路径
107+
* @throws IOException IO错误时抛出
108+
*/
109+
public static void zipFile(String resFilePath, String zipFilePath)
110+
throws IOException {
111+
zipFile(resFilePath, zipFilePath, null);
112+
}
113+
114+
/**
115+
* 压缩文件
116+
*
117+
* @param resFilePath 需要压缩的文件路径
118+
* @param zipFilePath 压缩文件路径
119+
* @param comment 压缩文件的注释
120+
* @throws IOException IO错误时抛出
121+
*/
122+
public static void zipFile(String resFilePath, String zipFilePath, String comment)
123+
throws IOException {
124+
zipFile(FileUtils.getFileByPath(resFilePath), FileUtils.getFileByPath(zipFilePath), comment);
125+
}
126+
127+
/**
128+
* 压缩文件
129+
*
130+
* @param resFile 需要压缩的文件
131+
* @param zipFile 压缩文件
132+
* @throws IOException IO错误时抛出
133+
*/
134+
public static void zipFile(File resFile, File zipFile)
135+
throws IOException {
136+
zipFile(resFile, zipFile, null);
137+
}
138+
139+
/**
140+
* 压缩文件
141+
*
142+
* @param resFile 需要压缩的文件
143+
* @param zipFile 压缩文件
144+
* @param comment 压缩文件的注释
145+
* @throws IOException IO错误时抛出
146+
*/
147+
public static void zipFile(File resFile, File zipFile, String comment)
148+
throws IOException {
149+
if (resFile == null || zipFile == null) return;
150+
zipFile(resFile, "", new ZipOutputStream(new FileOutputStream(zipFile)), comment);
151+
}
152+
153+
/**
154+
* 压缩文件
155+
*
156+
* @param resFile 需要压缩的文件
157+
* @param rootPath 相对于压缩文件的路径
158+
* @param zos 压缩文件输出流
159+
* @param comment 压缩文件的注释
160+
* @throws IOException IO错误时抛出
72161
*/
73-
public static void zipFile(File resDirPath, String rootPath, File zipFile, String comment)
162+
public static void zipFile(File resFile, String rootPath, ZipOutputStream zos, String comment)
74163
throws IOException {
75-
rootPath = rootPath + (rootPath.trim().length() == 0 ? "" : File.separator) + resDirPath.getName();
76-
if (resDirPath.isDirectory()) {
77-
File[] fileList = resDirPath.listFiles();
164+
rootPath = rootPath + (StringUtils.isSpace(rootPath) ? "" : File.separator) + resFile.getName();
165+
if (resFile.isDirectory()) {
166+
File[] fileList = resFile.listFiles();
78167
for (File file : fileList) {
79-
zipFile(file, rootPath, file, comment);
168+
zipFile(file, rootPath, zos, comment);
80169
}
81170
} else {
82171
InputStream is = null;
83-
ZipOutputStream zos = null;
84172
try {
85-
is = new BufferedInputStream(new FileInputStream(resDirPath));
86-
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
173+
is = new BufferedInputStream(new FileInputStream(resFile));
87174
zos.putNextEntry(new ZipEntry(rootPath));
88175
byte buffer[] = new byte[MB];
89176
int len;
90177
while ((len = is.read(buffer)) != -1) {
91178
zos.write(buffer, 0, len);
92179
}
93-
if (comment != null) zos.setComment(comment);
180+
if (StringUtils.isEmpty(comment)) zos.setComment(comment);
94181
} finally {
95-
FileUtils.closeIO(is, zos);
182+
FileUtils.closeIO(is);
183+
zos.flush();
184+
zos.closeEntry();
96185
}
97186
}
98187
}
@@ -104,7 +193,7 @@ public static void zipFile(File resDirPath, String rootPath, File zipFile, Strin
104193
* @param destDirPath 解压缩的目标目录
105194
* @throws IOException IO错误时抛出
106195
*/
107-
public static void upZipFile(String zipFilePath, String destDirPath)
196+
public static void unzipFile(String zipFilePath, String destDirPath)
108197
throws IOException {
109198
if (!FileUtils.createOrExistsDir(destDirPath)) return;
110199
ZipFile zf = new ZipFile(zipFilePath);
@@ -142,7 +231,7 @@ public static void upZipFile(String zipFilePath, String destDirPath)
142231
* @param targetFile 目标文件名
143232
* @throws IOException IO错误时抛出
144233
*/
145-
public static List<File> upZipSelectedFile(File zipFile, String destDirPath, String targetFile)
234+
public static List<File> unzipSelectedFile(File zipFile, String destDirPath, String targetFile)
146235
throws IOException {
147236
if (!FileUtils.createOrExistsDir(destDirPath)) return null;
148237
List<File> fileList = new ArrayList<>();

0 commit comments

Comments
 (0)