View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.util;
18  
19  import java.nio.charset.Charset;
20  
21  import org.apache.logging.log4j.status.StatusLogger;
22  
23  /**
24   * Charset utilities. Contains the standard character sets guaranteed to be available on all implementations of the
25   * Java platform. Parts adapted from JDK 1.7 (in particular, the {@code java.nio.charset.StandardCharsets} class).
26   *
27   * @see java.nio.charset.Charset
28   */
29  public final class Charsets {
30  
31      /**
32       * Seven-bit ASCII. ISO646-US. The Basic Latin block of the Unicode character set.
33       */
34      public static final Charset US_ASCII = Charset.forName("US-ASCII");
35  
36      /**
37       * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
38       */
39      public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
40  
41      /**
42       * Eight-bit UCS Transformation Format.
43       */
44      public static final Charset UTF_8 = Charset.forName("UTF-8");
45  
46      /**
47       * Sixteen-bit UCS Transformation Format, big-endian byte order.
48       */
49      public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
50  
51      /**
52       * Sixteen-bit UCS Transformation Format, little-endian byte order.
53       */
54      public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
55  
56      /**
57       * Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark.
58       */
59      public static final Charset UTF_16 = Charset.forName("UTF-16");
60  
61      /**
62       * Returns a Charset, if possible the Charset for the specified {@code charsetName}, otherwise (if the specified
63       * {@code charsetName} is {@code null} or not supported) this method returns the platform default Charset.
64       *
65       * @param charsetName
66       *            name of the preferred charset or {@code null}
67       * @return a Charset, not null.
68       */
69      public static Charset getSupportedCharset(final String charsetName) {
70          return getSupportedCharset(charsetName, Charset.defaultCharset());
71      }
72  
73      /**
74       * Returns a Charset, if possible the Charset for the specified {@code charsetName}, otherwise (if the specified
75       * {@code charsetName} is {@code null} or not supported) this method returns the platform default Charset.
76       *
77       * @param charsetName
78       *            name of the preferred charset or {@code null}
79       * @param defaultCharset
80       *            returned if {@code charsetName} is null or is not supported.
81       * @return a Charset, never null.
82       */
83      public static Charset getSupportedCharset(final String charsetName, final Charset defaultCharset) {
84          Charset charset = null;
85          if (charsetName != null && Charset.isSupported(charsetName)) {
86              charset = Charset.forName(charsetName);
87          }
88          if (charset == null) {
89              charset = defaultCharset;
90              if (charsetName != null) {
91                  StatusLogger.getLogger().error(
92                          "Charset " + charsetName + " is not supported for layout, using " + charset.displayName());
93              }
94          }
95          return charset;
96      }
97  
98      private Charsets() {
99      }
100 
101 }