Initial project
This commit is contained in:
168
src/main/java/com/imyeyu/io/IOSize.java
Normal file
168
src/main/java/com/imyeyu/io/IOSize.java
Normal file
@ -0,0 +1,168 @@
|
||||
package com.imyeyu.io;
|
||||
|
||||
/**
|
||||
* 字节大小工具
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2023-06-01 16:41
|
||||
*/
|
||||
public class IOSize {
|
||||
|
||||
/**
|
||||
* 储存单位
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2022-04-08 14:37
|
||||
*/
|
||||
public enum Unit {
|
||||
|
||||
/** B */
|
||||
B,
|
||||
|
||||
/** KB */
|
||||
KB,
|
||||
|
||||
/** MB */
|
||||
MB,
|
||||
|
||||
/** GB */
|
||||
GB,
|
||||
|
||||
/** TB */
|
||||
TB,
|
||||
|
||||
/** PB */
|
||||
PB,
|
||||
|
||||
/** EB */
|
||||
EB;
|
||||
|
||||
/**
|
||||
* 转换指定单位字节量
|
||||
*
|
||||
* @param unit 单位
|
||||
* @param value 值
|
||||
* @return 该单位值字节量
|
||||
*/
|
||||
public static long value(Unit unit, double value) {
|
||||
Unit[] values = values();
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (values[i] == unit) {
|
||||
return (long) (value * Math.pow(1024, i));
|
||||
}
|
||||
}
|
||||
return (long) value;
|
||||
}
|
||||
}
|
||||
|
||||
/** 1 字节 */
|
||||
public static long BYTE = 1;
|
||||
|
||||
/** 1 KB */
|
||||
public static long KB = BYTE << 10;
|
||||
|
||||
/** 1 MB */
|
||||
public static long MB = KB << 10;
|
||||
|
||||
/** 1 GB */
|
||||
public static long GB = MB << 10;
|
||||
|
||||
/** 1 TB */
|
||||
public static long TB = GB << 10;
|
||||
|
||||
/** 1 PB */
|
||||
public static long PB = TB << 10;
|
||||
|
||||
/** 1 EB */
|
||||
public static long EB = PB << 10;
|
||||
|
||||
|
||||
/**
|
||||
* <p>格式化一个储存容量,保留两位小数
|
||||
* <pre>
|
||||
* // 返回 100.01 KB
|
||||
* Tools.byteFormat(102411, 2);
|
||||
* </pre>
|
||||
*
|
||||
* @param size 字节大小
|
||||
* @return 格式化结果
|
||||
*/
|
||||
public static String format(double size) {
|
||||
return format(size, 2, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>格式化一个储存容量
|
||||
* <pre>
|
||||
* // 返回 100.01 KB
|
||||
* Tools.byteFormat(102411, 2);
|
||||
* </pre>
|
||||
*
|
||||
* @param size 字节大小
|
||||
* @param stopUnit 最高等级,格式化到某单位后不再升级,最低 {@link Unit#B},最高 {@link Unit#EB}
|
||||
* @return 格式化结果
|
||||
*/
|
||||
public static String format(double size, Unit stopUnit) {
|
||||
return format(size, 2, stopUnit);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>格式化一个储存容量
|
||||
* <pre>
|
||||
* // 返回 100.01 KB
|
||||
* Tools.byteFormat(102411, 2);
|
||||
* </pre>
|
||||
*
|
||||
* @param size 字节大小
|
||||
* @param decimal 保留小数
|
||||
* @param stopUnit 最高等级,格式化到某单位后不再升级,最低 {@link Unit#B},最高 {@link Unit#EB}
|
||||
* @return 格式化结果
|
||||
*/
|
||||
public static String format(double size, int decimal, Unit stopUnit) {
|
||||
final Unit[] unit = Unit.values();
|
||||
if (0 < size) {
|
||||
for (int i = 0; i < unit.length; i++, size /= 1024d) {
|
||||
if (size <= 1000 || i == unit.length - 1 || unit[i] == stopUnit) {
|
||||
if (i == 0) {
|
||||
// 最小单位不需要小数
|
||||
return (int) size + " B";
|
||||
} else {
|
||||
String format = "%." + decimal + "f " + unit[i];
|
||||
return String.format(format, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return "0 B";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <p>格式化一个储存容量,不带单位
|
||||
* <pre>
|
||||
* // 返回 100.01
|
||||
* Tools.byteFormat(102411, 2);
|
||||
* </pre>
|
||||
*
|
||||
* @param size 字节大小
|
||||
* @param decimal 保留小数
|
||||
* @param stopUnit 最高等级,格式化到某单位后不再升级,最低 {@link Unit#B},最高 {@link Unit#TB}
|
||||
* @return 格式化结果(不带单位)
|
||||
*/
|
||||
public static String formatWithoutUnit(double size, int decimal, Unit stopUnit) {
|
||||
final Unit[] unit = Unit.values();
|
||||
if (0 < size) {
|
||||
for (int i = 0; i < unit.length; i++, size /= 1024d) {
|
||||
if (size <= 1000 || i == unit.length - 1 || unit[i] == stopUnit) {
|
||||
if (i == 0) {
|
||||
return String.valueOf((int) size);
|
||||
} else {
|
||||
String format = "%." + decimal + "f";
|
||||
return String.format(format, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return "0";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user