added ByteSize.java
[org.ibex.util.git] / src / org / ibex / util / ByteSize.java
1 // Copyright 2000-2006 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.util;
6
7 /** prints numbers (like 1000) as byte sizes (like 1mb) */
8 public class ByteSize {
9
10     public static String toString(int bytes) {
11
12         if (bytes < 1024)
13             return bytes+"b";
14
15         if (bytes < 1024 * 1024)
16             return (bytes/1024)+"kb";
17
18         if (bytes < 1024 * 1024 * 1024)
19             return (bytes/(1024*1024))+"mb";
20
21         if (bytes < 1024 * 1024 * 1024 * 1024)
22             return (bytes/(1024*1024*1024))+"gb";
23
24         return bytes+"b";
25     }
26
27 }