From 19aa01b0927ea27eb8eabbeecce50924c11fbba4 Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Thu, 5 Dec 2019 17:35:45 +0800 Subject: [PATCH] improve humanReadableByteCount --- .../taobao/arthas/core/util/StringUtils.java | 24 +++++++------------ .../arthas/core/util/StringUtilsTest.java | 12 +++++----- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/com/taobao/arthas/core/util/StringUtils.java b/core/src/main/java/com/taobao/arthas/core/util/StringUtils.java index bb028fa11..3d915f1df 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/StringUtils.java +++ b/core/src/main/java/com/taobao/arthas/core/util/StringUtils.java @@ -15,15 +15,6 @@ import java.util.TreeSet; public abstract class StringUtils { - private static final String FOLDER_SEPARATOR = "/"; - private static final String WINDOWS_FOLDER_SEPARATOR = "\\"; - private static final String TOP_PATH = ".."; - private static final String CURRENT_PATH = "."; - private static final char EXTENSION_SEPARATOR = '.'; - public static final int UNIT = 1024; - public static final String STRING_UNITS = "KMGTPE"; - - /** * 获取异常的原因描述 * @@ -878,16 +869,17 @@ public abstract class StringUtils { } /** - * format byte size to human readable format + * format byte size to human readable format. https://stackoverflow.com/a/3758880 * @param bytes byets * @return human readable format */ public static String humanReadableByteCount(long bytes) { - if (bytes < UNIT) { - return bytes + " B"; - } - int exp = (int) (Math.log(bytes) / Math.log(UNIT)); - String pre = STRING_UNITS.charAt(exp-1) + "i"; - return String.format("%.2f %sB", bytes / Math.pow(UNIT, exp), pre); + return bytes < 1024L ? bytes + " B" + : bytes < 0xfffccccccccccccL >> 40 ? String.format("%.1f KiB", bytes / 0x1p10) + : bytes < 0xfffccccccccccccL >> 30 ? String.format("%.1f MiB", bytes / 0x1p20) + : bytes < 0xfffccccccccccccL >> 20 ? String.format("%.1f GiB", bytes / 0x1p30) + : bytes < 0xfffccccccccccccL >> 10 ? String.format("%.1f TiB", bytes / 0x1p40) + : bytes < 0xfffccccccccccccL ? String.format("%.1f PiB", (bytes >> 10) / 0x1p40) + : String.format("%.1f EiB", (bytes >> 20) / 0x1p40); } } diff --git a/core/src/test/java/com/taobao/arthas/core/util/StringUtilsTest.java b/core/src/test/java/com/taobao/arthas/core/util/StringUtilsTest.java index 5e77c025e..2b76e80d0 100644 --- a/core/src/test/java/com/taobao/arthas/core/util/StringUtilsTest.java +++ b/core/src/test/java/com/taobao/arthas/core/util/StringUtilsTest.java @@ -20,12 +20,12 @@ public class StringUtilsTest { @Test public void testHumanReadableByteCount() { Assert.assertEquals(StringUtils.humanReadableByteCount(1023L), "1023 B"); - Assert.assertEquals(StringUtils.humanReadableByteCount(1024L), "1.00 KiB"); - Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024L), "1.00 MiB"); - Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024L - 100), "1023.90 KiB"); - Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024 * 1024L), "1.00 GiB"); - Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024 * 1024 * 1024L), "1.00 TiB"); - Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024 * 1024 * 1024 * 1024), "1.00 PiB"); + Assert.assertEquals(StringUtils.humanReadableByteCount(1024L), "1.0 KiB"); + Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024L), "1.0 MiB"); + Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024L - 100), "1023.9 KiB"); + Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024 * 1024L), "1.0 GiB"); + Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024 * 1024 * 1024L), "1.0 TiB"); + Assert.assertEquals(StringUtils.humanReadableByteCount(1024L * 1024 * 1024 * 1024 * 1024), "1.0 PiB"); } @Test