简介
实现需求时遇到了需要进行字节转换的情况,针对面临的一些问题以及思考拓展实现了一个工具类。
基本功能如下:
- 自定义转换的单位区间。比如转换结果的最小单位为KB,最大单位为GB。
- 自定义进制数。一般来说以1024为进制数,但有时也可能会用到1000。
- 转换结果再处理。支持对转换结果进行保留小数、向上取整、向下取整、四舍五入等操作。
- 转换结果带单位。支持为结果添加单位,例如10KB。
- 特殊处理“零”。当转换结果为0时,往往需要展示特殊的文案,如"无可清理内容",可通过自定义接口的方式返回特殊的字符串,默认返回空字符串。
- 更多功能可自行阅读代码后拓展实现,或在网上下载研究其他转换工具类,相信各位能收获更多。
完整代码
public class ByteUtils {
public static final String TAG = "ByteUtils";
public static final float STANDARD_RADIX = 1024;
public static final float SPECIAL_RADIX = 1000;
public static final int MODE_DEFAULT = 0;
public static final int MODE_KEEP_ONE = 1;
public static final int MODE_KEEP_TWO = 2;
public static final int MODE_CEIL = 10;
public static final int MODE_FLOOR = 11;
public static final int MODE_FLOOR_NOT_ZERO = 12;
public static final int MODE_ROUND = 13;
public static final int MODE_ROUND_NOT_ZERO = 14;
public static final zeroHandler DEFAULT_HANDLER = new zeroHandler() {
@Override
public String getString() {
return "";
}
};
public enum Unit {
BYTE(R.string.byteShort),
KILOBYTE(R.string.kilobyteShort),
MEGABYTE(R.string.megabyteShort),
GIGABYTE(R.string.gigabyteShort),
TERABYTE(R.string.terabyteShort),
PETABYTE(R.string.petabyteShort);
private final int resId;
Unit(int resId) {
this.resId = resId;
}
public int getResId() {
return resId;
}
}
public static String numberToByteString(double size, Unit unit, Unit minimumUnit, Unit maximumUnit, float radix, int resultMode, boolean needUnit, zeroHandler handler) {
if (size < 0) {
DefaultLogger.d(TAG, "the size must be greater than zero");
return "";
}
if (handler != null && size == 0) {
return handler.getString();
}
int init = unit.ordinal();
int min = minimumUnit.ordinal();
int max = maximumUnit.ordinal();
if (min > max) {
DefaultLogger.d(TAG, "min unit need be less than max unit");
return "";
}
double byteSize = size * Math.pow(radix, init);
int finalUnit = 0;
while (finalUnit < min) {
byteSize = byteSize / radix;
finalUnit++;
}
while (byteSize > radix) {
if (finalUnit == max) break;
byteSize = byteSize / radix;
finalUnit++;
}
String resultSize = String.valueOf(byteSize);
String resultUnit = "";
if (resultMode != MODE_DEFAULT) {
if (resultMode <= MODE_KEEP_TWO) {
String format = "%." + resultMode + "f";
resultSize = String.format(format, byteSize);
} else {
switch (resultMode) {
case MODE_CEIL:
byteSize = Math.ceil(byteSize);
break;
case MODE_FLOOR:
byteSize = Math.floor(byteSize);
break;
case MODE_FLOOR_NOT_ZERO:
double temp1 = Math.floor(byteSize);
byteSize = temp1 == 0 ? 1 : temp1;
break;
case MODE_ROUND:
byteSize = Math.round(byteSize);
break;
case MODE_ROUND_NOT_ZERO:
double temp2 = Math.round(byteSize);
byteSize = temp2 == 0 ? 1 : temp2;
break;
default:
break;
}
resultSize = String.valueOf((long) byteSize);
}
}
if (needUnit) {
resultUnit = ResourceUtils.getString(Unit.values()[finalUnit].getResId());
}
return resultSize + resultUnit;
}
public interface zeroHandler {
String getString();
}
public static String numberToByteString(double size, Unit unit) {
return numberToByteString(size, Unit.BYTE, unit, unit, STANDARD_RADIX, MODE_DEFAULT, false, null);
}
public static String numberToByteString(double size, Unit unit, boolean needUnit) {
return numberToByteString(size, Unit.BYTE, unit, unit, STANDARD_RADIX, MODE_DEFAULT, needUnit, null);
}
public static String numberToByteString(double size, Unit unit, int mode) {
return numberToByteString(size, Unit.BYTE, unit, unit, STANDARD_RADIX, mode, false, null);
}
public static String numberToByteString(double size, Unit unit, int mode, boolean needUnit) {
return numberToByteString(size, Unit.BYTE, unit, unit, STANDARD_RADIX, mode, needUnit, null);
}
public static String numberToByteString(double size, Unit unit, float radix) {
return numberToByteString(size, Unit.BYTE, unit, unit, radix, MODE_DEFAULT, false, null);
}
}
|