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 */ 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 * Constructs a sorted map from canonical charset names to charset objects required of every implementation of the 161 * Java platform. 162 * <p> 163 * From the Java documentation <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html"> 164 * Standard charsets</a>: 165 * </p> 166 * 167 * @return An immutable, case-insensitive map from canonical charset names to charset objects. 168 * @see Charset#availableCharsets() 169 * @since 2.5 170 */ 171 public static SortedMap<String, Charset> requiredCharsets() { 172 return STANDARD_CHARSET_MAP; 173 } 174 175 /** 176 * Returns the given Charset or the default Charset if the given Charset is null. 177 * 178 * @param charset 179 * A charset or null. 180 * @return the given Charset or the default Charset if the given Charset is null 181 */ 182 public static Charset toCharset(final Charset charset) { 183 return charset == null ? Charset.defaultCharset() : charset; 184 } 185 186 /** 187 * Returns the given charset if non-null, otherwise return defaultCharset. 188 * 189 * @param charset The charset to test, may be null. 190 * @param defaultCharset The charset to return if charset is null, may be null. 191 * @return a Charset . 192 * @since 2.12.0 193 */ 194 public static Charset toCharset(final Charset charset, final Charset defaultCharset) { 195 return charset == null ? defaultCharset : charset; 196 } 197 198 /** 199 * Returns a Charset for the named charset. If the name is null, return the default Charset. 200 * 201 * @param charsetName The name of the requested charset, may be null. 202 * @return a Charset for the named charset. 203 * @throws UnsupportedCharsetException If the named charset is unavailable (unchecked exception). 204 */ 205 public static Charset toCharset(final String charsetName) throws UnsupportedCharsetException { 206 return toCharset(charsetName, Charset.defaultCharset()); 207 } 208 209 /** 210 * Returns a Charset for the named charset. If the name is null, return the given default Charset. 211 * 212 * @param charsetName The name of the requested charset, may be null. 213 * @param defaultCharset The name charset to return if charsetName is null, may be null. 214 * @return a Charset for the named charset. 215 * @throws UnsupportedCharsetException If the named charset is unavailable (unchecked exception). 216 * @since 2.12.0 217 */ 218 public static Charset toCharset(final String charsetName, final Charset defaultCharset) throws UnsupportedCharsetException { 219 return charsetName == null ? defaultCharset : Charset.forName(charsetName); 220 } 221}