File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed
java/com/tang/commons/utils
kotlin/com/tang/commons/enumeration Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import java .text .DecimalFormat ;
4
4
import java .util .List ;
5
+ import java .util .Objects ;
6
+
7
+ import com .tang .commons .enumeration .SizeUnit ;
5
8
6
9
/**
7
10
* 字节工具类
@@ -52,4 +55,27 @@ public static String byteToHex(byte[] bytes) {
52
55
return new String (hexChars );
53
56
}
54
57
58
+ /**
59
+ * 文件单位转换
60
+ *
61
+ * @param size 大小
62
+ * @param inputUnit 输入单位
63
+ * @param outputUnit 输出单位
64
+ * @return 转换后的大小
65
+ */
66
+ public static double convert (double size , SizeUnit inputUnit , SizeUnit outputUnit ) {
67
+ if (size == 0 ) {
68
+ return size ;
69
+ }
70
+
71
+ if (Objects .isNull (inputUnit ) || Objects .isNull (outputUnit )) {
72
+ return size ;
73
+ }
74
+
75
+ var inputUnitValue = inputUnit .getValue ();
76
+ var outputUnitValue = outputUnit .getValue ();
77
+
78
+ return size * inputUnitValue / outputUnitValue ;
79
+ }
80
+
55
81
}
Original file line number Diff line number Diff line change
1
+ package com.tang.commons.enumeration
2
+
3
+ /* *
4
+ * 文件大小单位枚举类
5
+ *
6
+ * @author Tang
7
+ */
8
+ enum class SizeUnit (val value : Long ) {
9
+
10
+ B (1L ),
11
+
12
+ KB ( B .value * 1024L ),
13
+
14
+ MB (KB .value * 1024L ),
15
+
16
+ GB (MB .value * 1024L ),
17
+
18
+ TB (GB .value * 1024L ),
19
+
20
+ PB (TB .value * 1024L ),
21
+
22
+ EB (PB .value * 1024L ),
23
+
24
+ ZB (EB .value * 1024L ),
25
+
26
+ YB (ZB .value * 1024L );
27
+
28
+ }
You can’t perform that action at this time.
0 commit comments