Skip to content

Commit 7379cbb

Browse files
brittergarydgregory
authored andcommitted
[IO-507] Add a ByteOrderParser class.
1 parent fa24ac8 commit 7379cbb

3 files changed

Lines changed: 20 additions & 34 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ The <action> type attribute can be add,update,fix,remove.
114114
Add infinite circular input stream
115115
</action>
116116
<action issue="IO-507" dev="ggregory" type="add">
117-
Add a ByteOrderUtils class.
117+
Add a ByteOrderParser class.
118118
</action>
119119
<action issue="IO-518" dev="jochen" type="add">
120120
Add ObservableInputStream

src/main/java/org/apache/commons/io/ByteOrderUtils.java renamed to src/main/java/org/apache/commons/io/ByteOrderParser.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,26 @@
2525
*
2626
* @since 2.6
2727
*/
28-
public final class ByteOrderUtils {
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";
28+
public final class ByteOrderParser {
4129

4230
/**
4331
* ByteOrderUtils is a static utility class, so prevent construction with a private constructor.
4432
*/
45-
private ByteOrderUtils() {
33+
private ByteOrderParser() {
4634
}
4735

4836
/**
4937
* Parses the String argument as a {@link ByteOrder}, ignoring case.
5038
* <p>
51-
* Returns {@code ByteOrder.LITTLE_ENDIAN} if the given value is {@code "little"} or {@code "LITTLE_ENDIAN"}.
39+
* Returns {@code ByteOrder.LITTLE_ENDIAN} if the given value is {@code "LITTLE_ENDIAN"}.
5240
* </p>
5341
* <p>
54-
* Returns {@code ByteOrder.BIG_ENDIAN} if the given value is {@code "big"} or {@code "BIG_ENDIAN"}.
42+
* Returns {@code ByteOrder.BIG_ENDIAN} if the given value is {@code "BIG_ENDIAN"}.
5543
* </p>
5644
* Examples:
5745
* <ul>
58-
* <li>{@code ByteOrderUtils.parseByteOrder("little")} returns {@code ByteOrder.LITTLE_ENDIAN}</li>
59-
* <li>{@code ByteOrderUtils.parseByteOrder("big")} returns {@code ByteOrder.BIG_ENDIAN}</li>
46+
* <li>{@code ByteOrderParser.parseByteOrder("LITTLE_ENDIAN")} returns {@code ByteOrder.LITTLE_ENDIAN}</li>
47+
* <li>{@code ByteOrderParser.parseByteOrder("BIG_ENDIAN")} returns {@code ByteOrder.BIG_ENDIAN}</li>
6048
* </ul>
6149
*
6250
* @param value
@@ -66,17 +54,14 @@ private ByteOrderUtils() {
6654
* if the {@code String} containing the ByteOrder representation to be parsed is unknown.
6755
*/
6856
public static ByteOrder parseByteOrder(final String value) {
69-
final String valueUp = value.toUpperCase(ComparisonLocale);
70-
final String bigEndianUp = BIG_ENDIAN.toUpperCase(ComparisonLocale);
71-
final String littleEndianUp = LITTLE_ENDIAN.toUpperCase(ComparisonLocale);
72-
if (bigEndianUp.equals(valueUp) || ByteOrder.BIG_ENDIAN.toString().equals(valueUp)) {
57+
if (ByteOrder.BIG_ENDIAN.toString().equals(value)) {
7358
return ByteOrder.BIG_ENDIAN;
7459
}
75-
if (littleEndianUp.equals(valueUp) || ByteOrder.LITTLE_ENDIAN.toString().equals(valueUp)) {
60+
if (ByteOrder.LITTLE_ENDIAN.toString().equals(value)) {
7661
return ByteOrder.LITTLE_ENDIAN;
7762
}
78-
throw new IllegalArgumentException("Unsupported byte order setting: " + value + ", expeced one of " + ByteOrder.LITTLE_ENDIAN + ", " +
79-
LITTLE_ENDIAN + ", " + ByteOrder.BIG_ENDIAN + ", " + bigEndianUp);
63+
throw new IllegalArgumentException("Unsupported byte order setting: " + value + ", expeced one of " + ByteOrder.LITTLE_ENDIAN +
64+
", " + ByteOrder.BIG_ENDIAN);
8065
}
8166

8267
}

src/test/java/org/apache/commons/io/ByteOrderUtilsTest.java renamed to src/test/java/org/apache/commons/io/ByteOrderParserTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,24 @@
2121
import org.junit.Assert;
2222
import org.junit.Test;
2323

24-
public class ByteOrderUtilsTest {
24+
public class ByteOrderParserTest {
2525

2626
private ByteOrder parseByteOrder(final String value) {
27-
return ByteOrderUtils.parseByteOrder(value);
27+
return ByteOrderParser.parseByteOrder(value);
2828
}
2929

3030
@Test
3131
public void testParseBig() {
32-
Assert.assertEquals(ByteOrder.BIG_ENDIAN, parseByteOrder("big"));
33-
Assert.assertEquals(ByteOrder.BIG_ENDIAN, parseByteOrder("Big"));
34-
Assert.assertEquals(ByteOrder.BIG_ENDIAN, parseByteOrder("BIG"));
32+
Assert.assertEquals(ByteOrder.BIG_ENDIAN, parseByteOrder("BIG_ENDIAN"));
3533
}
3634

3735
@Test
3836
public void testParseLittle() {
39-
Assert.assertEquals(ByteOrder.LITTLE_ENDIAN, parseByteOrder("little"));
40-
Assert.assertEquals(ByteOrder.LITTLE_ENDIAN, parseByteOrder("Little"));
41-
Assert.assertEquals(ByteOrder.LITTLE_ENDIAN, parseByteOrder("LITTLE"));
37+
Assert.assertEquals(ByteOrder.LITTLE_ENDIAN, parseByteOrder("LITTLE_ENDIAN"));
38+
}
39+
40+
@Test(expected = IllegalArgumentException.class)
41+
public void testThrowsException() throws Exception {
42+
parseByteOrder("some value");
4243
}
4344
}

0 commit comments

Comments
 (0)