Fix overflow and tests

Signed-off-by: Andrew DeMaria <lostonamountain@gmail.com>
master
Andrew DeMaria 8 years ago
parent 30bc5a90f5
commit 0cbd3ad538
No known key found for this signature in database
GPG Key ID: 0A3F5E91F8364EDF
  1. 16
      libresonic-main/src/main/java/org/libresonic/player/util/StringUtil.java
  2. 4
      libresonic-main/src/test/java/org/libresonic/player/util/StringUtilTestCase.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";

@ -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() {

Loading…
Cancel
Save