Fix overflow and tests

Signed-off-by: Andrew DeMaria <lostonamountain@gmail.com>
This commit is contained in:
Andrew DeMaria
2016-12-23 12:10:54 -07:00
parent 30bc5a90f5
commit 0cbd3ad538
2 changed files with 12 additions and 8 deletions
@@ -213,27 +213,27 @@ public final class StringUtil {
public static synchronized String formatBytes(long byteCount, Locale locale) { public static synchronized String formatBytes(long byteCount, Locale locale) {
// More than 1 TB? // More than 1 TB?
if (byteCount >= 1024 * 1024 * 1024 * 1024) { if (byteCount >= 1024L * 1024 * 1024 * 1024) {
NumberFormat teraByteFormat = new DecimalFormat("0.00 TB", new DecimalFormatSymbols(locale)); NumberFormat teraByteFormat = new DecimalFormat("0.00 TB", new DecimalFormatSymbols(locale));
return teraByteFormat.format((double) byteCount / (1024 * 1024 * 1024 * 1024)); return teraByteFormat.format( ((double) byteCount ) / ((double) 1024 * 1024 * 1024 * 1024));
} }
// More than 1 GB? // More than 1 GB?
if (byteCount >= 1024 * 1024 * 1024) { if (byteCount >= 1024L * 1024 * 1024) {
NumberFormat gigaByteFormat = new DecimalFormat("0.00 GB", new DecimalFormatSymbols(locale)); NumberFormat gigaByteFormat = new DecimalFormat("0.00 GB", new DecimalFormatSymbols(locale));
return gigaByteFormat.format((double) byteCount / (1024 * 1024 * 1024)); return gigaByteFormat.format((double) byteCount / ((double) 1024 * 1024 * 1024));
} }
// More than 1 MB? // More than 1 MB?
if (byteCount >= 1024 * 1024) { if (byteCount >= 1024L * 1024) {
NumberFormat megaByteFormat = new DecimalFormat("0.0 MB", new DecimalFormatSymbols(locale)); NumberFormat megaByteFormat = new DecimalFormat("0.0 MB", new DecimalFormatSymbols(locale));
return megaByteFormat.format((double) byteCount / (1024 * 1024)); return megaByteFormat.format((double) byteCount / ((double) 1024 * 1024));
} }
// More than 1 KB? // More than 1 KB?
if (byteCount >= 1024) { if (byteCount >= 1024L) {
NumberFormat kiloByteFormat = new DecimalFormat("0 KB", new DecimalFormatSymbols(locale)); NumberFormat kiloByteFormat = new DecimalFormat("0 KB", new DecimalFormatSymbols(locale));
return kiloByteFormat.format((double) byteCount / 1024); return kiloByteFormat.format((double) byteCount / ((double) 1024));
} }
return byteCount + " B"; return byteCount + " B";
@@ -68,6 +68,8 @@ public class StringUtilTestCase extends TestCase {
assertEquals("Error in formatBytes().", "1024 KB", StringUtil.formatBytes(1048575, locale)); assertEquals("Error in formatBytes().", "1024 KB", StringUtil.formatBytes(1048575, locale));
assertEquals("Error in formatBytes().", "1.2 MB", StringUtil.formatBytes(1238476, locale)); assertEquals("Error in formatBytes().", "1.2 MB", StringUtil.formatBytes(1238476, locale));
assertEquals("Error in formatBytes().", "3.50 GB", StringUtil.formatBytes(3758096384L, locale)); assertEquals("Error in formatBytes().", "3.50 GB", StringUtil.formatBytes(3758096384L, locale));
assertEquals("Error in formatBytes().", "410.00 TB", StringUtil.formatBytes(450799767388160L, locale));
assertEquals("Error in formatBytes().", "4413.43 TB", StringUtil.formatBytes(4852617603375432L, locale));
locale = new Locale("no", "", ""); locale = new Locale("no", "", "");
assertEquals("Error in formatBytes().", "918 B", StringUtil.formatBytes(918, locale)); assertEquals("Error in formatBytes().", "918 B", StringUtil.formatBytes(918, locale));
@@ -77,6 +79,8 @@ public class StringUtilTestCase extends TestCase {
assertEquals("Error in formatBytes().", "1024 KB", StringUtil.formatBytes(1048575, locale)); assertEquals("Error in formatBytes().", "1024 KB", StringUtil.formatBytes(1048575, locale));
assertEquals("Error in formatBytes().", "1,2 MB", StringUtil.formatBytes(1238476, locale)); assertEquals("Error in formatBytes().", "1,2 MB", StringUtil.formatBytes(1238476, locale));
assertEquals("Error in formatBytes().", "3,50 GB", StringUtil.formatBytes(3758096384L, locale)); assertEquals("Error in formatBytes().", "3,50 GB", StringUtil.formatBytes(3758096384L, locale));
assertEquals("Error in formatBytes().", "410,00 TB", StringUtil.formatBytes(450799767388160L, locale));
assertEquals("Error in formatBytes().", "4413,43 TB", StringUtil.formatBytes(4852617603375432L, locale));
} }
public void testFormatDuration() { public void testFormatDuration() {