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.codec; 018 019import java.nio.charset.Charset; 020 021/** 022 * Charsets required of every implementation of the Java platform. 023 * 024 * From the Java documentation <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard 025 * charsets</a>: 026 * <p> 027 * <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the 028 * release documentation for your implementation to see if any other encodings are supported. Consult the release 029 * documentation for your implementation to see if any other encodings are supported. </cite> 030 * </p> 031 * 032 * <ul> 033 * <li><code>US-ASCII</code><p> 034 * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</p></li> 035 * <li><code>ISO-8859-1</code><p> 036 * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</p></li> 037 * <li><code>UTF-8</code><p> 038 * Eight-bit Unicode Transformation Format.</p></li> 039 * <li><code>UTF-16BE</code><p> 040 * Sixteen-bit Unicode Transformation Format, big-endian byte order.</p></li> 041 * <li><code>UTF-16LE</code><p> 042 * Sixteen-bit Unicode Transformation Format, little-endian byte order.</p></li> 043 * <li><code>UTF-16</code><p> 044 * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order 045 * accepted on input, big-endian used on output.)</p></li> 046 * </ul> 047 * 048 * This perhaps would best belong in the Commons Lang project. Even if a similar class is defined in Commons Lang, it is 049 * not foreseen that Commons Codec would be made to depend on Commons Lang. 050 * 051 * <p> 052 * This class is immutable and thread-safe. 053 * </p> 054 * 055 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> 056 * @since 1.7 057 */ 058public class Charsets { 059 060 // 061 // This class should only contain Charset instances for required encodings. This guarantees that it will load 062 // correctly and without delay on all Java platforms. 063 // 064 065 /** 066 * Returns the given Charset or the default Charset if the given Charset is null. 067 * 068 * @param charset 069 * A charset or null. 070 * @return the given Charset or the default Charset if the given Charset is null 071 */ 072 public static Charset toCharset(final Charset charset) { 073 return charset == null ? Charset.defaultCharset() : charset; 074 } 075 076 /** 077 * Returns a Charset for the named charset. If the name is null, return the default Charset. 078 * 079 * @param charset 080 * The name of the requested charset, may be null. 081 * @return a Charset for the named charset 082 * @throws java.nio.charset.UnsupportedCharsetException 083 * If the named charset is unavailable 084 */ 085 public static Charset toCharset(final String charset) { 086 return charset == null ? Charset.defaultCharset() : Charset.forName(charset); 087 } 088 089 /** 090 * CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1. 091 * <p> 092 * Every implementation of the Java platform is required to support this character encoding. 093 * </p> 094 * <p> 095 * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead. 096 * </p> 097 * 098 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> 099 */ 100 public static final Charset ISO_8859_1 = Charset.forName(CharEncoding.ISO_8859_1); 101 102 /** 103 * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set. 104 * <p> 105 * Every implementation of the Java platform is required to support this character encoding. 106 * </p> 107 * <p> 108 * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead. 109 * </p> 110 * 111 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> 112 */ 113 public static final Charset US_ASCII = Charset.forName(CharEncoding.US_ASCII); 114 115 /** 116 * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark 117 * (either order accepted on input, big-endian used on output) 118 * <p> 119 * Every implementation of the Java platform is required to support this character encoding. 120 * </p> 121 * <p> 122 * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead. 123 * </p> 124 * 125 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> 126 */ 127 public static final Charset UTF_16 = Charset.forName(CharEncoding.UTF_16); 128 129 /** 130 * Sixteen-bit Unicode Transformation Format, big-endian byte order. 131 * <p> 132 * Every implementation of the Java platform is required to support this character encoding. 133 * </p> 134 * <p> 135 * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead. 136 * </p> 137 * 138 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> 139 */ 140 public static final Charset UTF_16BE = Charset.forName(CharEncoding.UTF_16BE); 141 142 /** 143 * Sixteen-bit Unicode Transformation Format, little-endian byte order. 144 * <p> 145 * Every implementation of the Java platform is required to support this character encoding. 146 * </p> 147 * <p> 148 * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead. 149 * </p> 150 * 151 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> 152 */ 153 public static final Charset UTF_16LE = Charset.forName(CharEncoding.UTF_16LE); 154 155 /** 156 * Eight-bit Unicode Transformation Format. 157 * <p> 158 * Every implementation of the Java platform is required to support this character encoding. 159 * </p> 160 * <p> 161 * On Java 7 or later, use {@link java.nio.charset.StandardCharsets#ISO_8859_1} instead. 162 * </p> 163 * 164 * @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> 165 */ 166 public static final Charset UTF_8 = Charset.forName(CharEncoding.UTF_8); 167}