improve humanReadableByteCount

pull/966/head
hengyunabc 5 years ago
parent 50f8cf862f
commit 19aa01b092

@ -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);
}
}

@ -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

Loading…
Cancel
Save