001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.common.utils;
020
021/**
022 * Class containing helpers related to the expression of sizes in bytes, kilobytes, etc.
023 */
024public class SizeUtils {
025
026    private SizeUtils() {
027        // utility class
028    }
029
030    public static final long KB = 1024;
031
032    public static final long MB = KB * KB;
033
034    public static final long GB = MB * KB;
035
036    public static final long TB = GB * KB;
037
038    /**
039     * Returns the number of bytes corresponding to a string expressing a size.
040     * <p>
041     * The suffixes KB, MB, GB, TB are recognized (in any case).
042     *
043     * @param string the size as a string
044     * @return the size
045     * @throws NumberFormatException if the size cannot be parsed
046     */
047    public static long parseSizeInBytes(String string) throws NumberFormatException {
048        String digits = string;
049        if (digits.length() == 0) {
050            throw new NumberFormatException("Invalid empty string");
051        }
052        char unit = digits.charAt(digits.length() - 1);
053        if (unit == 'b' || unit == 'B') {
054            digits = digits.substring(0, digits.length() - 1);
055            if (digits.length() == 0) {
056                throw new NumberFormatException(string);
057            }
058            unit = digits.charAt(digits.length() - 1);
059        }
060        long mul;
061        switch (unit) {
062        case 'k':
063        case 'K':
064            mul = KB;
065            break;
066        case 'm':
067        case 'M':
068            mul = MB;
069            break;
070        case 'g':
071        case 'G':
072            mul = GB;
073            break;
074        case 't':
075        case 'T':
076            mul = TB;
077            break;
078        default:
079            if (!Character.isDigit(unit)) {
080                throw new NumberFormatException(string);
081            }
082            mul = 1;
083        }
084        if (mul != 1) {
085            digits = digits.substring(0, digits.length() - 1);
086            if (digits.length() == 0) {
087                throw new NumberFormatException(string);
088            }
089        }
090        try {
091            return Long.parseLong(digits.trim()) * mul;
092        } catch (NumberFormatException e) {
093            throw new NumberFormatException(string);
094        }
095    }
096
097}