added ByteSize.java
[org.ibex.util.git] / src / org / ibex / util / ByteSize.java
diff --git a/src/org/ibex/util/ByteSize.java b/src/org/ibex/util/ByteSize.java
new file mode 100644 (file)
index 0000000..2f1c003
--- /dev/null
@@ -0,0 +1,27 @@
+// Copyright 2000-2006 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
+package org.ibex.util;
+
+/** prints numbers (like 1000) as byte sizes (like 1mb) */
+public class ByteSize {
+
+    public static String toString(int bytes) {
+
+        if (bytes < 1024)
+            return bytes+"b";
+
+        if (bytes < 1024 * 1024)
+            return (bytes/1024)+"kb";
+
+        if (bytes < 1024 * 1024 * 1024)
+            return (bytes/(1024*1024))+"mb";
+
+        if (bytes < 1024 * 1024 * 1024 * 1024)
+            return (bytes/(1024*1024*1024))+"gb";
+
+        return bytes+"b";
+    }
+
+}