001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.io; 019 020import java.nio.ByteOrder; 021 022/** 023 * Converts Strings to {@link ByteOrder} instances. 024 * 025 * @since 2.6 026 */ 027public final class ByteOrderParser { 028 029 /** 030 * Parses the String argument as a {@link ByteOrder}. 031 * <p> 032 * Returns {@code ByteOrder.LITTLE_ENDIAN} if the given value is {@code "LITTLE_ENDIAN"}. 033 * </p> 034 * <p> 035 * Returns {@code ByteOrder.BIG_ENDIAN} if the given value is {@code "BIG_ENDIAN"}. 036 * </p> 037 * Examples: 038 * <ul> 039 * <li>{@code ByteOrderParser.parseByteOrder("LITTLE_ENDIAN")} returns {@code ByteOrder.LITTLE_ENDIAN}</li> 040 * <li>{@code ByteOrderParser.parseByteOrder("BIG_ENDIAN")} returns {@code ByteOrder.BIG_ENDIAN}</li> 041 * </ul> 042 * 043 * @param value 044 * the {@link String} containing the ByteOrder representation to be parsed 045 * @return the ByteOrder represented by the string argument 046 * @throws IllegalArgumentException 047 * if the {@link String} containing the ByteOrder representation to be parsed is unknown. 048 */ 049 public static ByteOrder parseByteOrder(final String value) { 050 if (ByteOrder.BIG_ENDIAN.toString().equals(value)) { 051 return ByteOrder.BIG_ENDIAN; 052 } 053 if (ByteOrder.LITTLE_ENDIAN.toString().equals(value)) { 054 return ByteOrder.LITTLE_ENDIAN; 055 } 056 throw new IllegalArgumentException("Unsupported byte order setting: " + value + ", expected one of " + ByteOrder.LITTLE_ENDIAN + 057 ", " + ByteOrder.BIG_ENDIAN); 058 } 059 060 /** 061 * ByteOrderUtils is a static utility class, so prevent construction with a private constructor. 062 */ 063 private ByteOrderParser() { 064 } 065 066}