001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.utils;
020
021import java.io.IOException;
022
023/**
024 * Utility methods for parsing data and converting it to other formats.
025 *
026 * @since 1.26.0
027 */
028public final class ParsingUtils {
029    /**
030     * Parses the provided string value to an Integer, assuming a base-10 radix
031     *
032     * @param value string value to parse
033     * @return parsed value as an int
034     * @throws IOException when the value cannot be parsed
035     */
036    public static int parseIntValue(final String value) throws IOException {
037        return parseIntValue(value, 10);
038    }
039
040    /**
041     * Parse the provided string value to an Integer with a provided radix
042     *
043     * @param value string value to parse
044     * @param radix radix value to use for parsing
045     * @return parsed value as an int
046     * @throws IOException when the value cannot be parsed
047     */
048    public static int parseIntValue(final String value, final int radix) throws IOException {
049        try {
050            return Integer.parseInt(value, radix);
051        } catch (final NumberFormatException exp) {
052            throw new IOException("Unable to parse int from string value: " + value);
053        }
054    }
055
056    /**
057     * Parses the provided string value to a Long, assuming a base-10 radix
058     *
059     * @param value string value to parse
060     * @return parsed value as a long
061     * @throws IOException when the value cannot be parsed
062     */
063    public static long parseLongValue(final String value) throws IOException {
064        return parseLongValue(value, 10);
065    }
066
067    /**
068     * Parses the provided string value to a Long with a provided radix
069     *
070     * @param value string value to parse
071     * @param radix radix value to use for parsing
072     * @return parsed value as a long
073     * @throws IOException when the value cannot be parsed
074     */
075    public static long parseLongValue(final String value, final int radix) throws IOException {
076        try {
077            return Long.parseLong(value, radix);
078        } catch (final NumberFormatException exp) {
079            throw new IOException("Unable to parse long from string value: " + value);
080        }
081    }
082
083    private ParsingUtils() {
084        /* no instances */ }
085}