jdk/src/share/classes/sun/io/Converters.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 7816 55a18147b4bf
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5506
diff changeset
     2
 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4818
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4818
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4818
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4818
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4818
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Package-private utility class that caches the default converter classes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * provides other logic common to both the ByteToCharConverter and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * CharToByteConverter classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author   Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @since    1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @deprecated Replaced by {@link java.nio.charset}.  THIS API WILL BE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * REMOVED IN J2SE 1.6.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
@Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class Converters {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private Converters() { }    /* To prevent instantiation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* Lock for all static fields in this class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static Object lock = Converters.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /* Cached values of system properties */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static String converterPackageName = null;  /* file.encoding.pkg */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static String defaultEncoding = null;       /* file.encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /* Converter type constants and names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static final int BYTE_TO_CHAR = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final int CHAR_TO_BYTE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final String[] converterPrefix = { "ByteToChar",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                                      "CharToByte" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // -- Converter class cache --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final int CACHE_SIZE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /* For the default charset, whatever it turns out to be */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final Object DEFAULT_NAME = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /* Cached converter classes, CACHE_SIZE per converter type.  Each cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * entry is a soft reference to a two-object array; the first element of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * the array is the converter class, the second is an object (typically a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * string) representing the encoding name that was used to request the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * converter, e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *     ((Object[])classCache[CHAR_TO_BYTE][i].get())[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * will be a CharToByteConverter and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *     ((Object[])classCache[CHAR_TO_BYTE][i].get())[1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * will be the string encoding name used to request it, assuming that cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * entry i is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Ordinarily we'd do this with a private static utility class, but since
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * this code can be involved in the startup sequence it's important to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * the footprint down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    88
    @SuppressWarnings("unchecked")
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    89
    private static SoftReference<Object[]>[][] classCache
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    90
        = (SoftReference<Object[]>[][]) new SoftReference<?>[][] {
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    91
            new SoftReference<?>[CACHE_SIZE],
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    92
            new SoftReference<?>[CACHE_SIZE]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static void moveToFront(Object[] oa, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Object ob = oa[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        for (int j = i; j > 0; j--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            oa[j] = oa[j - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        oa[0] = ob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   102
    private static Class<?> cache(int type, Object encoding) {
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   103
        SoftReference<Object[]>[] srs = classCache[type];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for (int i = 0; i < CACHE_SIZE; i++) {
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   105
            SoftReference<Object[]> sr = srs[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (sr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                continue;
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   108
            Object[] oa = sr.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (oa == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                srs[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (oa[1].equals(encoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                moveToFront(srs, i);
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   115
                return (Class<?>)oa[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   121
    private static Class<?> cache(int type, Object encoding, Class<?> c) {
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   122
        SoftReference<Object[]>[] srs = classCache[type];
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   123
        srs[CACHE_SIZE - 1] = new SoftReference<Object[]>(new Object[] { c, encoding });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        moveToFront(srs, CACHE_SIZE - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /* Used to avoid doing expensive charset lookups for charsets that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * yet directly supported by NIO.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static boolean isCached(int type, String encoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        synchronized (lock) {
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   133
            SoftReference<Object[]>[] srs = classCache[type];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            for (int i = 0; i < CACHE_SIZE; i++) {
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   135
                SoftReference<Object[]> sr = srs[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (sr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    continue;
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   138
                Object[] oa = sr.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                if (oa == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    srs[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (oa[1].equals(encoding))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /** Get the name of the converter package */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static String getConverterPackageName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        String cp = converterPackageName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (cp != null) return cp;
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   156
        java.security.PrivilegedAction<String> pa =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            new sun.security.action.GetPropertyAction("file.encoding.pkg");
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   158
        cp = java.security.AccessController.doPrivileged(pa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (cp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            /* Property is set, so take it as the true converter package */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            converterPackageName = cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            /* Fall back to sun.io */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            cp = "sun.io";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public static String getDefaultEncodingName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (defaultEncoding == null) {
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   172
                java.security.PrivilegedAction<String> pa =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    new sun.security.action.GetPropertyAction("file.encoding");
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   174
                defaultEncoding = java.security.AccessController.doPrivileged(pa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return defaultEncoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public static void resetDefaultEncodingName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // This method should only be called during VM initialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (sun.misc.VM.isBooted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            defaultEncoding = "ISO-8859-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            Properties p = System.getProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            p.setProperty("file.encoding", defaultEncoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            System.setProperties(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Get the class that implements the given type of converter for the named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * encoding, or throw an UnsupportedEncodingException if no such class can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   198
    private static Class<?> getConverterClass(int type, String encoding)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        throws UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        String enc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        /* "ISO8859_1" is the canonical name for the ISO-Latin-1 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
           Native code in the JDK commonly uses the alias "8859_1" instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
           "ISO8859_1".  We hardwire this alias here in order to avoid loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
           the full alias table just for this case. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (!encoding.equals("ISO8859_1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (encoding.equals("8859_1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                enc = "ISO8859_1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
             * On Solaris with nl_langinfo() called in GetJavaProperties():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
             *   locale undefined -> NULL -> hardcoded default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
             *   "C" locale       -> "" -> hardcoded default    (on 2.6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
             *   "C" locale       -> "646"                      (on 2.7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
             *   "en_US" locale -> "ISO8859-1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
             *   "en_GB" locale -> "ISO8859-1"                  (on 2.7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
             *   "en_UK" locale -> "ISO8859-1"                  (on 2.6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            } else if (encoding.equals("ISO8859-1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                enc = "ISO8859_1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            } else if (encoding.equals("646")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                enc = "ASCII";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                enc = CharacterEncoding.aliasName(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (enc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            enc = encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return Class.forName(getConverterPackageName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                 + "." + converterPrefix[type] + enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } catch(ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new UnsupportedEncodingException(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Instantiate the given converter class, or throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * UnsupportedEncodingException if it cannot be instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   245
    private static Object newConverter(String enc, Class<?> c)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        throws UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        } catch(InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new UnsupportedEncodingException(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } catch(IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throw new UnsupportedEncodingException(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Create a converter object that implements the given type of converter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * for the given encoding, or throw an UnsupportedEncodingException if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * appropriate converter class can be found and instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    static Object newConverter(int type, String enc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        throws UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    {
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   265
        Class<?> c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            c = cache(type, enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                c = getConverterClass(type, enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                if (!c.getName().equals("sun.io.CharToByteUTF8"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    cache(type, enc, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return newConverter(enc, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Find the class that implements the given type of converter for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * default encoding.  If the default encoding cannot be determined or is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * not yet defined, return a class that implements the fallback default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * encoding, which is just ISO 8859-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   283
    private static Class<?> getDefaultConverterClass(int type) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        boolean fillCache = false;
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   285
        Class<?> c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        /* First check the class cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        c = cache(type, DEFAULT_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (c != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        /* Determine the encoding name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        String enc = getDefaultEncodingName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (enc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            /* file.encoding has been set, so cache the converter class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            fillCache = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            /* file.encoding has not been set, so use a default encoding which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
               will not be cached */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            enc = "ISO8859_1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        /* We have an encoding name; try to find its class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            c = getConverterClass(type, enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (fillCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                cache(type, DEFAULT_NAME, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            /* Can't find the default class, so fall back to ISO 8859-1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                c = getConverterClass(type, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            } catch (UnsupportedEncodingException y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                throw new InternalError("Cannot find default "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                        + converterPrefix[type]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                        + " converter class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Create a converter object that implements the given type of converter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * for the default encoding, falling back to ISO 8859-1 if the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * encoding cannot be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    static Object newDefaultConverter(int type) {
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   329
        Class<?> c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            c = getDefaultConverterClass(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return newConverter("", c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            throw new InternalError("Cannot instantiate default converter"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                    + " class " + c.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
}