Skip to content

Commit e803aec

Browse files
committed
添加文件单位转换
1 parent 3abd310 commit e803aec

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

tang-commons/src/main/java/com/tang/commons/utils/ByteUtils.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.text.DecimalFormat;
44
import java.util.List;
5+
import java.util.Objects;
6+
7+
import com.tang.commons.enumeration.SizeUnit;
58

69
/**
710
* 字节工具类
@@ -52,4 +55,27 @@ public static String byteToHex(byte[] bytes) {
5255
return new String(hexChars);
5356
}
5457

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+
5581
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

0 commit comments

Comments
 (0)