From 0cbd3ad53825076998f2bdbec308c9c206873901 Mon Sep 17 00:00:00 2001 From: Andrew DeMaria Date: Fri, 23 Dec 2016 12:10:54 -0700 Subject: [PATCH] Fix overflow and tests Signed-off-by: Andrew DeMaria --- .../org/libresonic/player/util/StringUtil.java | 16 ++++++++-------- .../player/util/StringUtilTestCase.java | 4 ++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/libresonic-main/src/main/java/org/libresonic/player/util/StringUtil.java b/libresonic-main/src/main/java/org/libresonic/player/util/StringUtil.java index b5124b91..d918ce4d 100644 --- a/libresonic-main/src/main/java/org/libresonic/player/util/StringUtil.java +++ b/libresonic-main/src/main/java/org/libresonic/player/util/StringUtil.java @@ -213,27 +213,27 @@ public final class StringUtil { public static synchronized String formatBytes(long byteCount, Locale locale) { // 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)); - return teraByteFormat.format((double) byteCount / (1024 * 1024 * 1024 * 1024)); + return teraByteFormat.format( ((double) byteCount ) / ((double) 1024 * 1024 * 1024 * 1024)); } // More than 1 GB? - if (byteCount >= 1024 * 1024 * 1024) { + if (byteCount >= 1024L * 1024 * 1024) { 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? - if (byteCount >= 1024 * 1024) { + if (byteCount >= 1024L * 1024) { 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? - if (byteCount >= 1024) { + if (byteCount >= 1024L) { 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"; diff --git a/libresonic-main/src/test/java/org/libresonic/player/util/StringUtilTestCase.java b/libresonic-main/src/test/java/org/libresonic/player/util/StringUtilTestCase.java index d8b73d5a..0f4d3f9b 100644 --- a/libresonic-main/src/test/java/org/libresonic/player/util/StringUtilTestCase.java +++ b/libresonic-main/src/test/java/org/libresonic/player/util/StringUtilTestCase.java @@ -68,6 +68,8 @@ public class StringUtilTestCase extends TestCase { 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().", "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", "", ""); 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().", "1,2 MB", StringUtil.formatBytes(1238476, 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() {