KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > util > MemorySize


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.util;
18
19 /**
20  * Memory Size Class
21  * <p>
22  * Convenience class to convert memory size value specified as 'nK' for kilobytes, 'nM' for
23  * megabytes and 'nG' for gigabytes, to an absolute value.
24  */

25 public class MemorySize
26 {
27     // Convertor constants
28

29     public static final long KILOBYTE = 1024L;
30     public static final long MEGABYTE = 1024L * KILOBYTE;
31     public static final long GIGABYTE = 1024L * MEGABYTE;
32     public static final long TERABYTE = 1024L * GIGABYTE;
33
34     /**
35      * Convert a memory size to an integer byte value.
36      *
37      * @param memSize String
38      * @return int
39      * @exception NumberFormatException
40      */

41     public static final int getByteValueInt(String JavaDoc memSize)
42     {
43         return (int) (getByteValue(memSize) & 0xFFFFFFFFL);
44     }
45
46     /**
47      * Convert a memory size to a byte value
48      *
49      * @param memSize String
50      * @return long
51      * @exception NumberFormatException
52      */

53     public static final long getByteValue(String JavaDoc memSize)
54     {
55
56         // Check if the string is valid
57

58         if (memSize == null || memSize.length() == 0)
59             return -1L;
60
61         // Check for a kilobyte value
62

63         String JavaDoc sizeStr = memSize.toUpperCase();
64         long mult = 1;
65         long val = 0;
66
67         if (sizeStr.endsWith("K"))
68         {
69
70             // Use the kilobyte multiplier
71

72             mult = KILOBYTE;
73             val = getValue(sizeStr);
74         }
75         else if (sizeStr.endsWith("M"))
76         {
77
78             // Use the megabyte nultiplier
79

80             mult = MEGABYTE;
81             val = getValue(sizeStr);
82         }
83         else if (sizeStr.endsWith("G"))
84         {
85
86             // Use the gigabyte multiplier
87

88             mult = GIGABYTE;
89             val = getValue(sizeStr);
90         }
91         else if (sizeStr.endsWith("T"))
92         {
93
94             // Use the terabyte multiplier
95

96             mult = TERABYTE;
97             val = getValue(sizeStr);
98         }
99         else
100         {
101
102             // Convert a numeric byte value
103

104             val = Long.valueOf(sizeStr).longValue();
105         }
106
107         // Apply the multiplier
108

109         return val * mult;
110     }
111
112     /**
113      * Get the size value from a string and return the numeric value
114      *
115      * @param val String
116      * @return long
117      * @exception NumberFormatException
118      */

119     private final static long getValue(String JavaDoc val)
120     {
121
122         // Strip the trailing size indicator
123

124         String JavaDoc sizStr = val.substring(0, val.length() - 1);
125         return Long.valueOf(sizStr).longValue();
126     }
127
128     /**
129      * Return a byte value as a kilobyte string
130      *
131      * @param val long
132      * @return String
133      */

134     public final static String JavaDoc asKilobyteString(long val)
135     {
136
137         // Calculate the kilobyte value
138

139         long mbVal = val / KILOBYTE;
140         return "" + mbVal + "Kb";
141     }
142
143     /**
144      * Return a byte value as a megabyte string
145      *
146      * @param val long
147      * @return String
148      */

149     public final static String JavaDoc asMegabyteString(long val)
150     {
151
152         // Calculate the megabyte value
153

154         long mbVal = val / MEGABYTE;
155         return "" + mbVal + "Mb";
156     }
157
158     /**
159      * Return a byte value as a gigabyte string
160      *
161      * @param val long
162      * @return String
163      */

164     public final static String JavaDoc asGigabyteString(long val)
165     {
166
167         // Calculate the gigabyte value
168

169         long mbVal = val / GIGABYTE;
170         return "" + mbVal + "Gb";
171     }
172
173     /**
174      * Return a byte value as a terabyte string
175      *
176      * @param val long
177      * @return String
178      */

179     public final static String JavaDoc asTerabyteString(long val)
180     {
181
182         // Calculate the terabyte value
183

184         long mbVal = val / TERABYTE;
185         return "" + mbVal + "Tb";
186     }
187
188     /**
189      * Return a byte value as a scaled string
190      *
191      * @param val long
192      * @return String
193      */

194     public final static String JavaDoc asScaledString(long val)
195     {
196
197         // Determine the scaling to apply
198

199         String JavaDoc ret = null;
200
201         if (val < (KILOBYTE * 2L))
202             ret = Long.toString(val);
203         else if (val < (MEGABYTE * 2L))
204             ret = asKilobyteString(val);
205         else if (val < (GIGABYTE * 2L))
206             ret = asMegabyteString(val);
207         else if (val < (TERABYTE * 2L))
208             ret = asGigabyteString(val);
209         else
210             ret = asTerabyteString(val);
211
212         return ret;
213     }
214 }
215
Popular Tags