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 * https://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
18 package org.apache.commons.io;
19
20 import java.nio.ByteOrder;
21
22 /**
23 * Converts Strings to {@link ByteOrder} instances.
24 *
25 * @since 2.6
26 */
27 public final class ByteOrderParser {
28
29 /**
30 * Parses the String argument as a {@link ByteOrder}.
31 * <p>
32 * Returns {@code ByteOrder.LITTLE_ENDIAN} if the given value is {@code "LITTLE_ENDIAN"}.
33 * </p>
34 * <p>
35 * Returns {@code ByteOrder.BIG_ENDIAN} if the given value is {@code "BIG_ENDIAN"}.
36 * </p>
37 * Examples:
38 * <ul>
39 * <li>{@code ByteOrderParser.parseByteOrder("LITTLE_ENDIAN")} returns {@code ByteOrder.LITTLE_ENDIAN}</li>
40 * <li>{@code ByteOrderParser.parseByteOrder("BIG_ENDIAN")} returns {@code ByteOrder.BIG_ENDIAN}</li>
41 * </ul>
42 *
43 * @param value
44 * the {@link String} containing the ByteOrder representation to be parsed
45 * @return the ByteOrder represented by the string argument
46 * @throws IllegalArgumentException
47 * if the {@link String} containing the ByteOrder representation to be parsed is unknown.
48 */
49 public static ByteOrder parseByteOrder(final String value) {
50 if (ByteOrder.BIG_ENDIAN.toString().equals(value)) {
51 return ByteOrder.BIG_ENDIAN;
52 }
53 if (ByteOrder.LITTLE_ENDIAN.toString().equals(value)) {
54 return ByteOrder.LITTLE_ENDIAN;
55 }
56 throw new IllegalArgumentException("Unsupported byte order setting: " + value + ", expected one of " + ByteOrder.LITTLE_ENDIAN +
57 ", " + ByteOrder.BIG_ENDIAN);
58 }
59
60 /**
61 * ByteOrderUtils is a static utility class, so prevent construction with a private constructor.
62 */
63 private ByteOrderParser() {
64 }
65
66 }