1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17
18 package org.apache.logging.log4j.core.appender.rolling;
19
20 import java.text.NumberFormat;
21 import java.text.ParseException;
22 import java.util.Locale;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import org.apache.logging.log4j.Logger;
27 import org.apache.logging.log4j.status.StatusLogger;
28
29 /**
30 * FileSize utility class.
31 */
32 public final class FileSize {
33 private static final Logger LOGGER = StatusLogger.getLogger();
34
35 private static final long KB = 1024;
36 private static final long MB = KB * KB;
37 private static final long GB = KB * MB;
38
39 /**
40 * Pattern for string parsing.
41 */
42 private static final Pattern VALUE_PATTERN =
43 Pattern.compile("([0-9]+([\\.,][0-9]+)?)\\s*(|K|M|G)B?", Pattern.CASE_INSENSITIVE);
44
45 private FileSize() {
46 }
47
48 /**
49 * Converts a string to a number of bytes. Strings consist of a floating point value followed by
50 * K, M, or G for kilobytes, megabytes, gigabytes, respectively. The
51 * abbreviations KB, MB, and GB are also accepted. Matching is case insensitive.
52 *
53 * @param string The string to convert
54 * @param defaultValue The default value if a problem is detected parsing.
55 * @return The Bytes value for the string
56 */
57 public static long parse(final String string, final long defaultValue) {
58 final Matcher matcher = VALUE_PATTERN.matcher(string);
59
60 // Valid input?
61 if (matcher.matches()) {
62 try {
63 // Get double precision value
64 final long value = NumberFormat.getNumberInstance(Locale.getDefault()).parse(
65 matcher.group(1)).longValue();
66
67 // Get units specified
68 final String units = matcher.group(3);
69
70 if (units.isEmpty()) {
71 return value;
72 } else if (units.equalsIgnoreCase("K")) {
73 return value * KB;
74 } else if (units.equalsIgnoreCase("M")) {
75 return value * MB;
76 } else if (units.equalsIgnoreCase("G")) {
77 return value * GB;
78 } else {
79 LOGGER.error("FileSize units not recognized: " + string);
80 return defaultValue;
81 }
82 } catch (final ParseException e) {
83 LOGGER.error("FileSize unable to parse numeric part: " + string, e);
84 return defaultValue;
85 }
86 }
87 LOGGER.error("FileSize unable to parse bytes: " + string);
88 return defaultValue;
89 }
90
91 }