KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > Bytes


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd;
19
20 import java.text.DecimalFormat JavaDoc;
21 import java.text.DecimalFormatSymbols JavaDoc;
22
23 /**
24  * See http://physics.nist.gov/cuu/Units/binary.html for an explanation of binary multiples.
25  *
26  * @author mog
27  * @version $Id: Bytes.java,v 1.14 2004/02/10 00:03:04 mog Exp $
28  */

29 public class Bytes {
30     private static class Multiple {
31         private long _binaryMultiple;
32         private long _multiple;
33
34         private char _suffix;
35         public Multiple(char suffix, long multiple, long binarymultiple) {
36             _suffix = suffix;
37             _multiple = multiple;
38             _binaryMultiple = binarymultiple;
39         }
40
41         public long getBinaryMultiple() {
42             return _binaryMultiple;
43         }
44
45         public long getMultiple() {
46             return _multiple;
47         }
48
49         public char getSuffix() {
50             return _suffix;
51         }
52
53     }
54     private static final DecimalFormat JavaDoc FORMAT;
55     static {
56         DecimalFormatSymbols JavaDoc formatsymbols = new DecimalFormatSymbols JavaDoc();
57         //formatsymbols.setDecimalSeparator('.');
58
FORMAT = new DecimalFormat JavaDoc("0.0", formatsymbols);
59         FORMAT.setDecimalSeparatorAlwaysShown(true);
60     }
61
62     public static final long GIBI = 1073741824L;
63     public static final long GIGA = 1000000000L;
64     public static final long KIBI = 1024L;
65     public static final long KILO = 1000L;
66     public static final long MEBI = 1048576L;
67     public static final long MEGA = 1000000L;
68
69     private static final Multiple[] MULTIPLES =
70         new Multiple[] {
71             new Multiple('E', 1000000000000000000L, 1152921504606846976L),
72             new Multiple('P', 1000000000000000L, 1125899906842624L),
73             new Multiple('T', 1000000000000L, 1099511627776L),
74             new Multiple('G', 1000000000L, 1073741824L),
75             new Multiple('M', 1000000L, 1048576L),
76             new Multiple('K', 1000L, 1024L)};
77
78     public static final long PETA = 1000000000000000L;
79     public static final long TEBI = 1099511627776L;
80     public static final long TERRA = 1000000000000L;
81
82     public static String JavaDoc formatBytes(long bytes) {
83         return formatBytes(bytes, Boolean.getBoolean(System.getProperty("bytes.binary", "false")));
84     }
85
86     public static String JavaDoc formatBytes(long bytes, boolean binary) {
87         long absbytes = Math.abs(bytes);
88         for (int i = 0; i < MULTIPLES.length; i++) {
89             Multiple multiple = MULTIPLES[i];
90             long multipleVal =
91                 binary ? multiple.getBinaryMultiple() : multiple.getMultiple();
92             if (absbytes >= multipleVal) {
93                 return Bytes.FORMAT.format((float) bytes / multipleVal)
94                         + multiple.getSuffix()
95                         + (binary ? "i" : "")
96                         + "B";
97             }
98         }
99         return bytes+"B";
100     }
101     /**
102      * Parse a string representation of an amount of bytes. The suffix b is optional and makes no different, this method is case insensitive.
103      * <p>
104      * For example:
105      * 1000 = 1000 bytes
106      * 1000b = 1000 bytes
107      * 1000B = 1000 bytes
108      * 1k = 1000 bytes
109      * 1kb = 1000 bytes
110      * 1t = 1 terrabyte
111      * 1tib = 1 tebibyte
112      */

113     public static long parseBytes(String JavaDoc str) throws NumberFormatException JavaDoc {
114         str = str.toUpperCase();
115         if (str.endsWith("B"))
116             str = str.substring(0, str.length() - 1);
117
118         boolean binary = false;
119         if (str.endsWith("I")) {
120             str = str.substring(0, str.length() - 1);
121             binary = true;
122         }
123
124         char suffix = Character.toUpperCase(str.charAt(str.length() - 1));
125         if (Character.isDigit(suffix)) {
126             return Long.parseLong(str);
127         }
128         str = str.substring(0, str.length() - 1);
129
130         for (int i = 0; i < MULTIPLES.length; i++) {
131             Multiple multiple = MULTIPLES[i];
132             //long multiple = ;
133
if (suffix == multiple.getSuffix()) {
134                 return Long.parseLong(str)
135                     * (binary
136                         ? multiple.getBinaryMultiple()
137                         : multiple.getMultiple());
138             }
139         }
140         throw new IllegalArgumentException JavaDoc("Unknown suffix " + suffix);
141     }
142 }
143
Popular Tags