|
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 | | - |
18 | | -package org.apache.commons.io; |
19 | | - |
20 | | -import java.nio.ByteOrder; |
21 | | -import java.util.Locale; |
22 | | - |
23 | | -/** |
24 | | - * Converts Strings to {@link ByteOrder} instances. |
25 | | - * |
26 | | - * @since 2.6 |
27 | | - */ |
28 | | -public class ByteOrderFactory { |
29 | | - |
30 | | - private static final Locale ComparisonLocale = Locale.ROOT; |
31 | | - |
32 | | - /** |
33 | | - * Big endian. |
34 | | - */ |
35 | | - public static final String BIG_ENDIAN = "Big"; |
36 | | - |
37 | | - /** |
38 | | - * Little endian. |
39 | | - */ |
40 | | - public static final String LITTLE_ENDIAN = "Little"; |
41 | | - |
42 | | - /** |
43 | | - * Parses the String argument as a {@link ByteOrder}, ignoring case. |
44 | | - * <p> |
45 | | - * Returns {@code ByteOrder.LITTLE_ENDIAN} if the given value is {@code "little"} or {@code "LITTLE_ENDIAN"}. |
46 | | - * </p> |
47 | | - * <p> |
48 | | - * Returns {@code ByteOrder.BIG_ENDIAN} if the given value is {@code "big"} or {@code "BIG_ENDIAN"}. |
49 | | - * </p> |
50 | | - * Examples: |
51 | | - * <ul> |
52 | | - * <li>{@code ByteOrderFactory.parseByteOrder("little")} returns {@code ByteOrder.LITTLE_ENDIAN}</li> |
53 | | - * <li>{@code ByteOrderFactory.parseByteOrder("big")} returns {@code ByteOrder.BIG_ENDIAN}</li> |
54 | | - * </ul> |
55 | | - * |
56 | | - * @param value |
57 | | - * the {@code String} containing the ByteOrder representation to be parsed |
58 | | - * @return the ByteOrder represented by the string argument |
59 | | - * @throws IllegalArgumentException |
60 | | - * if the {@code String} containing the ByteOrder representation to be parsed is unknown. |
61 | | - */ |
62 | | - public static ByteOrder parseByteOrder(final String value) { |
63 | | - final String valueUp = value.toUpperCase(ComparisonLocale); |
64 | | - final String bigEndianUp = BIG_ENDIAN.toUpperCase(ComparisonLocale); |
65 | | - final String littleEndianUp = LITTLE_ENDIAN.toUpperCase(ComparisonLocale); |
66 | | - if (bigEndianUp.equals(valueUp) || ByteOrder.BIG_ENDIAN.toString().equals(valueUp)) { |
67 | | - return ByteOrder.BIG_ENDIAN; |
68 | | - } |
69 | | - if (littleEndianUp.equals(valueUp) || ByteOrder.LITTLE_ENDIAN.toString().equals(valueUp)) { |
70 | | - return ByteOrder.LITTLE_ENDIAN; |
71 | | - } |
72 | | - throw new IllegalArgumentException("Unsupported byte order setting: " + value + ", expeced one of " + ByteOrder.LITTLE_ENDIAN + ", " + |
73 | | - LITTLE_ENDIAN + ", " + ByteOrder.BIG_ENDIAN + ", " + bigEndianUp); |
74 | | - } |
75 | | - |
76 | | -} |
| 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 | + |
| 18 | +package org.apache.commons.io; |
| 19 | + |
| 20 | +import java.nio.ByteOrder; |
| 21 | +import java.util.Locale; |
| 22 | + |
| 23 | +/** |
| 24 | + * Converts Strings to {@link ByteOrder} instances. |
| 25 | + * |
| 26 | + * @since 2.6 |
| 27 | + */ |
| 28 | +public class ByteOrderFactory { |
| 29 | + |
| 30 | + private static final Locale ComparisonLocale = Locale.ROOT; |
| 31 | + |
| 32 | + /** |
| 33 | + * Big endian. |
| 34 | + */ |
| 35 | + public static final String BIG_ENDIAN = "Big"; |
| 36 | + |
| 37 | + /** |
| 38 | + * Little endian. |
| 39 | + */ |
| 40 | + public static final String LITTLE_ENDIAN = "Little"; |
| 41 | + |
| 42 | + /** |
| 43 | + * Parses the String argument as a {@link ByteOrder}, ignoring case. |
| 44 | + * <p> |
| 45 | + * Returns {@code ByteOrder.LITTLE_ENDIAN} if the given value is {@code "little"} or {@code "LITTLE_ENDIAN"}. |
| 46 | + * </p> |
| 47 | + * <p> |
| 48 | + * Returns {@code ByteOrder.BIG_ENDIAN} if the given value is {@code "big"} or {@code "BIG_ENDIAN"}. |
| 49 | + * </p> |
| 50 | + * Examples: |
| 51 | + * <ul> |
| 52 | + * <li>{@code ByteOrderFactory.parseByteOrder("little")} returns {@code ByteOrder.LITTLE_ENDIAN}</li> |
| 53 | + * <li>{@code ByteOrderFactory.parseByteOrder("big")} returns {@code ByteOrder.BIG_ENDIAN}</li> |
| 54 | + * </ul> |
| 55 | + * |
| 56 | + * @param value |
| 57 | + * the {@code String} containing the ByteOrder representation to be parsed |
| 58 | + * @return the ByteOrder represented by the string argument |
| 59 | + * @throws IllegalArgumentException |
| 60 | + * if the {@code String} containing the ByteOrder representation to be parsed is unknown. |
| 61 | + */ |
| 62 | + public static ByteOrder parseByteOrder(final String value) { |
| 63 | + final String valueUp = value.toUpperCase(ComparisonLocale); |
| 64 | + final String bigEndianUp = BIG_ENDIAN.toUpperCase(ComparisonLocale); |
| 65 | + final String littleEndianUp = LITTLE_ENDIAN.toUpperCase(ComparisonLocale); |
| 66 | + if (bigEndianUp.equals(valueUp) || ByteOrder.BIG_ENDIAN.toString().equals(valueUp)) { |
| 67 | + return ByteOrder.BIG_ENDIAN; |
| 68 | + } |
| 69 | + if (littleEndianUp.equals(valueUp) || ByteOrder.LITTLE_ENDIAN.toString().equals(valueUp)) { |
| 70 | + return ByteOrder.LITTLE_ENDIAN; |
| 71 | + } |
| 72 | + throw new IllegalArgumentException("Unsupported byte order setting: " + value + ", expeced one of " + ByteOrder.LITTLE_ENDIAN + ", " + |
| 73 | + LITTLE_ENDIAN + ", " + ByteOrder.BIG_ENDIAN + ", " + bigEndianUp); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments