View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.utils;
20  
21  import java.io.IOException;
22  
23  /**
24   * Utility methods for parsing data and converting it to other formats.
25   *
26   * @since 1.26.0
27   */
28  public final class ParsingUtils {
29      /**
30       * Parses the provided string value to an Integer, assuming a base-10 radix
31       *
32       * @param value string value to parse
33       * @return parsed value as an int
34       * @throws IOException when the value cannot be parsed
35       */
36      public static int parseIntValue(final String value) throws IOException {
37          return parseIntValue(value, 10);
38      }
39  
40      /**
41       * Parse the provided string value to an Integer with a provided radix
42       *
43       * @param value string value to parse
44       * @param radix radix value to use for parsing
45       * @return parsed value as an int
46       * @throws IOException when the value cannot be parsed
47       */
48      public static int parseIntValue(final String value, final int radix) throws IOException {
49          try {
50              return Integer.parseInt(value, radix);
51          } catch (final NumberFormatException exp) {
52              throw new IOException("Unable to parse int from string value: " + value);
53          }
54      }
55  
56      /**
57       * Parses the provided string value to a Long, assuming a base-10 radix
58       *
59       * @param value string value to parse
60       * @return parsed value as a long
61       * @throws IOException when the value cannot be parsed
62       */
63      public static long parseLongValue(final String value) throws IOException {
64          return parseLongValue(value, 10);
65      }
66  
67      /**
68       * Parses the provided string value to a Long with a provided radix
69       *
70       * @param value string value to parse
71       * @param radix radix value to use for parsing
72       * @return parsed value as a long
73       * @throws IOException when the value cannot be parsed
74       */
75      public static long parseLongValue(final String value, final int radix) throws IOException {
76          try {
77              return Long.parseLong(value, radix);
78          } catch (final NumberFormatException exp) {
79              throw new IOException("Unable to parse long from string value: " + value);
80          }
81      }
82  
83      private ParsingUtils() {
84          /* no instances */ }
85  }