001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd;
019:
020: import java.text.DecimalFormat;
021: import java.text.DecimalFormatSymbols;
022:
023: /**
024: * See http://physics.nist.gov/cuu/Units/binary.html for an explanation of binary multiples.
025: *
026: * @author mog
027: * @version $Id: Bytes.java 858 2004-12-08 05:03:47Z mog $
028: */
029: public class Bytes {
030: private static final DecimalFormat FORMAT;
031:
032: static {
033: DecimalFormatSymbols formatsymbols = new DecimalFormatSymbols();
034: formatsymbols.setDecimalSeparator('.');
035: FORMAT = new DecimalFormat("0.0", formatsymbols);
036: FORMAT.setDecimalSeparatorAlwaysShown(true);
037: }
038:
039: public static final long GIBI = 1073741824L;
040: public static final long GIGA = 1000000000L;
041: public static final long KIBI = 1024L;
042: public static final long KILO = 1000L;
043: public static final long MEBI = 1048576L;
044: public static final long MEGA = 1000000L;
045: private static final Multiple[] MULTIPLES = new Multiple[] {
046: new Multiple('E', 1000000000000000000L,
047: 1152921504606846976L),
048: new Multiple('P', 1000000000000000L, 1125899906842624L),
049: new Multiple('T', 1000000000000L, 1099511627776L),
050: new Multiple('G', 1000000000L, 1073741824L),
051: new Multiple('M', 1000000L, 1048576L),
052: new Multiple('K', 1000L, 1024L) };
053: public static final long PETA = 1000000000000000L;
054: public static final long TEBI = 1099511627776L;
055: public static final long TERRA = 1000000000000L;
056:
057: public static String formatBytes(long bytes) {
058: return formatBytes(bytes, System.getProperty("bytes.binary",
059: "false").equals("true"));
060: }
061:
062: public static String formatBytes(long bytes, boolean binary) {
063: long absbytes = Math.abs(bytes);
064:
065: for (int i = 0; i < MULTIPLES.length; i++) {
066: Multiple multiple = MULTIPLES[i];
067: long multipleVal = binary ? multiple.getBinaryMultiple()
068: : multiple.getMultiple();
069:
070: if (absbytes >= multipleVal) {
071: return Bytes.FORMAT.format((float) bytes / multipleVal)
072: + multiple.getSuffix() + (binary ? "i" : "")
073: + "B";
074: }
075: }
076:
077: return bytes + "B";
078: }
079:
080: /**
081: * Parse a string representation of an amount of bytes. The suffix b is optional and makes no different, this method is case insensitive.
082: * <p>
083: * For example:
084: * 1000 = 1000 bytes
085: * 1000b = 1000 bytes
086: * 1000B = 1000 bytes
087: * 1k = 1000 bytes
088: * 1kb = 1000 bytes
089: * 1t = 1 terrabyte
090: * 1tib = 1 tebibyte
091: */
092: public static long parseBytes(String str)
093: throws NumberFormatException {
094: str = str.toUpperCase();
095:
096: if (str.endsWith("B")) {
097: str = str.substring(0, str.length() - 1);
098: }
099:
100: boolean binary = false;
101:
102: if (str.endsWith("I")) {
103: str = str.substring(0, str.length() - 1);
104: binary = true;
105: }
106:
107: char suffix = Character.toUpperCase(str
108: .charAt(str.length() - 1));
109:
110: if (Character.isDigit(suffix)) {
111: return Long.parseLong(str);
112: }
113:
114: str = str.substring(0, str.length() - 1);
115:
116: for (int i = 0; i < MULTIPLES.length; i++) {
117: Multiple multiple = MULTIPLES[i];
118:
119: //long multiple = ;
120: if (suffix == multiple.getSuffix()) {
121: return Long.parseLong(str)
122: * (binary ? multiple.getBinaryMultiple()
123: : multiple.getMultiple());
124: }
125: }
126:
127: throw new IllegalArgumentException("Unknown suffix " + suffix);
128: }
129:
130: private static class Multiple {
131: private long _binaryMultiple;
132: private long _multiple;
133: private char _suffix;
134:
135: public Multiple(char suffix, long multiple, long binarymultiple) {
136: _suffix = suffix;
137: _multiple = multiple;
138: _binaryMultiple = binarymultiple;
139: }
140:
141: public long getBinaryMultiple() {
142: return _binaryMultiple;
143: }
144:
145: public long getMultiple() {
146: return _multiple;
147: }
148:
149: public char getSuffix() {
150: return _suffix;
151: }
152: }
153: }
|