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    package org.apache.logging.log4j.core.util;
018    
019    import java.nio.charset.Charset;
020    
021    import org.apache.logging.log4j.status.StatusLogger;
022    
023    /**
024     * Charset utilities. Contains the standard character sets guaranteed to be available on all implementations of the
025     * Java platform. Parts adapted from JDK 1.7 (in particular, the {@code java.nio.charset.StandardCharsets} class).
026     *
027     * @see java.nio.charset.Charset
028     */
029    public final class Charsets {
030    
031        /**
032         * Seven-bit ASCII. ISO646-US. The Basic Latin block of the Unicode character set.
033         */
034        public static final Charset US_ASCII = Charset.forName("US-ASCII");
035    
036        /**
037         * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
038         */
039        public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
040    
041        /**
042         * Eight-bit UCS Transformation Format.
043         */
044        public static final Charset UTF_8 = Charset.forName("UTF-8");
045    
046        /**
047         * Sixteen-bit UCS Transformation Format, big-endian byte order.
048         */
049        public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
050    
051        /**
052         * Sixteen-bit UCS Transformation Format, little-endian byte order.
053         */
054        public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
055    
056        /**
057         * Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark.
058         */
059        public static final Charset UTF_16 = Charset.forName("UTF-16");
060    
061        /**
062         * Returns a Charset, if possible the Charset for the specified {@code charsetName}, otherwise (if the specified
063         * {@code charsetName} is {@code null} or not supported) this method returns the platform default Charset.
064         *
065         * @param charsetName
066         *            name of the preferred charset or {@code null}
067         * @return a Charset, not null.
068         */
069        public static Charset getSupportedCharset(final String charsetName) {
070            return getSupportedCharset(charsetName, Charset.defaultCharset());
071        }
072    
073        /**
074         * Returns a Charset, if possible the Charset for the specified {@code charsetName}, otherwise (if the specified
075         * {@code charsetName} is {@code null} or not supported) this method returns the platform default Charset.
076         *
077         * @param charsetName
078         *            name of the preferred charset or {@code null}
079         * @param defaultCharset
080         *            returned if {@code charsetName} is null or is not supported.
081         * @return a Charset, never null.
082         */
083        public static Charset getSupportedCharset(final String charsetName, final Charset defaultCharset) {
084            Charset charset = null;
085            if (charsetName != null && Charset.isSupported(charsetName)) {
086                charset = Charset.forName(charsetName);
087            }
088            if (charset == null) {
089                charset = defaultCharset;
090                if (charsetName != null) {
091                    StatusLogger.getLogger().error(
092                            "Charset " + charsetName + " is not supported for layout, using " + charset.displayName());
093                }
094            }
095            return charset;
096        }
097    
098        private Charsets() {
099        }
100    
101    }