Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, QOS.ch. All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
package ch.qos.logback.core.util;
Instances of this class represent the size of a file. Internally, the size is stored as long.>

The valueOf(java.lang.String) method can convert strings such as "3 kb", "5 mb", into FileSize instances. The recognized unit specifications for file size are the "kb", "mb", and "gb". The unit name may be followed by an "s". Thus, "2 kbs" and "2 kb" are equivalent. In the absence of a time unit specification, byte is assumed.

Author(s):
Ceki Gülcü
public class FileSize {
  private final static String LENGTH_PART = "([0-9]+)";
  private final static int DOUBLE_GROUP = 1;
  private final static String UNIT_PART = "(|kb|mb|gb)s?";
  private final static int UNIT_GROUP = 2;
  private static final Pattern FILE_SIZE_PATTERN = Pattern.compile(
      + "\\s*" + .);
  static final long KB_COEFFICIENT = 1024;
  static final long MB_COEFFICIENT = 1024 * ;
  static final long GB_COEFFICIENT = 1024 * ;
  final long size;
  FileSize(long size) {
    this. = size;
  }
  public long getSize() {
    return ;
  }
  static public FileSize valueOf(String fileSizeStr) {
    Matcher matcher = .matcher(fileSizeStr);
    long coefficient;
    if (matcher.matches()) {
      String lenStr = matcher.group();
      String unitStr = matcher.group();
      long lenValue = Long.valueOf(lenStr);
      if (unitStr.equalsIgnoreCase("")) {
        coefficient = 1;
      } else if (unitStr.equalsIgnoreCase("kb")) {
        coefficient = ;
      } else if (unitStr.equalsIgnoreCase("mb")) {
        coefficient = ;
      } else if (unitStr.equalsIgnoreCase("gb")) {
        coefficient = ;
      } else {
        throw new IllegalStateException("Unexpected " + unitStr);
      }
      return new FileSize(lenValue * coefficient);
    } else {
      throw new IllegalArgumentException("String value [" + fileSizeStr
          + "] is not in the expected format.");
    }
  }
New to GrepCode? Check out our FAQ X