001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.config.types;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.util.L10N;
033:
034: /**
035: * Returns a number of bytes, allowing EL expressions. Default is bytes.
036: *
037: * <pre>
038: * b : bytes
039: * k : kilobytes
040: * kb : kilobytes
041: * m : megabytes
042: * mb : megabytes
043: * g : gigabytes
044: * </pre>
045: */
046: public class Bytes {
047: private static final L10N L = new L10N(Bytes.class);
048:
049: public static final long BYTE = 1L;
050: public static final long KILOBYTE = 1024;
051: public static final long MEGABYTE = 1024 * 1024;
052: public static final long GIGABYTE = 1024 * 1024 * 1024;
053:
054: public static final long INFINITE = Long.MAX_VALUE / 2;
055:
056: private long _bytes;
057:
058: public Bytes() {
059: }
060:
061: public Bytes(long bytes) {
062: _bytes = bytes;
063: }
064:
065: /**
066: * Sets the text.
067: */
068: public void addText(String text) throws ConfigException {
069: _bytes = toBytes(text);
070: }
071:
072: /**
073: * Replace with the real bytes.
074: */
075: public long getBytes() {
076: return _bytes;
077: }
078:
079: /**
080: * Converts a byte string to a number of bytes. Default is bytes.
081: *
082: * <pre>
083: * b : bytes
084: * k : kilobytes
085: * kb : kilobytes
086: * m : megabytes
087: * mb : megabytes
088: * g : gigabytes
089: * </pre>
090: */
091: public static long toBytes(String bytes) throws ConfigException {
092: if (bytes == null)
093: return -1;
094:
095: long value = 0;
096: long sign = 1;
097: int i = 0;
098: int length = bytes.length();
099:
100: if (length == 0)
101: return -1;
102:
103: if (bytes.charAt(i) == '-') {
104: sign = -1;
105: i++;
106: } else if (bytes.charAt(i) == '+') {
107: i++;
108: }
109:
110: if (length <= i)
111: return -1;
112:
113: int ch;
114: for (; i < length && (ch = bytes.charAt(i)) >= '0' && ch <= '9'; i++)
115: value = 10 * value + ch - '0';
116:
117: value = sign * value;
118:
119: if (bytes.endsWith("gb") || bytes.endsWith("g")
120: || bytes.endsWith("G")) {
121: return value * 1024L * 1024L * 1024L;
122: } else if (bytes.endsWith("mb") || bytes.endsWith("m")
123: || bytes.endsWith("M")) {
124: return value * 1024L * 1024L;
125: } else if (bytes.endsWith("kb") || bytes.endsWith("k")
126: || bytes.endsWith("K")) {
127: return value * 1024L;
128: } else if (bytes.endsWith("b") || bytes.endsWith("B")) {
129: return value;
130: } else if (value < 0)
131: return value;
132: else {
133: throw new ConfigException(
134: L
135: .l(
136: "byte-valued expression `{0}' must have units. '16B' for bytes, '16K' for kilobytes, '16M' for megabytes, '16G' for gigabytes.",
137: bytes));
138: }
139: }
140: }
|