Skip to content

Commit 19aa01b

Browse files
committed
improve humanReadableByteCount
1 parent 50f8cf8 commit 19aa01b

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

core/src/main/java/com/taobao/arthas/core/util/StringUtils.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@
1515

1616
public abstract class StringUtils {
1717

18-
private static final String FOLDER_SEPARATOR = "/";
19-
private static final String WINDOWS_FOLDER_SEPARATOR = "\\";
20-
private static final String TOP_PATH = "..";
21-
private static final String CURRENT_PATH = ".";
22-
private static final char EXTENSION_SEPARATOR = '.';
23-
public static final int UNIT = 1024;
24-
public static final String STRING_UNITS = "KMGTPE";
25-
26-
2718
/**
2819
* 获取异常的原因描述
2920
*
@@ -878,16 +869,17 @@ public static String classLoaderHash(Class<?> clazz) {
878869
}
879870

880871
/**
881-
* format byte size to human readable format
872+
* format byte size to human readable format. https://stackoverflow.com/a/3758880
882873
* @param bytes byets
883874
* @return human readable format
884875
*/
885876
public static String humanReadableByteCount(long bytes) {
886-
if (bytes < UNIT) {
887-
return bytes + " B";
888-
}
889-
int exp = (int) (Math.log(bytes) / Math.log(UNIT));
890-
String pre = STRING_UNITS.charAt(exp-1) + "i";
891-
return String.format("%.2f %sB", bytes / Math.pow(UNIT, exp), pre);
877+
return bytes < 1024L ? bytes + " B"
878+
: bytes < 0xfffccccccccccccL >> 40 ? String.format("%.1f KiB", bytes / 0x1p10)
879+
: bytes < 0xfffccccccccccccL >> 30 ? String.format("%.1f MiB", bytes / 0x1p20)
880+
: bytes < 0xfffccccccccccccL >> 20 ? String.format("%.1f GiB", bytes / 0x1p30)
881+
: bytes < 0xfffccccccccccccL >> 10 ? String.format("%.1f TiB", bytes / 0x1p40)
882+
: bytes < 0xfffccccccccccccL ? String.format("%.1f PiB", (bytes >> 10) / 0x1p40)
883+
: String.format("%.1f EiB", (bytes >> 20) / 0x1p40);
892884
}
893885
}

core/src/test/java/com/taobao/arthas/core/util/StringUtilsTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public class StringUtilsTest {
2020
@Test
2121
public void testHumanReadableByteCount() {
2222
Assert.assertEquals(StringUtils.humanReadableByteCount(1023L), "1023 B");
23-
Assert.assertEquals(StringUtils.humanReadableByteCount(1024L), "1.00 KiB");
24-
Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024L), "1.00 MiB");
25-
Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024L - 100), "1023.90 KiB");
26-
Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024 * 1024L), "1.00 GiB");
27-
Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024 * 1024 * 1024L), "1.00 TiB");
28-
Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024 * 1024 * 1024 * 1024), "1.00 PiB");
23+
Assert.assertEquals(StringUtils.humanReadableByteCount(1024L), "1.0 KiB");
24+
Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024L), "1.0 MiB");
25+
Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024L - 100), "1023.9 KiB");
26+
Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024 * 1024L), "1.0 GiB");
27+
Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024 * 1024 * 1024L), "1.0 TiB");
28+
Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024 * 1024 * 1024 * 1024), "1.0 PiB");
2929
}
3030

3131
@Test

0 commit comments

Comments
 (0)