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 */
017package org.apache.commons.compress.utils;
018
019import java.io.IOException;
020
021/**
022 * Utility methods for parsing data and converting it to other formats.
023 *
024 * @since 1.26.0
025 */
026public final class ParsingUtils {
027    /**
028     * Parses the provided string value to an Integer, assuming a base-10 radix
029     *
030     * @param value string value to parse
031     * @return parsed value as an int
032     * @throws IOException when the value cannot be parsed
033     */
034    public static int parseIntValue(final String value) throws IOException {
035        return parseIntValue(value, 10);
036    }
037
038    /**
039     * Parse the provided string value to an Integer with a provided radix
040     *
041     * @param value string value to parse
042     * @param radix radix value to use for parsing
043     * @return parsed value as an int
044     * @throws IOException when the value cannot be parsed
045     */
046    public static int parseIntValue(final String value, final int radix) throws IOException {
047        try {
048            return Integer.parseInt(value, radix);
049        } catch (final NumberFormatException exp) {
050            throw new IOException("Unable to parse int from string value: " + value);
051        }
052    }
053
054    /**
055     * Parses the provided string value to a Long, assuming a base-10 radix
056     *
057     * @param value string value to parse
058     * @return parsed value as a long
059     * @throws IOException when the value cannot be parsed
060     */
061    public static long parseLongValue(final String value) throws IOException {
062        return parseLongValue(value, 10);
063    }
064
065    /**
066     * Parses the provided string value to a Long with a provided radix
067     *
068     * @param value string value to parse
069     * @param radix radix value to use for parsing
070     * @return parsed value as a long
071     * @throws IOException when the value cannot be parsed
072     */
073    public static long parseLongValue(final String value, final int radix) throws IOException {
074        try {
075            return Long.parseLong(value, radix);
076        } catch (final NumberFormatException exp) {
077            throw new IOException("Unable to parse long from string value: " + value);
078        }
079    }
080
081    private ParsingUtils() {
082        /* no instances */ }
083}