001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.util;
015
016import java.util.regex.Matcher;
017import java.util.regex.Pattern;
018
019/**
020 * Instances of this class represent the size of a file. Internally, the size is
021 * stored as long.
022 * 
023 * <p>The {@link #valueOf} method can convert strings such as "3 kb", "5 mb", into
024 * FileSize instances. The recognized unit specifications for file size are the
025 * "kb", "mb", and "gb". The unit name may be followed by an "s". Thus, "2 kbs"
026 * and "2 kb" are equivalent. In the absence of a time unit specification, byte
027 * is assumed.
028 *  
029 * @author Ceki G&uuml;lc&uuml;
030 * 
031 */
032public class FileSize {
033
034    private final static String LENGTH_PART = "([0-9]+)";
035    private final static int DOUBLE_GROUP = 1;
036
037    private final static String UNIT_PART = "(|kb|mb|gb)s?";
038    private final static int UNIT_GROUP = 2;
039
040    private static final Pattern FILE_SIZE_PATTERN = Pattern.compile(LENGTH_PART + "\\s*" + UNIT_PART, Pattern.CASE_INSENSITIVE);
041
042    static public final long KB_COEFFICIENT = 1024;
043    static public final long MB_COEFFICIENT = 1024 * KB_COEFFICIENT;
044    static public final long GB_COEFFICIENT = 1024 * MB_COEFFICIENT;
045
046    final long size;
047
048    public FileSize(long size) {
049        this.size = size;
050    }
051
052    public long getSize() {
053        return size;
054    }
055
056    static public FileSize valueOf(String fileSizeStr) {
057        Matcher matcher = FILE_SIZE_PATTERN.matcher(fileSizeStr);
058
059        long coefficient;
060        if (matcher.matches()) {
061            String lenStr = matcher.group(DOUBLE_GROUP);
062            String unitStr = matcher.group(UNIT_GROUP);
063
064            long lenValue = Long.valueOf(lenStr);
065            if (unitStr.equalsIgnoreCase("")) {
066                coefficient = 1;
067            } else if (unitStr.equalsIgnoreCase("kb")) {
068                coefficient = KB_COEFFICIENT;
069            } else if (unitStr.equalsIgnoreCase("mb")) {
070                coefficient = MB_COEFFICIENT;
071            } else if (unitStr.equalsIgnoreCase("gb")) {
072                coefficient = GB_COEFFICIENT;
073            } else {
074                throw new IllegalStateException("Unexpected " + unitStr);
075            }
076            return new FileSize(lenValue * coefficient);
077        } else {
078            throw new IllegalArgumentException("String value [" + fileSizeStr + "] is not in the expected format.");
079        }
080    }
081    
082    @Override
083    public String toString() {
084        long inKB = size / KB_COEFFICIENT;
085        
086        if(inKB == 0) 
087            return size + " Bytes";
088        
089        long inMB = size / MB_COEFFICIENT;
090        if(inMB == 0) {
091            return inKB + " KB";
092        }
093        
094        long inGB = size / GB_COEFFICIENT;
095        if(inGB == 0) {
096            return inMB + " MB";
097        }
098        
099        return inGB + " GB";
100        
101    }
102}