001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 018package org.apache.logging.log4j.core.appender.rolling; 019 020import java.text.NumberFormat; 021import java.text.ParseException; 022import java.util.Locale; 023import java.util.regex.Matcher; 024import java.util.regex.Pattern; 025 026import org.apache.logging.log4j.Logger; 027import org.apache.logging.log4j.status.StatusLogger; 028 029/** 030 * FileSize utility class. 031 */ 032public final class FileSize { 033 private static final Logger LOGGER = StatusLogger.getLogger(); 034 035 private static final long KB = 1024; 036 private static final long MB = KB * KB; 037 private static final long GB = KB * MB; 038 039 /** 040 * Pattern for string parsing. 041 */ 042 private static final Pattern VALUE_PATTERN = 043 Pattern.compile("([0-9]+([\\.,][0-9]+)?)\\s*(|K|M|G)B?", Pattern.CASE_INSENSITIVE); 044 045 private FileSize() { 046 } 047 048 /** 049 * Converts a string to a number of bytes. Strings consist of a floating point value followed by 050 * K, M, or G for kilobytes, megabytes, gigabytes, respectively. The 051 * abbreviations KB, MB, and GB are also accepted. Matching is case insensitive. 052 * 053 * @param string The string to convert 054 * @param defaultValue The default value if a problem is detected parsing. 055 * @return The Bytes value for the string 056 */ 057 public static long parse(final String string, final long defaultValue) { 058 final Matcher matcher = VALUE_PATTERN.matcher(string); 059 060 // Valid input? 061 if (matcher.matches()) { 062 try { 063 // Get double precision value 064 final long value = NumberFormat.getNumberInstance(Locale.getDefault()).parse( 065 matcher.group(1)).longValue(); 066 067 // Get units specified 068 final String units = matcher.group(3); 069 070 if (units.isEmpty()) { 071 return value; 072 } else if (units.equalsIgnoreCase("K")) { 073 return value * KB; 074 } else if (units.equalsIgnoreCase("M")) { 075 return value * MB; 076 } else if (units.equalsIgnoreCase("G")) { 077 return value * GB; 078 } else { 079 LOGGER.error("FileSize units not recognized: " + string); 080 return defaultValue; 081 } 082 } catch (final ParseException e) { 083 LOGGER.error("FileSize unable to parse numeric part: " + string, e); 084 return defaultValue; 085 } 086 } 087 LOGGER.error("FileSize unable to parse bytes: " + string); 088 return defaultValue; 089 } 090 091}