improve MemoryUtil

This commit is contained in:
Marc Baloup 2020-03-29 21:22:31 +02:00
parent cda196e377
commit b38e0c7b60
2 changed files with 59 additions and 17 deletions

View File

@ -24,7 +24,7 @@ public class MinecraftWebUtil {
if (c == code_prefix && (i<ligne.length()-1)) {
i++;
c = ligne.charAt(i);
if (color_char.contains(new Character(c).toString().toLowerCase())) {
if (color_char.contains(new String(new char[] {Character.toLowerCase(c)}))) {
if (bold) {
builder += "</span>";
bold = false;

View File

@ -4,29 +4,71 @@ import java.text.DecimalFormat;
public class MemoryUtil {
public enum MemoryUnit {
B(1, 1, null),
KB(1024, 1000, "k"),
MB(1024 * 1024, 1000_000, "M"),
GB(1024 * 1024 * 1024, 1000_000_000, "G");
final long valueTrad;
final long valueSI;
final String unitMultiplier;
public long toUnitRound(long byteCount, boolean si) {
return byteCount / value(si);
}
public double toUnit(long byteCount, boolean si) {
return byteCount / (double)value(si);
}
public long value(boolean si) {
return si ? valueSI : valueTrad;
}
public String unit(boolean si) {
return unitMultiplier == null ? "o" : (unitMultiplier + (si ? "o" : "io"));
}
private MemoryUnit(long vTrad, long vSI, String uMult) {
valueTrad = vTrad;
valueSI = vSI;
unitMultiplier = uMult;
}
}
private static final DecimalFormat format = new DecimalFormat("#####0.00");
public static String humanReadableSize(long octet, boolean si) {
public static String humanReadableSize(long octet, MemoryUnit roundTo, boolean si) {
boolean neg = octet < 0;
double size = Math.abs(octet);
long size = Math.abs(octet);
int diveBy = si ? 1000 : 1024;
MemoryUnit unit = roundTo;
for (int ui = MemoryUnit.values().length - 1; ui >= 0; ui--) {
MemoryUnit u = MemoryUnit.values()[ui];
if (u == roundTo)
break;
if (size < u.value(si))
continue;
unit = u;
break;
}
if (size < diveBy) return (neg ? "-" : "") + size + "o";
size /= diveBy;
if (size < diveBy) return (neg ? "-" : "") + format.format(size) + (si ? "ko" : "kio");
size /= diveBy;
if (size < diveBy) return (neg ? "-" : "") + format.format(size) + (si ? "Mo" : "Mio");
size /= diveBy;
if (size < diveBy) return (neg ? "-" : "") + format.format(size) + (si ? "Go" : "Gio");
size /= diveBy;
String dispValue;
if (unit == roundTo) {
dispValue = ""+unit.toUnitRound(size, si);
}
else {
dispValue = format.format(unit.toUnit(size, si));
}
return (neg ? "-" : "") + format.format(size) + (si ? "To" : "Tio");
return (neg ? "-" : "") + dispValue + unit.unit(si);
}
public static String humanReadableSize(long octet) {
return humanReadableSize(octet, false);
return humanReadableSize(octet, MemoryUnit.B, false);
}
}