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 org.apache.logging.log4j.Logger;
021import org.apache.logging.log4j.status.StatusLogger;
022
023import java.text.NumberFormat;
024import java.text.ParseException;
025import java.util.Locale;
026import java.util.regex.Matcher;
027import java.util.regex.Pattern;
028
029/**
030 * FileSize utility class.
031 */
032public final class FileSize {
033
034    private static final Logger LOGGER = StatusLogger.getLogger();
035
036    private static final long KB = 1024;
037    private static final long MB = KB * KB;
038    private static final long GB = KB * MB;
039    private static final long TB = KB * GB;
040
041    /**
042     * Pattern for string parsing.
043     */
044    private static final Pattern VALUE_PATTERN =
045            Pattern.compile("([0-9]+([.,][0-9]+)?)\\s*(|K|M|G|T)B?", Pattern.CASE_INSENSITIVE);
046
047    private FileSize() {}
048
049    /**
050     * Converts a string to a number of bytes. Strings consist of a floating point value followed by
051     * K, M, or G for kilobytes, megabytes, gigabytes, respectively. The
052     * abbreviations KB, MB, and GB are also accepted. Matching is case insensitive.
053     *
054     * @param string The string to convert
055     * @param defaultValue The default value if a problem is detected parsing.
056     * @return The Bytes value for the string
057     */
058    public static long parse(final String string, final long defaultValue) {
059        final Matcher matcher = VALUE_PATTERN.matcher(string);
060
061        // Valid input?
062        if (matcher.matches()) {
063            try {
064
065                // Read the quantity.
066                final String quantityString = matcher.group(1);
067                final double quantity = NumberFormat
068                        .getNumberInstance(Locale.ROOT)
069                        .parse(quantityString)
070                        .doubleValue();
071
072                // Read the unit.
073                final String unit = matcher.group(3);
074
075                // Calculate the number of bytes.
076                if (unit == null || unit.isEmpty()) {
077                    return (long) quantity;
078                } else if (unit.equalsIgnoreCase("K")) {
079                    return (long) (quantity * KB);
080                } else if (unit.equalsIgnoreCase("M")) {
081                    return (long) (quantity * MB);
082                } else if (unit.equalsIgnoreCase("G")) {
083                    return (long) (quantity * GB);
084                } else if (unit.equalsIgnoreCase("T")) {
085                    return (long) (quantity * TB);
086                } else {
087                    LOGGER.error("FileSize units not recognized: " + string);
088                    return defaultValue;
089                }
090
091            } catch (final ParseException error) {
092                LOGGER.error("FileSize unable to parse numeric part: " + string, error);
093                return defaultValue;
094            }
095        }
096
097        // Invalid input, bail out.
098        LOGGER.error("FileSize unable to parse bytes: " + string);
099        return defaultValue;
100
101    }
102
103}