KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > types > Bytes


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.config.types;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.util.L10N;
33
34 /**
35  * Returns a number of bytes, allowing EL expressions. Default is bytes.
36  *
37  * <pre>
38  * b : bytes
39  * k : kilobytes
40  * kb : kilobytes
41  * m : megabytes
42  * mb : megabytes
43  * g : gigabytes
44  * </pre>
45  */

46 public class Bytes {
47   private static final L10N L = new L10N(Bytes.class);
48
49   public static final long BYTE = 1L;
50   public static final long KILOBYTE = 1024;
51   public static final long MEGABYTE = 1024 * 1024;
52   public static final long GIGABYTE = 1024 * 1024 * 1024;
53
54   public static final long INFINITE = Long.MAX_VALUE / 2;
55
56   private long _bytes;
57
58   public Bytes()
59   {
60   }
61
62   public Bytes(long bytes)
63   {
64     _bytes = bytes;
65   }
66
67   /**
68    * Sets the text.
69    */

70   public void addText(String JavaDoc text)
71     throws ConfigException
72   {
73     _bytes = toBytes(text);
74   }
75
76   /**
77    * Replace with the real bytes.
78    */

79   public long getBytes()
80   {
81     return _bytes;
82   }
83
84   /**
85    * Converts a byte string to a number of bytes. Default is bytes.
86    *
87    * <pre>
88    * b : bytes
89    * k : kilobytes
90    * kb : kilobytes
91    * m : megabytes
92    * mb : megabytes
93    * g : gigabytes
94    * </pre>
95    */

96   public static long toBytes(String JavaDoc bytes)
97     throws ConfigException
98   {
99     if (bytes == null)
100       return -1;
101
102     long value = 0;
103     long sign = 1;
104     int i = 0;
105     int length = bytes.length();
106
107     if (length == 0)
108       return -1;
109
110     if (bytes.charAt(i) == '-') {
111       sign = -1;
112       i++;
113     }
114     else if (bytes.charAt(i) == '+') {
115       i++;
116     }
117
118     if (length <= i)
119       return -1;
120
121     int ch;
122     for (; i < length && (ch = bytes.charAt(i)) >= '0' && ch <= '9'; i++)
123       value = 10 * value + ch - '0';
124
125     value = sign * value;
126
127     if (bytes.endsWith("gb") || bytes.endsWith("g") || bytes.endsWith("G")) {
128       return value * 1024L * 1024L * 1024L;
129     }
130     else if (bytes.endsWith("mb") || bytes.endsWith("m") || bytes.endsWith("M")) {
131       return value * 1024L * 1024L;
132     }
133     else if (bytes.endsWith("kb") || bytes.endsWith("k") || bytes.endsWith("K")) {
134       return value * 1024L;
135     }
136     else if (bytes.endsWith("b") || bytes.endsWith("B")) {
137       return value;
138     }
139     else if (value < 0)
140       return value;
141     else {
142       throw new ConfigException(L.l("byte-valued expression `{0}' must have units. '16B' for bytes, '16K' for kilobytes, '16M' for megabytes, '16G' for gigabytes.", bytes));
143     }
144   }
145 }
146
Popular Tags