Skip to content

Commit f7c09b9

Browse files
committed
[IO-226] question with byteCountToDisplaySize(long size). Add more tests. Add commented out code for processing terabyte, petabyte and exabyte. Add constants for terabyte, petabyte and exabyte, zettabyte, and yottabyte.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1153952 13f79535-47bb-0310-9956-ffa450edef68
1 parent f9bc6b9 commit f7c09b9

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.io.OutputStream;
27+
import java.math.BigInteger;
2728
import java.net.URL;
2829
import java.net.URLConnection;
2930
import java.nio.ByteBuffer;
@@ -109,6 +110,31 @@ public FileUtils() {
109110
*/
110111
public static final long ONE_GB = ONE_KB * ONE_MB;
111112

113+
/**
114+
* The number of bytes in a terabyte.
115+
*/
116+
public static final long ONE_TB = ONE_KB * ONE_GB;
117+
118+
/**
119+
* The number of bytes in a petabyte.
120+
*/
121+
public static final long ONE_PB = ONE_KB * ONE_TB;
122+
123+
/**
124+
* The number of bytes in an exabyte.
125+
*/
126+
public static final long ONE_EB = ONE_KB * ONE_PB;
127+
128+
/**
129+
* The number of bytes in a zettabyte.
130+
*/
131+
public static final BigInteger ONE_ZB = BigInteger.valueOf(ONE_KB).multiply(BigInteger.valueOf(ONE_EB));
132+
133+
/**
134+
* The number of bytes in a yottabyte.
135+
*/
136+
public static final BigInteger ONE_YB = ONE_ZB.multiply(BigInteger.valueOf(ONE_EB));
137+
112138
/**
113139
* An empty array of type <code>File</code>.
114140
*/
@@ -324,6 +350,13 @@ public static FileOutputStream openOutputStream(File file, boolean append) throw
324350
public static String byteCountToDisplaySize(long size) {
325351
String displaySize;
326352

353+
// if (size / ONE_EB > 0) {
354+
// displaySize = String.valueOf(size / ONE_EB) + " EB";
355+
// } else if (size / ONE_PB > 0) {
356+
// displaySize = String.valueOf(size / ONE_PB) + " PB";
357+
// } else if (size / ONE_TB > 0) {
358+
// displaySize = String.valueOf(size / ONE_TB) + " TB";
359+
// } else
327360
if (size / ONE_GB > 0) {
328361
displaySize = String.valueOf(size / ONE_GB) + " GB";
329362
} else if (size / ONE_MB > 0) {

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,25 @@ public void test_openOutputStream_notExistsCannotCreate() throws Exception {
311311
// byteCountToDisplaySize
312312
public void testByteCountToDisplaySize() {
313313
assertEquals(FileUtils.byteCountToDisplaySize(0), "0 bytes");
314+
assertEquals(FileUtils.byteCountToDisplaySize(1), "1 bytes");
315+
assertEquals(FileUtils.byteCountToDisplaySize(1023), "1023 bytes");
314316
assertEquals(FileUtils.byteCountToDisplaySize(1024), "1 KB");
317+
assertEquals(FileUtils.byteCountToDisplaySize(1025), "1 KB");
318+
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1023), "1023 KB");
315319
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024), "1 MB");
316-
assertEquals(
317-
FileUtils.byteCountToDisplaySize(1024 * 1024 * 1024),
318-
"1 GB");
320+
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1025), "1 MB");
321+
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1023), "1023 MB");
322+
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1024), "1 GB");
323+
assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1025), "1 GB");
324+
assertEquals(FileUtils.byteCountToDisplaySize((1024 * 1024 * 1024 * 2) - 1), "1 GB");
325+
// assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024), "1 TB");
326+
// assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024 * 1024), "1 PB");
327+
// assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024 * 1024 * 1024), "1 EB");
328+
// assertEquals(FileUtils.byteCountToDisplaySize(Long.MAX_VALUE), "7 EB");
329+
// Other MAX_VALUEs
330+
assertEquals(FileUtils.byteCountToDisplaySize(Character.MAX_VALUE), "63 KB");
331+
assertEquals(FileUtils.byteCountToDisplaySize(Short.MAX_VALUE), "31 KB");
332+
assertEquals(FileUtils.byteCountToDisplaySize(Integer.MAX_VALUE), "1 GB");
319333
}
320334

321335
//-----------------------------------------------------------------------

0 commit comments

Comments
 (0)