FormatUtil { public static String formatBytesDecimal(long bytes) { if (bytes == 1L) { return String.format("%d byte", bytes); } else if (bytes < KILO) { return String.format("%d bytes", bytes); } else { return formatValue(bytes, "B"); } } private  FormatUtil(); static String formatBytes(long bytes); static String formatBytesDecimal(long bytes); static String formatHertz(long hertz); static String formatValue(long value, String unit); static String formatElapsedSecs(long secs); static float round(float d, int decimalPlace); static long getUnsignedInt(int x); static String toUnsignedString(int i); static String toUnsignedString(long l); static String formatError(int errorCode); static final String HEX_ERROR; }
@Test public void testFormatBytesDecimal() { assertEquals("0 bytes", FormatUtil.formatBytesDecimal(0)); assertEquals("1 byte", FormatUtil.formatBytesDecimal(1)); assertEquals("532 bytes", FormatUtil.formatBytesDecimal(532)); assertEquals("1 KB", FormatUtil.formatBytesDecimal(1000)); assertEquals("1 GB", FormatUtil.formatBytesDecimal(1000 * 1000 * 1000)); assertEquals("1 TB", FormatUtil.formatBytesDecimal(1000000000000L)); }
@Test public void testFormatBytesDecimalWithDecimalSeparator() { String expected1 = "1" + DECIMAL_SEPARATOR + "3 KB"; String expected2 = "2" + DECIMAL_SEPARATOR + "3 MB"; String expected3 = "2" + DECIMAL_SEPARATOR + "2 GB"; String expected4 = "1" + DECIMAL_SEPARATOR + "1 TB"; String expected5 = "3" + DECIMAL_SEPARATOR + "4 PB"; String expected6 = "5" + DECIMAL_SEPARATOR + "6 EB"; assertEquals(expected1, FormatUtil.formatBytesDecimal(1300)); assertEquals(expected2, FormatUtil.formatBytesDecimal(2300000)); assertEquals(expected3, FormatUtil.formatBytesDecimal(2200000000L)); assertEquals(expected4, FormatUtil.formatBytesDecimal(1100000000000L)); assertEquals(expected5, FormatUtil.formatBytesDecimal(3400000000000000L)); assertEquals(expected6, FormatUtil.formatBytesDecimal(5600000000000000000L)); }