Skip to content

Commit a2457f7

Browse files
committed
adding KB to StringUtil
1 parent 2b7172e commit a2457f7

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/main/java/edu/harvard/iq/dataverse/util/StringUtil.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,19 @@ static public String normalize(String sentence) {
233233
return sentence;
234234
}
235235

236-
public static final long BYTES_PER_MB = 1024L * 1024L;
236+
public static final long BYTES_PER_KB = 1024L;
237+
public static final long BYTES_PER_MB = BYTES_PER_KB * 1024L;
237238
public static final long BYTES_PER_GB = BYTES_PER_MB * 1024L;
238239
public static final long BYTES_PER_TB = BYTES_PER_GB * 1024L;
239240
static public String formatBytes(long bytes) {
240-
if (bytes < BYTES_PER_MB) {
241+
if (bytes < BYTES_PER_KB) {
241242
return NumberFormat.getNumberInstance().format(bytes) + " bytes";
242243
} else {
243244
double dBytes = bytes;
244245
NumberFormat formatter = new DecimalFormat("#,##0.00");
245-
if (bytes < BYTES_PER_GB) {
246+
if (bytes < BYTES_PER_MB) {
247+
return formatter.format(dBytes / BYTES_PER_KB) + " KB";
248+
} else if (bytes < BYTES_PER_GB) {
246249
return formatter.format(dBytes / BYTES_PER_MB) + " MB";
247250
} else if (bytes < BYTES_PER_TB) {
248251
return formatter.format(dBytes / BYTES_PER_GB) + " GB";

src/test/java/edu/harvard/iq/dataverse/util/StringUtilTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ public void testNormalize() {
242242

243243
@Test
244244
public void testFormatBytes() {
245-
assertEquals("1,048,575 bytes", StringUtil.formatBytes(StringUtil.BYTES_PER_MB - 1));
245+
assertEquals("1,023 bytes", StringUtil.formatBytes(StringUtil.BYTES_PER_KB - 1));
246+
assertEquals("1,024.00 KB", StringUtil.formatBytes(StringUtil.BYTES_PER_MB - 1));
246247
assertEquals("1,024.00 MB", StringUtil.formatBytes(StringUtil.BYTES_PER_GB - 1));
247248
assertEquals("1.00 GB", StringUtil.formatBytes(StringUtil.BYTES_PER_GB));
248249
assertEquals("341.33 GB", StringUtil.formatBytes(StringUtil.BYTES_PER_TB / 3));

0 commit comments

Comments
 (0)