    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 *      https://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 */
    017package org.apache.commons.io;
    018
    019import java.nio.charset.Charset;
    020import java.nio.charset.StandardCharsets;
    021import java.nio.charset.UnsupportedCharsetException;
    022import java.util.Collections;
    023import java.util.SortedMap;
    024import java.util.TreeMap;
    025
    026/**
    027 * Charsets required of every implementation of the Java platform.
    028 *
    029 * From the Java documentation <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">
    030 * Standard charsets</a>:
    031 * <p>
    032 * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult
    033 * the release documentation for your implementation to see if any other encodings are supported. Consult the release
    034 * documentation for your implementation to see if any other encodings are supported. </cite>
    035 * </p>
    036 *
    037 * <ul>
    038 * <li>{@code US-ASCII}<br>
    039 * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li>
    040 * <li>{@code ISO-8859-1}<br>
    041 * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li>
    042 * <li>{@code UTF-8}<br>
    043 * Eight-bit Unicode Transformation Format.</li>
    044 * <li>{@code UTF-16BE}<br>
    045 * Sixteen-bit Unicode Transformation Format, big-endian byte order.</li>
    046 * <li>{@code UTF-16LE}<br>
    047 * Sixteen-bit Unicode Transformation Format, little-endian byte order.</li>
    048 * <li>{@code UTF-16}<br>
    049 * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
    050 * accepted on input, big-endian used on output.)</li>
    051 * </ul>
    052 *
    053 * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    054 * @since 2.3
    055 */
    056public class Charsets {
    057
    058    //
    059    // This class should only contain Charset instances for required encodings. This guarantees that it will load
    060    // correctly and without delay on all Java platforms.
    061    //
    062
    063    private static final SortedMap<String, Charset> STANDARD_CHARSET_MAP;
    064
    065    static {
    066        final SortedMap<String, Charset> standardCharsetMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    067        standardCharsetMap.put(StandardCharsets.ISO_8859_1.name(), StandardCharsets.ISO_8859_1);
    068        standardCharsetMap.put(StandardCharsets.US_ASCII.name(), StandardCharsets.US_ASCII);
    069        standardCharsetMap.put(StandardCharsets.UTF_16.name(), StandardCharsets.UTF_16);
    070        standardCharsetMap.put(StandardCharsets.UTF_16BE.name(), StandardCharsets.UTF_16BE);
    071        standardCharsetMap.put(StandardCharsets.UTF_16LE.name(), StandardCharsets.UTF_16LE);
    072        standardCharsetMap.put(StandardCharsets.UTF_8.name(), StandardCharsets.UTF_8);
    073        STANDARD_CHARSET_MAP = Collections.unmodifiableSortedMap(standardCharsetMap);
    074    }
    075
    076    /**
    077     * CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
    078     * <p>
    079     * Every implementation of the Java platform is required to support this character encoding.
    080     * </p>
    081     *
    082     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    083     * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
    084     */
    085    @Deprecated
    086    public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;
    087
    088    /**
    089     * <p>
    090     * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
    091     * </p>
    092     * <p>
    093     * Every implementation of the Java platform is required to support this character encoding.
    094     * </p>
    095     *
    096     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    097     * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
    098     */
    099    @Deprecated
    100    public static final Charset US_ASCII = StandardCharsets.US_ASCII;
    101
    102    /**
    103     * <p>
    104     * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
    105     * (either order accepted on input, big-endian used on output)
    106     * </p>
    107     * <p>
    108     * Every implementation of the Java platform is required to support this character encoding.
    109     * </p>
    110     *
    111     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    112     * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
    113     */
    114    @Deprecated
    115    public static final Charset UTF_16 = StandardCharsets.UTF_16;
    116
    117    /**
    118     * <p>
    119     * Sixteen-bit Unicode Transformation Format, big-endian byte order.
    120     * </p>
    121     * <p>
    122     * Every implementation of the Java platform is required to support this character encoding.
    123     * </p>
    124     *
    125     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    126     * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
    127     */
    128    @Deprecated
    129    public static final Charset UTF_16BE = StandardCharsets.UTF_16BE;
    130
    131    /**
    132     * <p>
    133     * Sixteen-bit Unicode Transformation Format, little-endian byte order.
    134     * </p>
    135     * <p>
    136     * Every implementation of the Java platform is required to support this character encoding.
    137     * </p>
    138     *
    139     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    140     * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
    141     */
    142    @Deprecated
    143    public static final Charset UTF_16LE = StandardCharsets.UTF_16LE;
    144
    145    /**
    146     * <p>
    147     * Eight-bit Unicode Transformation Format.
    148     * </p>
    149     * <p>
    150     * Every implementation of the Java platform is required to support this character encoding.
    151     * </p>
    152     *
    153     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
    154     * @deprecated Use Java 7's {@link java.nio.charset.StandardCharsets}
    155     */
    156    @Deprecated
    157    public static final Charset UTF_8 = StandardCharsets.UTF_8;
    158
    159    /**
    160     * Tests whether the given non-null Charset has an alias of the given name.
    161     *
    162     * @param charset a non-null Charset.
    163     * @param charsetName The name to test.
    164     * @return whether the given non-null charset name is a UTF-8 alias.
    165     * @since 2.20.0
    166     */
    167    public static boolean isAlias(final Charset charset, final String charsetName) {
    168        return charsetName != null && (charset.name().equalsIgnoreCase(charsetName) || charset.aliases().stream().anyMatch(charsetName::equalsIgnoreCase));
    169    }
    170
    171    /**
    172     * Tests whether a given encoding is UTF-8. If the given charset is null, then check the platform's default encoding.
    173     *
    174     * @param charset If the given charset is null, then check the platform's default encoding.
    175     * @return whether a given encoding is UTF-8.
    176     * @since 2.20.0
    177     */
    178    public static boolean isUTF8(final Charset charset) {
    179        return isUTF8Alias(toCharset(charset).name());
    180    }
    181
    182    /**
    183     * Tests whether the given non-null charset name is a UTF-8 alias.
    184     *
    185     * @param charsetName a non-null charset name.
    186     * @return whether the given non-null charset name is a UTF-8 alias.
    187     */
    188    private static boolean isUTF8Alias(final String charsetName) {
    189        return isAlias(StandardCharsets.UTF_8, charsetName);
    190    }
    191
    192    /**
    193     * Constructs a sorted map from canonical charset names to charset objects required of every implementation of the
    194     * Java platform.
    195     * <p>
    196     * From the Java documentation <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">
    197     * Standard charsets</a>:
    198     * </p>
    199     *
    200     * @return An immutable, case-insensitive map from canonical charset names to charset objects.
    201     * @see Charset#availableCharsets()
    202     * @since 2.5
    203     */
    204    public static SortedMap<String, Charset> requiredCharsets() {
    205        return STANDARD_CHARSET_MAP;
    206    }
    207
    208    /**
    209     * Returns the given Charset or the {@link Charset#defaultCharset() default Charset} if the given Charset is null.
    210     *
    211     * @param charset
    212     *            A charset or null.
    213     * @return the given Charset or the default Charset if the given Charset is null
    214     * @see Charset#defaultCharset()
    215     */
    216    public static Charset toCharset(final Charset charset) {
    217        return charset == null ? Charset.defaultCharset() : charset;
    218    }
    219
    220    /**
    221     * Returns the given charset if non-null, otherwise return defaultCharset.
    222     *
    223     * @param charset The charset to test, may be null.
    224     * @param defaultCharset The charset to return if charset is null, may be null.
    225     * @return a Charset.
    226     * @since 2.12.0
    227     */
    228    public static Charset toCharset(final Charset charset, final Charset defaultCharset) {
    229        return charset == null ? defaultCharset : charset;
    230    }
    231
    232    /**
    233     * Returns a Charset for the named charset. If the name is null, return the {@link Charset#defaultCharset() default Charset}.
    234     *
    235     * @param charsetName The name of the requested charset, may be null.
    236     * @return a Charset for the named charset.
    237     * @throws UnsupportedCharsetException If the named charset is unavailable (unchecked exception).
    238     * @see Charset#defaultCharset()
    239     */
    240    public static Charset toCharset(final String charsetName) throws UnsupportedCharsetException {
    241        return toCharset(charsetName, Charset.defaultCharset());
    242    }
    243
    244    /**
    245     * Returns a Charset for the named charset. If the name is null, return the given default Charset.
    246     *
    247     * @param charsetName The name of the requested charset, may be null.
    248     * @param defaultCharset The charset to return if charsetName is null, may be null.
    249     * @return a Charset for the named charset.
    250     * @throws UnsupportedCharsetException If the named charset is unavailable (unchecked exception).
    251     * @since 2.12.0
    252     */
    253    public static Charset toCharset(final String charsetName, final Charset defaultCharset) throws UnsupportedCharsetException {
    254        return charsetName == null ? defaultCharset : Charset.forName(charsetName);
    255    }
    256
    257    /**
    258     * Returns a Charset for the named charset or the {@code defaultCharset}.
    259     * <p>
    260     * If {@code charsetName} cannot load a charset, return {@code defaultCharset}. Therefore, this method should never fail and always return a Charset.
    261     * </p>
    262     *
    263     * @param charsetName    The name of the requested charset, may be null.
    264     * @param defaultCharset The charset to return if charsetName is null or there is a problem, may be null which returns {@link Charset#defaultCharset()}.
    265     * @return a Charset for the named charset or {@code defaultCharset} if any errors occur.
    266     * @see Charset#defaultCharset()
    267     * @since 2.20.0
    268     */
    269    public static Charset toCharsetDefault(final String charsetName, final Charset defaultCharset) {
    270        try {
    271            return toCharset(charsetName);
    272        } catch (final RuntimeException ignored) {
    273            return toCharset(defaultCharset);
    274        }
    275    }
    276
    277    /**
    278     * Construct a new instance.
    279     *
    280     * @deprecated Will be private in 4.0
    281     */
    282    @Deprecated
    283    public Charsets() {
    284        // empty
    285    }
    286}