Revision: 15205
          http://gate.svn.sourceforge.net/gate/?rev=15205&view=rev
Author:   valyt
Date:     2012-01-24 14:20:59 +0000 (Tue, 24 Jan 2012)
Log Message:
-----------
Utility method for displaying file sizes.

Modified Paths:
--------------
    gate/trunk/src/gate/util/Strings.java

Modified: gate/trunk/src/gate/util/Strings.java
===================================================================
--- gate/trunk/src/gate/util/Strings.java       2012-01-24 13:52:32 UTC (rev 
15204)
+++ gate/trunk/src/gate/util/Strings.java       2012-01-24 14:20:59 UTC (rev 
15205)
@@ -164,6 +164,23 @@
   }
 
   /**
+   * Converts a size given as a number of bytes (e.g. a file size) to a 
+   * human-readable string.
+   * @param bytes the size to be converted.
+   * @param base10 if <code>true</code> then the multiplier used is 10^3 (i.e. 
+   * 1000 bytes are reported as 1kB, etc.); otherwise 1024 is used as a 
+   * multiplier (1024 bytes is 1KiB, etc.).
+   * @return
+   */
+  public static String humanReadableByteCount(long bytes, boolean base10) {
+    int unit = base10 ? 1000 : 1024;
+    if (bytes < unit) return bytes + " B";
+    int exp = (int) (Math.log(bytes) / Math.log(unit));
+    String pre = (base10 ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (base10 ? "" : 
"i");
+    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
+  }
+  
+  /**
    * Convert about any object to a human readable string.<br>
    * Use {@link Arrays#deepToString(Object[])} to convert an array or
    * a collection.

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to