View Javadoc
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  
19  import java.io.IOException;
20  
21  /**
22   * Utility methods for parsing data and converting it to other formats.
23   *
24   * @since 1.26.0
25   */
26  public final class ParsingUtils {
27      /**
28       * Parses the provided string value to an Integer, assuming a base-10 radix
29       *
30       * @param value string value to parse
31       * @return parsed value as an int
32       * @throws IOException when the value cannot be parsed
33       */
34      public static int parseIntValue(final String value) throws IOException {
35          return parseIntValue(value, 10);
36      }
37  
38      /**
39       * Parse the provided string value to an Integer with a provided radix
40       *
41       * @param value string value to parse
42       * @param radix radix value to use for parsing
43       * @return parsed value as an int
44       * @throws IOException when the value cannot be parsed
45       */
46      public static int parseIntValue(final String value, final int radix) throws IOException {
47          try {
48              return Integer.parseInt(value, radix);
49          } catch (final NumberFormatException exp) {
50              throw new IOException("Unable to parse int from string value: " + value);
51          }
52      }
53  
54      /**
55       * Parses the provided string value to a Long, assuming a base-10 radix
56       *
57       * @param value string value to parse
58       * @return parsed value as a long
59       * @throws IOException when the value cannot be parsed
60       */
61      public static long parseLongValue(final String value) throws IOException {
62          return parseLongValue(value, 10);
63      }
64  
65      /**
66       * Parses the provided string value to a Long with a provided radix
67       *
68       * @param value string value to parse
69       * @param radix radix value to use for parsing
70       * @return parsed value as a long
71       * @throws IOException when the value cannot be parsed
72       */
73      public static long parseLongValue(final String value, final int radix) throws IOException {
74          try {
75              return Long.parseLong(value, radix);
76          } catch (final NumberFormatException exp) {
77              throw new IOException("Unable to parse long from string value: " + value);
78          }
79      }
80  
81      private ParsingUtils() {
82          /* no instances */ }
83  }