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> 024 * The {@link #valueOf} method can convert strings such as "3 kb", "5 mb", into 025 * FileSize instances. The recognized unit specifications for file size are the 026 * "kb", "mb", and "gb". The unit name may be followed by an "s". Thus, "2 kbs" 027 * and "2 kb" are equivalent. In the absence of a time unit specification, byte 028 * is assumed. 029 * 030 * @author Ceki Gülcü 031 * 032 */ 033public class FileSize { 034 035 private final static String LENGTH_PART = "([0-9]+)"; 036 private final static int DOUBLE_GROUP = 1; 037 038 private final static String UNIT_PART = "(|kb|mb|gb)s?"; 039 private final static int UNIT_GROUP = 2; 040 041 private static final Pattern FILE_SIZE_PATTERN = Pattern.compile(LENGTH_PART + "\\s*" + UNIT_PART, 042 Pattern.CASE_INSENSITIVE); 043 044 static public final long KB_COEFFICIENT = 1024; 045 static public final long MB_COEFFICIENT = 1024 * KB_COEFFICIENT; 046 static public final long GB_COEFFICIENT = 1024 * MB_COEFFICIENT; 047 048 final long size; 049 050 public FileSize(long size) { 051 this.size = size; 052 } 053 054 public long getSize() { 055 return size; 056 } 057 058 static public FileSize valueOf(String fileSizeStr) { 059 Matcher matcher = FILE_SIZE_PATTERN.matcher(fileSizeStr); 060 061 long coefficient; 062 if (matcher.matches()) { 063 String lenStr = matcher.group(DOUBLE_GROUP); 064 String unitStr = matcher.group(UNIT_GROUP); 065 066 long lenValue = Long.valueOf(lenStr); 067 if (unitStr.equalsIgnoreCase("")) { 068 coefficient = 1; 069 } else if (unitStr.equalsIgnoreCase("kb")) { 070 coefficient = KB_COEFFICIENT; 071 } else if (unitStr.equalsIgnoreCase("mb")) { 072 coefficient = MB_COEFFICIENT; 073 } else if (unitStr.equalsIgnoreCase("gb")) { 074 coefficient = GB_COEFFICIENT; 075 } else { 076 throw new IllegalStateException("Unexpected " + unitStr); 077 } 078 return new FileSize(lenValue * coefficient); 079 } else { 080 throw new IllegalArgumentException("String value [" + fileSizeStr + "] is not in the expected format."); 081 } 082 } 083 084 @Override 085 public String toString() { 086 long inKB = size / KB_COEFFICIENT; 087 088 if (inKB == 0) 089 return size + " Bytes"; 090 091 long inMB = size / MB_COEFFICIENT; 092 if (inMB == 0) { 093 return inKB + " KB"; 094 } 095 096 long inGB = size / GB_COEFFICIENT; 097 if (inGB == 0) { 098 return inMB + " MB"; 099 } 100 101 return inGB + " GB"; 102 103 } 104}