View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import java.util.regex.Matcher;
17  import java.util.regex.Pattern;
18  
19  /**
20   * Instances of this class represent the size of a file. Internally, the size is
21   * stored as long.
22   * 
23   * <p>The {@link #valueOf} method can convert strings such as "3 kb", "5 mb", into
24   * FileSize instances. The recognized unit specifications for file size are the
25   * "kb", "mb", and "gb". The unit name may be followed by an "s". Thus, "2 kbs"
26   * and "2 kb" are equivalent. In the absence of a time unit specification, byte
27   * is assumed.
28   *  
29   * @author Ceki G&uuml;lc&uuml;
30   * 
31   */
32  public class FileSize {
33  
34      private final static String LENGTH_PART = "([0-9]+)";
35      private final static int DOUBLE_GROUP = 1;
36  
37      private final static String UNIT_PART = "(|kb|mb|gb)s?";
38      private final static int UNIT_GROUP = 2;
39  
40      private static final Pattern FILE_SIZE_PATTERN = Pattern.compile(LENGTH_PART + "\\s*" + UNIT_PART, Pattern.CASE_INSENSITIVE);
41  
42      static public final long KB_COEFFICIENT = 1024;
43      static public final long MB_COEFFICIENT = 1024 * KB_COEFFICIENT;
44      static public final long GB_COEFFICIENT = 1024 * MB_COEFFICIENT;
45  
46      final long size;
47  
48      public FileSize(long size) {
49          this.size = size;
50      }
51  
52      public long getSize() {
53          return size;
54      }
55  
56      static public FileSize valueOf(String fileSizeStr) {
57          Matcher matcher = FILE_SIZE_PATTERN.matcher(fileSizeStr);
58  
59          long coefficient;
60          if (matcher.matches()) {
61              String lenStr = matcher.group(DOUBLE_GROUP);
62              String unitStr = matcher.group(UNIT_GROUP);
63  
64              long lenValue = Long.valueOf(lenStr);
65              if (unitStr.equalsIgnoreCase("")) {
66                  coefficient = 1;
67              } else if (unitStr.equalsIgnoreCase("kb")) {
68                  coefficient = KB_COEFFICIENT;
69              } else if (unitStr.equalsIgnoreCase("mb")) {
70                  coefficient = MB_COEFFICIENT;
71              } else if (unitStr.equalsIgnoreCase("gb")) {
72                  coefficient = GB_COEFFICIENT;
73              } else {
74                  throw new IllegalStateException("Unexpected " + unitStr);
75              }
76              return new FileSize(lenValue * coefficient);
77          } else {
78              throw new IllegalArgumentException("String value [" + fileSizeStr + "] is not in the expected format.");
79          }
80      }
81      
82      @Override
83      public String toString() {
84          long inKB = size / KB_COEFFICIENT;
85          
86          if(inKB == 0) 
87              return size + " Bytes";
88          
89          long inMB = size / MB_COEFFICIENT;
90          if(inMB == 0) {
91              return inKB + " KB";
92          }
93          
94          long inGB = size / GB_COEFFICIENT;
95          if(inGB == 0) {
96              return inMB + " MB";
97          }
98          
99          return inGB + " GB";
100         
101     }
102 }