Skip to content

Commit 5418b18

Browse files
committed
see 08/30 log
1 parent 184618b commit 5418b18

File tree

4 files changed

+132
-46
lines changed

4 files changed

+132
-46
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ private ConstUtils() {
1515
}
1616

1717
/******************** 存储相关常量 ********************/
18-
public enum MemoryUnit {
19-
BYTE,
20-
KB,
21-
MB,
22-
GB,
23-
}
2418
/**
2519
* Byte与Byte的倍数
2620
*/
@@ -38,14 +32,14 @@ public enum MemoryUnit {
3832
*/
3933
public static final int GB = 1073741824;
4034

41-
/******************** 时间相关常量 ********************/
42-
public enum TimeUnit {
43-
MSEC,
44-
SEC,
45-
MIN,
46-
HOUR,
47-
DAY
35+
public enum MemoryUnit {
36+
BYTE,
37+
KB,
38+
MB,
39+
GB
4840
}
41+
42+
/******************** 时间相关常量 ********************/
4943
/**
5044
* 毫秒与毫秒的倍数
5145
*/
@@ -67,6 +61,14 @@ public enum TimeUnit {
6761
*/
6862
public static final int DAY = 86400000;
6963

64+
public enum TimeUnit {
65+
MSEC,
66+
SEC,
67+
MIN,
68+
HOUR,
69+
DAY
70+
}
71+
7072
/******************** 正则相关常量 ********************/
7173
/**
7274
* 正则:手机号(简单)

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

Lines changed: 94 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
import android.graphics.drawable.BitmapDrawable;
88
import android.graphics.drawable.Drawable;
99

10-
import java.io.BufferedReader;
1110
import java.io.ByteArrayInputStream;
1211
import java.io.ByteArrayOutputStream;
1312
import java.io.IOException;
1413
import java.io.InputStream;
15-
import java.io.InputStreamReader;
14+
import java.io.OutputStream;
1615
import java.io.UnsupportedEncodingException;
1716

1817
/**
@@ -112,12 +111,12 @@ public static char[] bytes2Chars(byte[] bytes) {
112111
}
113112

114113
/**
115-
* inputStream转byteArr
114+
* inputStream转outputStream
116115
*
117116
* @param is 输入流
118-
* @return 字节数组
117+
* @return outputStream子类
119118
*/
120-
public static byte[] inputStream2Bytes(InputStream is) {
119+
public static ByteArrayOutputStream input2OutputStream(InputStream is) {
121120
if (is == null) return null;
122121
try {
123122
ByteArrayOutputStream os = new ByteArrayOutputStream();
@@ -126,7 +125,7 @@ public static byte[] inputStream2Bytes(InputStream is) {
126125
while ((len = is.read(b)) != -1) {
127126
os.write(b, 0, len);
128127
}
129-
return os.toByteArray();
128+
return os;
130129
} catch (IOException e) {
131130
e.printStackTrace();
132131
return null;
@@ -135,6 +134,27 @@ public static byte[] inputStream2Bytes(InputStream is) {
135134
}
136135
}
137136

137+
/**
138+
* outputStream转inputStream
139+
*
140+
* @param out 输出流
141+
* @return inputStream子类
142+
*/
143+
public ByteArrayInputStream output2InputStream(OutputStream out) {
144+
if (out == null) return null;
145+
return new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray());
146+
}
147+
148+
/**
149+
* inputStream转byteArr
150+
*
151+
* @param is 输入流
152+
* @return 字节数组
153+
*/
154+
public static byte[] inputStream2Bytes(InputStream is) {
155+
return input2OutputStream(is).toByteArray();
156+
}
157+
138158
/**
139159
* byteArr转inputStream
140160
*
@@ -145,6 +165,37 @@ public static InputStream bytes2InputStream(byte[] bytes) {
145165
return new ByteArrayInputStream(bytes);
146166
}
147167

168+
/**
169+
* outputStream转byteArr
170+
*
171+
* @param out 输出流
172+
* @return 字节数组
173+
*/
174+
public static byte[] outputStream2Bytes(OutputStream out) {
175+
if (out == null) return null;
176+
return ((ByteArrayOutputStream) out).toByteArray();
177+
}
178+
179+
/**
180+
* outputStream转byteArr
181+
*
182+
* @param bytes 字节数组
183+
* @return 字节数组
184+
*/
185+
public static OutputStream bytes2OutputStream(byte[] bytes) {
186+
ByteArrayOutputStream os = null;
187+
try {
188+
os = new ByteArrayOutputStream();
189+
os.write(bytes);
190+
return os;
191+
} catch (IOException e) {
192+
e.printStackTrace();
193+
return null;
194+
} finally {
195+
FileUtils.closeIO(os);
196+
}
197+
}
198+
148199
/**
149200
* inputStream转string按编码
150201
*
@@ -153,26 +204,12 @@ public static InputStream bytes2InputStream(byte[] bytes) {
153204
* @return 字符串
154205
*/
155206
public static String inputStream2String(InputStream is, String charsetName) {
156-
if (is == null) return null;
157-
BufferedReader reader;
207+
if (is == null || StringUtils.isSpace(charsetName)) return null;
158208
try {
159-
StringBuilder sb = new StringBuilder();
160-
if (StringUtils.isSpace(charsetName)) {
161-
reader = new BufferedReader(new InputStreamReader(is));
162-
} else {
163-
reader = new BufferedReader(new InputStreamReader(is, charsetName));
164-
}
165-
String line;
166-
while ((line = reader.readLine()) != null) {
167-
sb.append(line).append("\r\n");// windows系统换行为\r\n,Linux为\n
168-
}
169-
// 要去除最后的换行符
170-
return sb.delete(sb.length() - 2, sb.length()).toString();
171-
} catch (IOException e) {
209+
return new String(inputStream2Bytes(is), charsetName);
210+
} catch (UnsupportedEncodingException e) {
172211
e.printStackTrace();
173212
return null;
174-
} finally {
175-
FileUtils.closeIO(is);
176213
}
177214
}
178215

@@ -193,6 +230,40 @@ public static InputStream string2InputStream(String string, String charsetName)
193230
}
194231
}
195232

233+
/**
234+
* outputStream转string按编码
235+
*
236+
* @param out 输出流
237+
* @param charsetName 编码格式
238+
* @return 字符串
239+
*/
240+
public static String outputStream2String(OutputStream out, String charsetName) {
241+
if (out == null) return null;
242+
try {
243+
return new String(outputStream2Bytes(out), charsetName);
244+
} catch (UnsupportedEncodingException e) {
245+
e.printStackTrace();
246+
return null;
247+
}
248+
}
249+
250+
/**
251+
* string转outputStream按编码
252+
*
253+
* @param string 字符串
254+
* @param charsetName 编码格式
255+
* @return 输入流
256+
*/
257+
public static OutputStream string2OutputStream(String string, String charsetName) {
258+
if (string == null || StringUtils.isSpace(charsetName)) return null;
259+
try {
260+
return bytes2OutputStream(string.getBytes(charsetName));
261+
} catch (UnsupportedEncodingException e) {
262+
e.printStackTrace();
263+
return null;
264+
}
265+
}
266+
196267
/**
197268
* bitmap转byteArr
198269
*

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -862,11 +862,25 @@ public static String readFile2String(String filePath, String charsetName) {
862862
*/
863863
public static String readFile2String(File file, String charsetName) {
864864
if (file == null) return null;
865+
BufferedReader reader = null;
865866
try {
866-
return ConvertUtils.inputStream2String(new FileInputStream(file), charsetName);
867-
} catch (FileNotFoundException e) {
867+
StringBuilder sb = new StringBuilder();
868+
if (StringUtils.isSpace(charsetName)) {
869+
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
870+
} else {
871+
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
872+
}
873+
String line;
874+
while ((line = reader.readLine()) != null) {
875+
sb.append(line).append("\r\n");// windows系统换行为\r\n,Linux为\n
876+
}
877+
// 要去除最后的换行符
878+
return sb.delete(sb.length() - 2, sb.length()).toString();
879+
} catch (IOException e) {
868880
e.printStackTrace();
869881
return null;
882+
} finally {
883+
closeIO(reader);
870884
}
871885
}
872886

@@ -982,7 +996,7 @@ public static void closeIO(Closeable... closeables) {
982996
* @return filePath最长目录
983997
*/
984998
public static String getDirName(File file) {
985-
if (!isFileExists(file)) return "";
999+
if (file == null) return null;
9861000
return getDirName(file.getPath());
9871001
}
9881002

@@ -1005,7 +1019,7 @@ public static String getDirName(String filePath) {
10051019
* @return 文件名
10061020
*/
10071021
public static String getFileName(File file) {
1008-
if (!isFileExists(file)) return "";
1022+
if (file == null) return null;
10091023
return getFileName(file.getPath());
10101024
}
10111025

@@ -1028,7 +1042,7 @@ public static String getFileName(String filePath) {
10281042
* @return 不带拓展名的文件名
10291043
*/
10301044
public static String getFileNameNoExtension(File file) {
1031-
if (!isFileExists(file)) return "";
1045+
if (file == null) return null;
10321046
return getFileNameNoExtension(file.getPath());
10331047
}
10341048

@@ -1059,7 +1073,7 @@ public static String getFileNameNoExtension(String filePath) {
10591073
* @return 文件拓展名
10601074
*/
10611075
public static String getFileExtension(File file) {
1062-
if (!isFileExists(file)) return "";
1076+
if (file == null) return null;
10631077
return getFileExtension(file.getPath());
10641078
}
10651079

@@ -1074,6 +1088,6 @@ public static String getFileExtension(String filePath) {
10741088
int lastPoi = filePath.lastIndexOf('.');
10751089
int lastSep = filePath.lastIndexOf(File.separator);
10761090
if (lastPoi == -1 || lastSep >= lastPoi) return "";
1077-
return filePath.substring(lastPoi + 1);
1091+
return filePath.substring(lastPoi);
10781092
}
10791093
}

utilcode/src/test/java/com/blankj/utilcode/utils/FileUtilsTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,7 @@ public void testGetFileNameNoExtension() throws Exception {
215215

216216
@Test
217217
public void testGetFileExtension() throws Exception {
218-
assertThat(getFileExtension(new File(path + "UTF8.txt"))).isEqualTo("txt");
219-
assertThat(getFileExtension(path + "UTF8.txt")).isEqualTo("txt");
220-
System.out.println(new File(path).getName().endsWith("file"));
218+
assertThat(getFileExtension(new File(path + "UTF8.txt"))).isEqualTo(".txt");
219+
assertThat(getFileExtension(path + "UTF8.txt")).isEqualTo(".txt");
221220
}
222221
}

0 commit comments

Comments
 (0)