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     * @return The Bytes value for the string
055     */
056    public static long parse(final String string, long defaultValue) {
057        final Matcher matcher = VALUE_PATTERN.matcher(string);
058
059        // Valid input?
060        if (matcher.matches()) {
061            try {
062                // Get double precision value
063                final long value = NumberFormat.getNumberInstance(Locale.getDefault()).parse(
064                    matcher.group(1)).longValue();
065
066                // Get units specified
067                final String units = matcher.group(3);
068
069                if (units.isEmpty()) {
070                    return value;
071                } else if (units.equalsIgnoreCase("K")) {
072                    return value * KB;
073                } else if (units.equalsIgnoreCase("M")) {
074                    return value * MB;
075                } else if (units.equalsIgnoreCase("G")) {
076                    return value * GB;
077                } else {
078                    LOGGER.error("FileSize units not recognized: " + string);
079                    return defaultValue;
080                }
081            } catch (final ParseException e) {
082                LOGGER.error("FileSize unable to parse numeric part: " + string, e);
083                return defaultValue;
084            }
085        }
086        LOGGER.error("FileSize unable to parse bytes: " + string);
087        return defaultValue;
088    }
089
090}