Converters.java revision 3261
286N/A/*
286N/A * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/Apackage sun.io;
286N/A
286N/Aimport java.io.UnsupportedEncodingException;
286N/Aimport java.lang.ref.SoftReference;
286N/Aimport java.util.Properties;
286N/A
286N/A/**
286N/A * Package-private utility class that caches the default converter classes and
286N/A * provides other logic common to both the ByteToCharConverter and
286N/A * CharToByteConverter classes.
286N/A *
286N/A * @author Mark Reinhold
286N/A * @since 1.2
286N/A *
286N/A * @deprecated Replaced by {@link java.nio.charset}. THIS API WILL BE
286N/A * REMOVED IN J2SE 1.6.
286N/A */
286N/A@Deprecated
286N/Apublic class Converters {
286N/A
286N/A private Converters() { } /* To prevent instantiation */
286N/A
286N/A /* Lock for all static fields in this class */
286N/A private static Object lock = Converters.class;
286N/A
286N/A /* Cached values of system properties */
286N/A private static String converterPackageName = null; /* file.encoding.pkg */
286N/A private static String defaultEncoding = null; /* file.encoding */
286N/A
286N/A /* Converter type constants and names */
286N/A public static final int BYTE_TO_CHAR = 0;
286N/A public static final int CHAR_TO_BYTE = 1;
286N/A private static final String[] converterPrefix = { "ByteToChar",
286N/A "CharToByte" };
286N/A
286N/A
286N/A // -- Converter class cache --
286N/A
286N/A private static final int CACHE_SIZE = 3;
286N/A
286N/A /* For the default charset, whatever it turns out to be */
286N/A private static final Object DEFAULT_NAME = new Object();
286N/A
286N/A /* Cached converter classes, CACHE_SIZE per converter type. Each cache
286N/A * entry is a soft reference to a two-object array; the first element of
286N/A * the array is the converter class, the second is an object (typically a
286N/A * string) representing the encoding name that was used to request the
286N/A * converter, e.g.,
286N/A *
286N/A * ((Object[])classCache[CHAR_TO_BYTE][i].get())[0]
286N/A *
286N/A * will be a CharToByteConverter and
286N/A *
286N/A * ((Object[])classCache[CHAR_TO_BYTE][i].get())[1]
286N/A *
286N/A * will be the string encoding name used to request it, assuming that cache
286N/A * entry i is valid.
286N/A *
286N/A * Ordinarily we'd do this with a private static utility class, but since
286N/A * this code can be involved in the startup sequence it's important to keep
286N/A * the footprint down.
286N/A */
286N/A @SuppressWarnings("unchecked")
286N/A private static SoftReference<Object[]>[][] classCache
286N/A = (SoftReference<Object[]>[][]) new SoftReference<?>[][] {
286N/A new SoftReference<?>[CACHE_SIZE],
286N/A new SoftReference<?>[CACHE_SIZE]
286N/A };
286N/A
286N/A private static void moveToFront(Object[] oa, int i) {
286N/A Object ob = oa[i];
286N/A for (int j = i; j > 0; j--)
286N/A oa[j] = oa[j - 1];
286N/A oa[0] = ob;
286N/A }
286N/A
286N/A private static Class<?> cache(int type, Object encoding) {
286N/A SoftReference<Object[]>[] srs = classCache[type];
286N/A for (int i = 0; i < CACHE_SIZE; i++) {
286N/A SoftReference<Object[]> sr = srs[i];
286N/A if (sr == null)
286N/A continue;
286N/A Object[] oa = sr.get();
286N/A if (oa == null) {
286N/A srs[i] = null;
286N/A continue;
286N/A }
286N/A if (oa[1].equals(encoding)) {
286N/A moveToFront(srs, i);
286N/A return (Class<?>)oa[0];
286N/A }
286N/A }
286N/A return null;
286N/A }
286N/A
286N/A private static Class<?> cache(int type, Object encoding, Class<?> c) {
286N/A SoftReference<Object[]>[] srs = classCache[type];
286N/A srs[CACHE_SIZE - 1] = new SoftReference<Object[]>(new Object[] { c, encoding });
286N/A moveToFront(srs, CACHE_SIZE - 1);
286N/A return c;
286N/A }
286N/A
286N/A /* Used to avoid doing expensive charset lookups for charsets that are not
286N/A * yet directly supported by NIO.
286N/A */
286N/A public static boolean isCached(int type, String encoding) {
286N/A synchronized (lock) {
286N/A SoftReference<Object[]>[] srs = classCache[type];
286N/A for (int i = 0; i < CACHE_SIZE; i++) {
286N/A SoftReference<Object[]> sr = srs[i];
286N/A if (sr == null)
286N/A continue;
286N/A Object[] oa = sr.get();
286N/A if (oa == null) {
286N/A srs[i] = null;
286N/A continue;
286N/A }
286N/A if (oa[1].equals(encoding))
286N/A return true;
286N/A }
286N/A return false;
286N/A }
286N/A }
286N/A
286N/A
286N/A
286N/A /** Get the name of the converter package */
286N/A private static String getConverterPackageName() {
286N/A String cp = converterPackageName;
286N/A if (cp != null) return cp;
286N/A java.security.PrivilegedAction<String> pa =
286N/A new sun.security.action.GetPropertyAction("file.encoding.pkg");
286N/A cp = java.security.AccessController.doPrivileged(pa);
286N/A if (cp != null) {
286N/A /* Property is set, so take it as the true converter package */
286N/A converterPackageName = cp;
286N/A } else {
286N/A /* Fall back to sun.io */
286N/A cp = "sun.io";
286N/A }
286N/A return cp;
286N/A }
286N/A
286N/A public static String getDefaultEncodingName() {
286N/A synchronized (lock) {
286N/A if (defaultEncoding == null) {
286N/A java.security.PrivilegedAction<String> pa =
286N/A new sun.security.action.GetPropertyAction("file.encoding");
286N/A defaultEncoding = java.security.AccessController.doPrivileged(pa);
286N/A }
286N/A }
286N/A return defaultEncoding;
286N/A }
286N/A
286N/A public static void resetDefaultEncodingName() {
286N/A // This method should only be called during VM initialization.
286N/A if (sun.misc.VM.isBooted())
286N/A return;
286N/A
286N/A synchronized (lock) {
286N/A defaultEncoding = "ISO-8859-1";
286N/A Properties p = System.getProperties();
286N/A p.setProperty("file.encoding", defaultEncoding);
286N/A System.setProperties(p);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Get the class that implements the given type of converter for the named
286N/A * encoding, or throw an UnsupportedEncodingException if no such class can
286N/A * be found
286N/A */
286N/A private static Class<?> getConverterClass(int type, String encoding)
286N/A throws UnsupportedEncodingException
286N/A {
286N/A String enc = null;
286N/A
286N/A /* "ISO8859_1" is the canonical name for the ISO-Latin-1 encoding.
286N/A Native code in the JDK commonly uses the alias "8859_1" instead of
286N/A "ISO8859_1". We hardwire this alias here in order to avoid loading
286N/A the full alias table just for this case. */
286N/A if (!encoding.equals("ISO8859_1")) {
286N/A if (encoding.equals("8859_1")) {
286N/A enc = "ISO8859_1";
286N/A /*
286N/A * On Solaris with nl_langinfo() called in GetJavaProperties():
286N/A *
286N/A * locale undefined -> NULL -> hardcoded default
286N/A * "C" locale -> "" -> hardcoded default (on 2.6)
286N/A * "C" locale -> "646" (on 2.7)
286N/A * "en_US" locale -> "ISO8859-1"
286N/A * "en_GB" locale -> "ISO8859-1" (on 2.7)
286N/A * "en_UK" locale -> "ISO8859-1" (on 2.6)
286N/A */
286N/A } else if (encoding.equals("ISO8859-1")) {
286N/A enc = "ISO8859_1";
286N/A } else if (encoding.equals("646")) {
286N/A enc = "ASCII";
286N/A } else {
286N/A enc = CharacterEncoding.aliasName(encoding);
286N/A }
286N/A }
286N/A if (enc == null) {
286N/A enc = encoding;
286N/A }
286N/A
286N/A try {
286N/A return Class.forName(getConverterPackageName()
286N/A + "." + converterPrefix[type] + enc);
286N/A } catch(ClassNotFoundException e) {
286N/A throw new UnsupportedEncodingException(enc);
286N/A }
286N/A
286N/A }
286N/A
286N/A /**
286N/A * Instantiate the given converter class, or throw an
286N/A * UnsupportedEncodingException if it cannot be instantiated
286N/A */
286N/A private static Object newConverter(String enc, Class<?> c)
286N/A throws UnsupportedEncodingException
286N/A {
286N/A try {
286N/A return c.newInstance();
286N/A } catch(InstantiationException e) {
286N/A throw new UnsupportedEncodingException(enc);
286N/A } catch(IllegalAccessException e) {
286N/A throw new UnsupportedEncodingException(enc);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Create a converter object that implements the given type of converter
286N/A * for the given encoding, or throw an UnsupportedEncodingException if no
286N/A * appropriate converter class can be found and instantiated
286N/A */
286N/A static Object newConverter(int type, String enc)
286N/A throws UnsupportedEncodingException
286N/A {
286N/A Class<?> c;
286N/A synchronized (lock) {
286N/A c = cache(type, enc);
286N/A if (c == null) {
286N/A c = getConverterClass(type, enc);
286N/A if (!c.getName().equals("sun.io.CharToByteUTF8"))
286N/A cache(type, enc, c);
286N/A }
286N/A }
286N/A return newConverter(enc, c);
286N/A }
286N/A
286N/A /**
286N/A * Find the class that implements the given type of converter for the
286N/A * default encoding. If the default encoding cannot be determined or is
286N/A * not yet defined, return a class that implements the fallback default
286N/A * encoding, which is just ISO 8859-1.
286N/A */
286N/A private static Class<?> getDefaultConverterClass(int type) {
286N/A boolean fillCache = false;
286N/A Class<?> c;
286N/A
286N/A /* First check the class cache */
286N/A c = cache(type, DEFAULT_NAME);
286N/A if (c != null)
286N/A return c;
286N/A
286N/A /* Determine the encoding name */
286N/A String enc = getDefaultEncodingName();
286N/A if (enc != null) {
286N/A /* file.encoding has been set, so cache the converter class */
286N/A fillCache = true;
286N/A } else {
286N/A /* file.encoding has not been set, so use a default encoding which
286N/A will not be cached */
286N/A enc = "ISO8859_1";
286N/A }
286N/A
286N/A /* We have an encoding name; try to find its class */
286N/A try {
286N/A c = getConverterClass(type, enc);
286N/A if (fillCache) {
286N/A cache(type, DEFAULT_NAME, c);
286N/A }
286N/A } catch (UnsupportedEncodingException x) {
286N/A /* Can't find the default class, so fall back to ISO 8859-1 */
286N/A try {
286N/A c = getConverterClass(type, "ISO8859_1");
286N/A } catch (UnsupportedEncodingException y) {
286N/A throw new InternalError("Cannot find default "
286N/A + converterPrefix[type]
286N/A + " converter class");
286N/A }
286N/A }
286N/A return c;
286N/A
286N/A }
286N/A
286N/A /**
286N/A * Create a converter object that implements the given type of converter
286N/A * for the default encoding, falling back to ISO 8859-1 if the default
286N/A * encoding cannot be determined.
286N/A */
286N/A static Object newDefaultConverter(int type) {
286N/A Class<?> c;
286N/A synchronized (lock) {
286N/A c = getDefaultConverterClass(type);
286N/A }
286N/A try {
286N/A return newConverter("", c);
286N/A } catch (UnsupportedEncodingException x) {
286N/A throw new InternalError("Cannot instantiate default converter"
286N/A + " class " + c.getName());
286N/A }
286N/A }
286N/A
286N/A}
286N/A