Skip to content

Commit 37c58b7

Browse files
committed
see 03/04 log
1 parent 1fd0c9a commit 37c58b7

File tree

6 files changed

+103
-15
lines changed

6 files changed

+103
-15
lines changed

app/src/main/java/com/blankj/androidutilcode/activity/FragmentActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected void onCreate(Bundle savedInstanceState) {
3030
super.onCreate(savedInstanceState);
3131
setContentView(R.layout.activity_fragment);
3232
ArrayList<Fragment> fragments = new ArrayList<>();
33-
fragments.add(Demo0Fragment.newInstance());
33+
fragments.add(Demo0Fragment.newInstance());
3434
rootFragment = FragmentUtils.addFragments(getSupportFragmentManager(), fragments, R.id.fragment_container, 0);
3535
}
3636

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
jcenter()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:2.3.0-rc1'
9+
classpath 'com.android.tools.build:gradle:2.3.0'
1010
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
1111
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
1212
// NOTE: Do not place your application dependencies here; they belong

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@ private ConstUtils() {
1515
}
1616

1717
/******************** 存储相关常量 ********************/
18+
/**
19+
* Byte与Byte的倍数
20+
*/
21+
public static final int BYTE = 1;
1822
/**
1923
* KB与Byte的倍数
2024
*/
21-
public static final int KB = 1024;
25+
public static final int KB = 1024;
2226
/**
2327
* MB与Byte的倍数
2428
*/
25-
public static final int MB = 1048576;
29+
public static final int MB = 1048576;
2630
/**
2731
* GB与Byte的倍数
2832
*/
29-
public static final int GB = 1073741824;
33+
public static final int GB = 1073741824;
3034

3135
public enum MemoryUnit {
3236
BYTE,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,13 @@ public static String byte2FitMemorySize(long byteNum) {
193193
if (byteNum < 0) {
194194
return "shouldn't be less than zero!";
195195
} else if (byteNum < ConstUtils.KB) {
196-
return String.format("%.3fB", byteNum + 0.0005);
196+
return String.format("%.3fB", (double) byteNum + 0.0005);
197197
} else if (byteNum < ConstUtils.MB) {
198-
return String.format("%.3fKB", byteNum / ConstUtils.KB + 0.0005);
198+
return String.format("%.3fKB", (double) byteNum / ConstUtils.KB + 0.0005);
199199
} else if (byteNum < ConstUtils.GB) {
200-
return String.format("%.3fMB", byteNum / ConstUtils.MB + 0.0005);
200+
return String.format("%.3fMB", (double) byteNum / ConstUtils.MB + 0.0005);
201201
} else {
202-
return String.format("%.3fGB", byteNum / ConstUtils.GB + 0.0005);
202+
return String.format("%.3fGB", (double) byteNum / ConstUtils.GB + 0.0005);
203203
}
204204
}
205205

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

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.blankj.utilcode.utils;
22

3+
import android.annotation.SuppressLint;
4+
35
import java.io.BufferedInputStream;
46
import java.io.BufferedOutputStream;
57
import java.io.BufferedReader;
68
import java.io.BufferedWriter;
9+
import java.io.ByteArrayOutputStream;
710
import java.io.File;
811
import java.io.FileInputStream;
912
import java.io.FileNotFoundException;
@@ -933,7 +936,7 @@ public static byte[] readFile2Bytes(String filePath) {
933936
public static byte[] readFile2Bytes(File file) {
934937
if (file == null) return null;
935938
try {
936-
return ConvertUtils.inputStream2Bytes(new FileInputStream(file));
939+
return inputStream2Bytes(new FileInputStream(file));
937940
} catch (FileNotFoundException e) {
938941
e.printStackTrace();
939942
return null;
@@ -1054,7 +1057,7 @@ public static String getDirSize(String dirPath) {
10541057
*/
10551058
public static String getDirSize(File dir) {
10561059
long len = getDirLength(dir);
1057-
return len == -1 ? "" : ConvertUtils.byte2FitMemorySize(len);
1060+
return len == -1 ? "" : byte2FitMemorySize(len);
10581061
}
10591062

10601063
/**
@@ -1075,7 +1078,7 @@ public static String getFileSize(String filePath) {
10751078
*/
10761079
public static String getFileSize(File file) {
10771080
long len = getFileLength(file);
1078-
return len == -1 ? "" : ConvertUtils.byte2FitMemorySize(len);
1081+
return len == -1 ? "" : byte2FitMemorySize(len);
10791082
}
10801083

10811084
/**
@@ -1160,7 +1163,7 @@ public static byte[] getFileMD5(String filePath) {
11601163
* @return 文件的MD5校验码
11611164
*/
11621165
public static String getFileMD5ToString(File file) {
1163-
return ConvertUtils.bytes2HexString(getFileMD5(file));
1166+
return bytes2HexString(getFileMD5(file));
11641167
}
11651168

11661169
/**
@@ -1288,4 +1291,85 @@ public static String getFileExtension(String filePath) {
12881291
if (lastPoi == -1 || lastSep >= lastPoi) return "";
12891292
return filePath.substring(lastPoi + 1);
12901293
}
1294+
1295+
/** copy from ConvertUtils **/
1296+
1297+
/**
1298+
* inputStream转byteArr
1299+
*
1300+
* @param is 输入流
1301+
* @return 字节数组
1302+
*/
1303+
private static byte[] inputStream2Bytes(InputStream is) {
1304+
if (is == null) return null;
1305+
return input2OutputStream(is).toByteArray();
1306+
}
1307+
1308+
/**
1309+
* inputStream转outputStream
1310+
*
1311+
* @param is 输入流
1312+
* @return outputStream子类
1313+
*/
1314+
private static ByteArrayOutputStream input2OutputStream(InputStream is) {
1315+
if (is == null) return null;
1316+
try {
1317+
ByteArrayOutputStream os = new ByteArrayOutputStream();
1318+
byte[] b = new byte[ConstUtils.KB];
1319+
int len;
1320+
while ((len = is.read(b, 0, ConstUtils.KB)) != -1) {
1321+
os.write(b, 0, len);
1322+
}
1323+
return os;
1324+
} catch (IOException e) {
1325+
e.printStackTrace();
1326+
return null;
1327+
} finally {
1328+
CloseUtils.closeIO(is);
1329+
}
1330+
}
1331+
1332+
private static final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
1333+
1334+
/**
1335+
* byteArr转hexString
1336+
* <p>例如:</p>
1337+
* bytes2HexString(new byte[] { 0, (byte) 0xa8 }) returns 00A8
1338+
*
1339+
* @param bytes 字节数组
1340+
* @return 16进制大写字符串
1341+
*/
1342+
private static String bytes2HexString(byte[] bytes) {
1343+
if (bytes == null) return null;
1344+
int len = bytes.length;
1345+
if (len <= 0) return null;
1346+
char[] ret = new char[len << 1];
1347+
for (int i = 0, j = 0; i < len; i++) {
1348+
ret[j++] = hexDigits[bytes[i] >>> 4 & 0x0f];
1349+
ret[j++] = hexDigits[bytes[i] & 0x0f];
1350+
}
1351+
return new String(ret);
1352+
}
1353+
1354+
/**
1355+
* 字节数转合适内存大小
1356+
* <p>保留3位小数</p>
1357+
*
1358+
* @param byteNum 字节数
1359+
* @return 合适内存大小
1360+
*/
1361+
@SuppressLint("DefaultLocale")
1362+
private static String byte2FitMemorySize(long byteNum) {
1363+
if (byteNum < 0) {
1364+
return "shouldn't be less than zero!";
1365+
} else if (byteNum < ConstUtils.KB) {
1366+
return String.format("%.3fB", (double) byteNum + 0.0005);
1367+
} else if (byteNum < ConstUtils.MB) {
1368+
return String.format("%.3fKB", (double) byteNum / ConstUtils.KB + 0.0005);
1369+
} else if (byteNum < ConstUtils.GB) {
1370+
return String.format("%.3fMB", (double) byteNum / ConstUtils.MB + 0.0005);
1371+
} else {
1372+
return String.format("%.3fGB", (double) byteNum / ConstUtils.GB + 0.0005);
1373+
}
1374+
}
12911375
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import java.util.zip.ZipFile;
1717
import java.util.zip.ZipOutputStream;
1818

19-
import static com.blankj.utilcode.utils.ConstUtils.KB;
20-
2119
/**
2220
* <pre>
2321
* author: Blankj
@@ -32,6 +30,8 @@ private ZipUtils() {
3230
throw new UnsupportedOperationException("u can't instantiate me...");
3331
}
3432

33+
private static final int KB = 1024;
34+
3535
/**
3636
* 批量压缩文件
3737
*

0 commit comments

Comments
 (0)