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 */
017
018package org.apache.commons.lang3;
019
020import java.nio.charset.Charset;
021import java.nio.charset.IllegalCharsetNameException;
022import java.nio.charset.StandardCharsets;
023
024/**
025 * Character encoding names required of every implementation of the Java platform.
026 *
027 * <p>
028 * According to <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html">JRE character
029 * encoding names</a>:
030 * </p>
031 *
032 * <p>
033 * <cite>Every implementation of the Java platform is required to support the following character encodings.
034 * Consult the release documentation for your implementation to see if any other encodings are supported.
035 * </cite>
036 * </p>
037 *
038 * @see <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html">JRE character encoding names</a>
039 * @since 2.1
040 * @deprecated Java 7 introduced {@link java.nio.charset.StandardCharsets}, which defines these constants as
041 * {@link Charset} objects. Use {@link Charset#name()} to get the string values provided in this class.
042 * This class will be removed in a future release.
043 */
044@Deprecated
045public class CharEncoding {
046
047    /**
048     * ISO Latin Alphabet #1, also known as ISO-LATIN-1.
049     *
050     * <p>
051     * Every implementation of the Java platform is required to support this character encoding.
052     * </p>
053     */
054    public static final String ISO_8859_1 = StandardCharsets.ISO_8859_1.name();
055
056    /**
057     * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block
058     * of the Unicode character set.
059     *
060     * <p>
061     * Every implementation of the Java platform is required to support this character encoding.
062     * </p>
063     */
064    public static final String US_ASCII = StandardCharsets.US_ASCII.name();
065
066    /**
067     * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial
068     * byte-order mark (either order accepted on input, big-endian used on output).
069     *
070     * <p>
071     * Every implementation of the Java platform is required to support this character encoding.
072     * </p>
073     */
074    public static final String UTF_16 = StandardCharsets.UTF_16.name();
075
076    /**
077     * Sixteen-bit Unicode Transformation Format, big-endian byte order.
078     *
079     * <p>
080     * Every implementation of the Java platform is required to support this character encoding.
081     * </p>
082     */
083    public static final String UTF_16BE = StandardCharsets.UTF_16BE.name();
084
085    /**
086     * Sixteen-bit Unicode Transformation Format, little-endian byte order.
087     *
088     * <p>
089     * Every implementation of the Java platform is required to support this character encoding.
090     * </p>
091     */
092    public static final String UTF_16LE = StandardCharsets.UTF_16LE.name();
093
094    /**
095     * Eight-bit Unicode Transformation Format.
096     *
097     * <p>
098     * Every implementation of the Java platform is required to support this character encoding.
099     * </p>
100     */
101    public static final String UTF_8 = StandardCharsets.UTF_8.name();
102
103    /**
104     * Tests whether the named charset is supported.
105     *
106     * <p>
107     * This is similar to <a
108     * href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html#isSupported%28java.lang.String%29">
109     * java.nio.charset.Charset.isSupported(String)</a> but handles more formats
110     * </p>
111     *
112     * @param name  The name of the requested charset; may be either a canonical name or an alias, null returns false
113     * @return {@code true} if the charset is available in the current Java virtual machine
114     * @deprecated Please use {@link Charset#isSupported(String)} instead, although be aware that {@code null}
115     * values are not accepted by that method and an {@link IllegalCharsetNameException} may be thrown.
116     */
117    @Deprecated
118    public static boolean isSupported(final String name) {
119        if (name == null) {
120            return false;
121        }
122        try {
123            return Charset.isSupported(name);
124        } catch (final IllegalCharsetNameException ex) {
125            return false;
126        }
127    }
128
129    /**
130     * Constructs a new instance.
131     *
132     * @deprecated Will be removed in 4.0.0.
133     */
134    @Deprecated
135    public CharEncoding() {
136        // empty
137    }
138}