ParsingUtils.java

  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. package org.apache.commons.compress.utils;

  18. import java.io.IOException;

  19. /**
  20.  * Utility methods for parsing data and converting it to other formats.
  21.  *
  22.  * @since 1.26.0
  23.  */
  24. public final class ParsingUtils {
  25.     /**
  26.      * Parses the provided string value to an Integer, assuming a base-10 radix
  27.      *
  28.      * @param value string value to parse
  29.      * @return parsed value as an int
  30.      * @throws IOException when the value cannot be parsed
  31.      */
  32.     public static int parseIntValue(final String value) throws IOException {
  33.         return parseIntValue(value, 10);
  34.     }

  35.     /**
  36.      * Parse the provided string value to an Integer with a provided radix
  37.      *
  38.      * @param value string value to parse
  39.      * @param radix radix value to use for parsing
  40.      * @return parsed value as an int
  41.      * @throws IOException when the value cannot be parsed
  42.      */
  43.     public static int parseIntValue(final String value, final int radix) throws IOException {
  44.         try {
  45.             return Integer.parseInt(value, radix);
  46.         } catch (final NumberFormatException exp) {
  47.             throw new IOException("Unable to parse int from string value: " + value);
  48.         }
  49.     }

  50.     /**
  51.      * Parses the provided string value to a Long, assuming a base-10 radix
  52.      *
  53.      * @param value string value to parse
  54.      * @return parsed value as a long
  55.      * @throws IOException when the value cannot be parsed
  56.      */
  57.     public static long parseLongValue(final String value) throws IOException {
  58.         return parseLongValue(value, 10);
  59.     }

  60.     /**
  61.      * Parses the provided string value to a Long with a provided radix
  62.      *
  63.      * @param value string value to parse
  64.      * @param radix radix value to use for parsing
  65.      * @return parsed value as a long
  66.      * @throws IOException when the value cannot be parsed
  67.      */
  68.     public static long parseLongValue(final String value, final int radix) throws IOException {
  69.         try {
  70.             return Long.parseLong(value, radix);
  71.         } catch (final NumberFormatException exp) {
  72.             throw new IOException("Unable to parse long from string value: " + value);
  73.         }
  74.     }

  75.     private ParsingUtils() {
  76.         /* no instances */ }
  77. }