Skip to content

Commit d731b2b

Browse files
committed
[IO-327] Add byteCountToDisplaySize(BigInteger)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1327496 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2dab8d5 commit d731b2b

3 files changed

Lines changed: 129 additions & 26 deletions

File tree

src/changes/changes.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.4" date="2012-TDB-TDB" description="">
50-
<action issue="IO-326" dev="ggregory" type="fix" due-to="ggregory">
50+
<action issue="IO-327" dev="ggregory" type="add" due-to="ggregory">
51+
Add byteCountToDisplaySize(BigInteger).
52+
</action>
53+
<action issue="IO-326" dev="ggregory" type="add" due-to="ggregory">
5154
Add new FileUtils.sizeOf[Directory] APIs to return BigInteger.
5255
</action>
53-
<action issue="IO-325" dev="ggregory" type="fix" due-to="raviprak">
56+
<action issue="IO-325" dev="ggregory" type="add" due-to="raviprak">
5457
Add IOUtils.toByteArray methods to work with URL and URI.
5558
</action>
56-
<action issue="IO-324" dev="ggregory" type="fix" due-to="raviprak">
59+
<action issue="IO-324" dev="ggregory" type="add" due-to="raviprak">
5760
Add missing Charset sister APIs to method that take a String charset name.
5861
</action>
5962
<action issue="IO-319" dev="ggregory" type="fix" due-to="raviprak">

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 88 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,25 @@ public FileUtils() {
8686
*/
8787
public static final long ONE_KB = 1024;
8888

89+
/**
90+
* The number of bytes in a kilobyte.
91+
*
92+
* @since 2.4
93+
*/
94+
public static final BigInteger ONE_KB_BI = BigInteger.valueOf(ONE_KB);
95+
8996
/**
9097
* The number of bytes in a megabyte.
9198
*/
9299
public static final long ONE_MB = ONE_KB * ONE_KB;
93100

101+
/**
102+
* The number of bytes in a megabyte.
103+
*
104+
* @since 2.4
105+
*/
106+
public static final BigInteger ONE_MB_BI = ONE_KB_BI.multiply(ONE_KB_BI);
107+
94108
/**
95109
* The file copy buffer size (30 MB)
96110
*/
@@ -101,21 +115,49 @@ public FileUtils() {
101115
*/
102116
public static final long ONE_GB = ONE_KB * ONE_MB;
103117

118+
/**
119+
* The number of bytes in a gigabyte.
120+
*
121+
* @since 2.4
122+
*/
123+
public static final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI);
124+
104125
/**
105126
* The number of bytes in a terabyte.
106127
*/
107128
public static final long ONE_TB = ONE_KB * ONE_GB;
108129

130+
/**
131+
* The number of bytes in a terabyte.
132+
*
133+
* @since 2.4
134+
*/
135+
public static final BigInteger ONE_TB_BI = ONE_KB_BI.multiply(ONE_GB_BI);
136+
109137
/**
110138
* The number of bytes in a petabyte.
111139
*/
112140
public static final long ONE_PB = ONE_KB * ONE_TB;
113141

142+
/**
143+
* The number of bytes in a petabyte.
144+
*
145+
* @since 2.4
146+
*/
147+
public static final BigInteger ONE_PB_BI = ONE_KB_BI.multiply(ONE_TB_BI);
148+
114149
/**
115150
* The number of bytes in an exabyte.
116151
*/
117152
public static final long ONE_EB = ONE_KB * ONE_PB;
118153

154+
/**
155+
* The number of bytes in an exabyte.
156+
*
157+
* @since 2.4
158+
*/
159+
public static final BigInteger ONE_EB_BI = ONE_KB_BI.multiply(ONE_PB_BI);
160+
119161
/**
120162
* The number of bytes in a zettabyte.
121163
*/
@@ -326,39 +368,63 @@ public static FileOutputStream openOutputStream(File file, boolean append) throw
326368

327369
//-----------------------------------------------------------------------
328370
/**
329-
* Returns a human-readable version of the file size, where the input
330-
* represents a specific number of bytes.
331-
*
332-
* If the size is over 1GB, the size is returned as the number of whole GB,
333-
* i.e. the size is rounded down to the nearest GB boundary.
334-
*
371+
* Returns a human-readable version of the file size, where the input represents a specific number of bytes.
372+
* <p>
373+
* If the size is over 1GB, the size is returned as the number of whole GB, i.e. the size is rounded down to the
374+
* nearest GB boundary.
375+
* </p>
376+
* <p>
335377
* Similarly for the 1MB and 1KB boundaries.
336-
*
337-
* @param size the number of bytes
338-
* @return a human-readable display value (includes units - GB, MB, KB or bytes)
378+
* </p>
379+
*
380+
* @param size
381+
* the number of bytes
382+
* @return a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes)
383+
* @see <a href="https://issues.apache.org/jira/browse/IO-226">IO-226 - should the rounding be changed?</a>
384+
* @since 2.4
339385
*/
340386
// See https://issues.apache.org/jira/browse/IO-226 - should the rounding be changed?
341-
public static String byteCountToDisplaySize(long size) {
387+
public static String byteCountToDisplaySize(BigInteger size) {
342388
String displaySize;
343389

344-
if (size / ONE_EB > 0) {
345-
displaySize = String.valueOf(size / ONE_EB) + " EB";
346-
} else if (size / ONE_PB > 0) {
347-
displaySize = String.valueOf(size / ONE_PB) + " PB";
348-
} else if (size / ONE_TB > 0) {
349-
displaySize = String.valueOf(size / ONE_TB) + " TB";
350-
} else if (size / ONE_GB > 0) {
351-
displaySize = String.valueOf(size / ONE_GB) + " GB";
352-
} else if (size / ONE_MB > 0) {
353-
displaySize = String.valueOf(size / ONE_MB) + " MB";
354-
} else if (size / ONE_KB > 0) {
355-
displaySize = String.valueOf(size / ONE_KB) + " KB";
390+
if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) {
391+
displaySize = String.valueOf(size.divide(ONE_EB_BI)) + " EB";
392+
} else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) {
393+
displaySize = String.valueOf(size.divide(ONE_PB_BI)) + " PB";
394+
} else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0) {
395+
displaySize = String.valueOf(size.divide(ONE_TB_BI)) + " TB";
396+
} else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
397+
displaySize = String.valueOf(size.divide(ONE_GB_BI)) + " GB";
398+
} else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
399+
displaySize = String.valueOf(size.divide(ONE_MB_BI)) + " MB";
400+
} else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
401+
displaySize = String.valueOf(size.divide(ONE_KB_BI)) + " KB";
356402
} else {
357403
displaySize = String.valueOf(size) + " bytes";
358404
}
359405
return displaySize;
360406
}
361407

408+
/**
409+
* Returns a human-readable version of the file size, where the input represents a specific number of bytes.
410+
* <p>
411+
* If the size is over 1GB, the size is returned as the number of whole GB, i.e. the size is rounded down to the
412+
* nearest GB boundary.
413+
* </p>
414+
* <p>
415+
* Similarly for the 1MB and 1KB boundaries.
416+
* </p>
417+
*
418+
* @param size
419+
* the number of bytes
420+
* @return a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes)
421+
* @see <a href="https://issues.apache.org/jira/browse/IO-226">IO-226 - should the rounding be changed?</a>
422+
*/
423+
// See https://issues.apache.org/jira/browse/IO-226 - should the rounding be changed?
424+
public static String byteCountToDisplaySize(long size) {
425+
return byteCountToDisplaySize(BigInteger.valueOf(size));
426+
}
427+
362428
//-----------------------------------------------------------------------
363429
/**
364430
* Implements the same behaviour as the "touch" utility on Unix. It creates

src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,40 @@ public void test_openOutputStream_notExistsCannotCreate() throws Exception {
314314

315315
//-----------------------------------------------------------------------
316316
// byteCountToDisplaySize
317-
public void testByteCountToDisplaySize() {
317+
public void testByteCountToDisplaySizeBigInteger() {
318+
final BigInteger b1023 = BigInteger.valueOf(1023);
319+
final BigInteger b1025 = BigInteger.valueOf(1025);
320+
final BigInteger KB1 = BigInteger.valueOf(1024);
321+
final BigInteger MB1 = KB1.multiply(KB1);
322+
final BigInteger GB1 = MB1.multiply(KB1);
323+
final BigInteger GB2 = GB1.add(GB1);
324+
final BigInteger TB1 = GB1.multiply(KB1);
325+
final BigInteger PB1 = TB1.multiply(KB1);
326+
final BigInteger EB1 = PB1.multiply(KB1);
327+
assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.ZERO), "0 bytes");
328+
assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.ONE), "1 bytes");
329+
assertEquals(FileUtils.byteCountToDisplaySize(b1023), "1023 bytes");
330+
assertEquals(FileUtils.byteCountToDisplaySize(KB1), "1 KB");
331+
assertEquals(FileUtils.byteCountToDisplaySize(b1025), "1 KB");
332+
assertEquals(FileUtils.byteCountToDisplaySize(MB1.subtract(BigInteger.ONE)), "1023 KB");
333+
assertEquals(FileUtils.byteCountToDisplaySize(MB1), "1 MB");
334+
assertEquals(FileUtils.byteCountToDisplaySize(MB1.add(BigInteger.ONE)), "1 MB");
335+
assertEquals(FileUtils.byteCountToDisplaySize(GB1.subtract(BigInteger.ONE)), "1023 MB");
336+
assertEquals(FileUtils.byteCountToDisplaySize(GB1), "1 GB");
337+
assertEquals(FileUtils.byteCountToDisplaySize(GB1.add(BigInteger.ONE)), "1 GB");
338+
assertEquals(FileUtils.byteCountToDisplaySize(GB2), "2 GB");
339+
assertEquals(FileUtils.byteCountToDisplaySize(GB2.subtract(BigInteger.ONE)), "1 GB");
340+
assertEquals(FileUtils.byteCountToDisplaySize(TB1), "1 TB");
341+
assertEquals(FileUtils.byteCountToDisplaySize(PB1), "1 PB");
342+
assertEquals(FileUtils.byteCountToDisplaySize(EB1), "1 EB");
343+
assertEquals(FileUtils.byteCountToDisplaySize(Long.MAX_VALUE), "7 EB");
344+
// Other MAX_VALUEs
345+
assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Character.MAX_VALUE)), "63 KB");
346+
assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Short.MAX_VALUE)), "31 KB");
347+
assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Integer.MAX_VALUE)), "1 GB");
348+
}
349+
350+
public void testByteCountToDisplaySizeLong() {
318351
assertEquals(FileUtils.byteCountToDisplaySize(0), "0 bytes");
319352
assertEquals(FileUtils.byteCountToDisplaySize(1), "1 bytes");
320353
assertEquals(FileUtils.byteCountToDisplaySize(1023), "1023 bytes");
@@ -326,6 +359,7 @@ public void testByteCountToDisplaySize() {
326359
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1023), "1023 MB");
327360
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1024), "1 GB");
328361
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1025), "1 GB");
362+
assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 2), "2 GB");
329363
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1024 * 2 - 1), "1 GB");
330364
assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024), "1 TB");
331365
assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024 * 1024), "1 PB");

0 commit comments

Comments
 (0)