You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/04/18 15:06:21 UTC

svn commit: r1327496 - in /commons/proper/io/trunk/src: changes/changes.xml main/java/org/apache/commons/io/FileUtils.java test/java/org/apache/commons/io/FileUtilsTestCase.java

Author: ggregory
Date: Wed Apr 18 13:06:20 2012
New Revision: 1327496

URL: http://svn.apache.org/viewvc?rev=1327496&view=rev
Log:
[IO-327] Add byteCountToDisplaySize(BigInteger)

Modified:
    commons/proper/io/trunk/src/changes/changes.xml
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1327496&r1=1327495&r2=1327496&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Wed Apr 18 13:06:20 2012
@@ -47,13 +47,16 @@ The <action> type attribute can be add,u
   <body>
     <!-- The release date is the date RC is cut -->
     <release version="2.4" date="2012-TDB-TDB" description="">
-      <action issue="IO-326" dev="ggregory" type="fix" due-to="ggregory">
+      <action issue="IO-327" dev="ggregory" type="add" due-to="ggregory">
+        Add byteCountToDisplaySize(BigInteger).
+      </action>            
+      <action issue="IO-326" dev="ggregory" type="add" due-to="ggregory">
         Add new FileUtils.sizeOf[Directory] APIs to return BigInteger.
       </action>            
-      <action issue="IO-325" dev="ggregory" type="fix" due-to="raviprak">
+      <action issue="IO-325" dev="ggregory" type="add" due-to="raviprak">
         Add IOUtils.toByteArray methods to work with URL and URI.
       </action>            
-      <action issue="IO-324" dev="ggregory" type="fix" due-to="raviprak">
+      <action issue="IO-324" dev="ggregory" type="add" due-to="raviprak">
         Add missing Charset sister APIs to method that take a String charset name.
       </action>            
       <action issue="IO-319" dev="ggregory" type="fix" due-to="raviprak">

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1327496&r1=1327495&r2=1327496&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java Wed Apr 18 13:06:20 2012
@@ -87,11 +87,25 @@ public class FileUtils {
     public static final long ONE_KB = 1024;
 
     /**
+     * The number of bytes in a kilobyte.
+     * 
+     * @since 2.4
+     */
+    public static final BigInteger ONE_KB_BI = BigInteger.valueOf(ONE_KB);
+
+    /**
      * The number of bytes in a megabyte.
      */
     public static final long ONE_MB = ONE_KB * ONE_KB;
 
     /**
+     * The number of bytes in a megabyte.
+     * 
+     * @since 2.4
+     */
+    public static final BigInteger ONE_MB_BI = ONE_KB_BI.multiply(ONE_KB_BI);
+
+    /**
      * The file copy buffer size (30 MB)
      */
     private static final long FILE_COPY_BUFFER_SIZE = ONE_MB * 30;
@@ -102,21 +116,49 @@ public class FileUtils {
     public static final long ONE_GB = ONE_KB * ONE_MB;
 
     /**
+     * The number of bytes in a gigabyte.
+     * 
+     * @since 2.4
+     */
+    public static final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI);
+
+    /**
      * The number of bytes in a terabyte.
      */
     public static final long ONE_TB = ONE_KB * ONE_GB;
 
     /**
+     * The number of bytes in a terabyte.
+     * 
+     * @since 2.4
+     */
+    public static final BigInteger ONE_TB_BI = ONE_KB_BI.multiply(ONE_GB_BI);
+
+    /**
      * The number of bytes in a petabyte.
      */
     public static final long ONE_PB = ONE_KB * ONE_TB;
 
     /**
+     * The number of bytes in a petabyte.
+     * 
+     * @since 2.4
+     */
+    public static final BigInteger ONE_PB_BI = ONE_KB_BI.multiply(ONE_TB_BI);
+
+    /**
      * The number of bytes in an exabyte.
      */
     public static final long ONE_EB = ONE_KB * ONE_PB;
 
     /**
+     * The number of bytes in an exabyte.
+     * 
+     * @since 2.4
+     */
+    public static final BigInteger ONE_EB_BI = ONE_KB_BI.multiply(ONE_PB_BI);
+
+    /**
      * The number of bytes in a zettabyte.
      */
     public static final BigInteger ONE_ZB = BigInteger.valueOf(ONE_KB).multiply(BigInteger.valueOf(ONE_EB));
@@ -326,39 +368,63 @@ public class FileUtils {
 
     //-----------------------------------------------------------------------
     /**
-     * Returns a human-readable version of the file size, where the input
-     * represents a specific number of bytes.
-     * 
-     * 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 nearest GB boundary.
-     * 
+     * Returns a human-readable version of the file size, where the input represents a specific number of bytes.
+     * <p>
+     * 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
+     * nearest GB boundary.
+     * </p>
+     * <p>
      * Similarly for the 1MB and 1KB boundaries.
-     *
-     * @param size  the number of bytes
-     * @return a human-readable display value (includes units - GB, MB, KB or bytes)
+     * </p>
+     * 
+     * @param size
+     *            the number of bytes
+     * @return a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes)
+     * @see <a href="https://issues.apache.org/jira/browse/IO-226">IO-226 - should the rounding be changed?</a>
+     * @since 2.4
      */
     // See https://issues.apache.org/jira/browse/IO-226 - should the rounding be changed?
-    public static String byteCountToDisplaySize(long size) {
+    public static String byteCountToDisplaySize(BigInteger size) {
         String displaySize;
 
-        if (size / ONE_EB > 0) {
-            displaySize = String.valueOf(size / ONE_EB) + " EB";
-        } else if (size / ONE_PB > 0) {
-            displaySize = String.valueOf(size / ONE_PB) + " PB";
-        } else if (size / ONE_TB > 0) {
-            displaySize = String.valueOf(size / ONE_TB) + " TB";
-        } else if (size / ONE_GB > 0) {
-            displaySize = String.valueOf(size / ONE_GB) + " GB";
-        } else if (size / ONE_MB > 0) {
-            displaySize = String.valueOf(size / ONE_MB) + " MB";
-        } else if (size / ONE_KB > 0) {
-            displaySize = String.valueOf(size / ONE_KB) + " KB";
+        if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) {
+            displaySize = String.valueOf(size.divide(ONE_EB_BI)) + " EB";
+        } else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) {
+            displaySize = String.valueOf(size.divide(ONE_PB_BI)) + " PB";
+        } else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0) {
+            displaySize = String.valueOf(size.divide(ONE_TB_BI)) + " TB";
+        } else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
+            displaySize = String.valueOf(size.divide(ONE_GB_BI)) + " GB";
+        } else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
+            displaySize = String.valueOf(size.divide(ONE_MB_BI)) + " MB";
+        } else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
+            displaySize = String.valueOf(size.divide(ONE_KB_BI)) + " KB";
         } else {
             displaySize = String.valueOf(size) + " bytes";
         }
         return displaySize;
     }
 
+    /**
+     * Returns a human-readable version of the file size, where the input represents a specific number of bytes.
+     * <p>
+     * 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
+     * nearest GB boundary.
+     * </p>
+     * <p>
+     * Similarly for the 1MB and 1KB boundaries.
+     * </p>
+     * 
+     * @param size
+     *            the number of bytes
+     * @return a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes)
+     * @see <a href="https://issues.apache.org/jira/browse/IO-226">IO-226 - should the rounding be changed?</a>
+     */
+    // See https://issues.apache.org/jira/browse/IO-226 - should the rounding be changed?
+    public static String byteCountToDisplaySize(long size) {
+        return byteCountToDisplaySize(BigInteger.valueOf(size));
+    }
+
     //-----------------------------------------------------------------------
     /**
      * Implements the same behaviour as the "touch" utility on Unix. It creates

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java?rev=1327496&r1=1327495&r2=1327496&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Wed Apr 18 13:06:20 2012
@@ -314,7 +314,40 @@ public class FileUtilsTestCase extends F
 
     //-----------------------------------------------------------------------
     // byteCountToDisplaySize
-    public void testByteCountToDisplaySize() {
+    public void testByteCountToDisplaySizeBigInteger() {
+        final BigInteger b1023 = BigInteger.valueOf(1023);
+        final BigInteger b1025 = BigInteger.valueOf(1025);
+        final BigInteger KB1 = BigInteger.valueOf(1024);
+        final BigInteger MB1 = KB1.multiply(KB1);
+        final BigInteger GB1 = MB1.multiply(KB1);
+        final BigInteger GB2 = GB1.add(GB1);
+        final BigInteger TB1 = GB1.multiply(KB1);
+        final BigInteger PB1 = TB1.multiply(KB1);
+        final BigInteger EB1 = PB1.multiply(KB1);
+        assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.ZERO), "0 bytes");
+        assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.ONE), "1 bytes");
+        assertEquals(FileUtils.byteCountToDisplaySize(b1023), "1023 bytes");
+        assertEquals(FileUtils.byteCountToDisplaySize(KB1), "1 KB");
+        assertEquals(FileUtils.byteCountToDisplaySize(b1025), "1 KB");
+        assertEquals(FileUtils.byteCountToDisplaySize(MB1.subtract(BigInteger.ONE)), "1023 KB");
+        assertEquals(FileUtils.byteCountToDisplaySize(MB1), "1 MB");
+        assertEquals(FileUtils.byteCountToDisplaySize(MB1.add(BigInteger.ONE)), "1 MB");
+        assertEquals(FileUtils.byteCountToDisplaySize(GB1.subtract(BigInteger.ONE)), "1023 MB");
+        assertEquals(FileUtils.byteCountToDisplaySize(GB1), "1 GB");
+        assertEquals(FileUtils.byteCountToDisplaySize(GB1.add(BigInteger.ONE)), "1 GB");
+        assertEquals(FileUtils.byteCountToDisplaySize(GB2), "2 GB");
+        assertEquals(FileUtils.byteCountToDisplaySize(GB2.subtract(BigInteger.ONE)), "1 GB");
+        assertEquals(FileUtils.byteCountToDisplaySize(TB1), "1 TB");
+        assertEquals(FileUtils.byteCountToDisplaySize(PB1), "1 PB");
+        assertEquals(FileUtils.byteCountToDisplaySize(EB1), "1 EB");
+        assertEquals(FileUtils.byteCountToDisplaySize(Long.MAX_VALUE), "7 EB");
+        // Other MAX_VALUEs
+        assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Character.MAX_VALUE)), "63 KB");
+        assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Short.MAX_VALUE)), "31 KB");
+        assertEquals(FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Integer.MAX_VALUE)), "1 GB");
+    }
+
+    public void testByteCountToDisplaySizeLong() {
         assertEquals(FileUtils.byteCountToDisplaySize(0), "0 bytes");
         assertEquals(FileUtils.byteCountToDisplaySize(1), "1 bytes");
         assertEquals(FileUtils.byteCountToDisplaySize(1023), "1023 bytes");
@@ -326,6 +359,7 @@ public class FileUtilsTestCase extends F
         assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1023), "1023 MB");
         assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1024), "1 GB");
         assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1025), "1 GB");
+        assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 2), "2 GB");
         assertEquals(FileUtils.byteCountToDisplaySize(1024 * 1024 * 1024 * 2 - 1), "1 GB");
         assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024), "1 TB");
         assertEquals(FileUtils.byteCountToDisplaySize(1024L * 1024 * 1024 * 1024 * 1024), "1 PB");