jdk/src/java.base/share/classes/sun/nio/cs/AbstractCharsetProvider.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 14342 jdk/src/share/classes/sun/nio/cs/AbstractCharsetProvider.java@8435a30053c1
child 28969 f980bee32887
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10424
diff changeset
     2
 * Copyright (c) 2000, 2011, 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.nio.cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.charset.spi.CharsetProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.TreeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.misc.ASCIICaseInsensitiveComparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Abstract base class for charset providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class AbstractCharsetProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    extends CharsetProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /* Maps canonical names to class names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    51
    private Map<String,String> classMap
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    52
        = new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /* Maps alias names to canonical names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    56
    private Map<String,String> aliasMap
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    57
        = new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /* Maps canonical names to alias-name arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    61
    private Map<String,String[]> aliasNameMap
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    62
        = new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /* Maps canonical names to soft references that hold cached instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    66
    private Map<String,SoftReference<Charset>> cache
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    67
        = new TreeMap<>(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private String packagePrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected AbstractCharsetProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        packagePrefix = "sun.nio.cs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected AbstractCharsetProvider(String pkgPrefixName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        packagePrefix = pkgPrefixName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /* Add an entry to the given map, but only if no mapping yet exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * for the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    82
    private static <K,V> void put(Map<K,V> m, K name, V value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (!m.containsKey(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            m.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    87
    private static <K,V> void remove(Map<K,V> m, K name) {
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    88
        V x  = m.remove(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        assert (x != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /* Declare support for the given charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    protected void charset(String name, String className, String[] aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            put(classMap, name, className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            for (int i = 0; i < aliases.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                put(aliasMap, aliases[i], name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            put(aliasNameMap, name, aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            cache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected void deleteCharset(String name, String[] aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            remove(classMap, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            for (int i = 0; i < aliases.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                remove(aliasMap, aliases[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            remove(aliasNameMap, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            cache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /* Late initialization hook, needed by some providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    protected void init() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private String canonicalize(String charsetName) {
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   119
        String acn = aliasMap.get(charsetName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return (acn != null) ? acn : charsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private Charset lookup(String csn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // Check cache first
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   126
        SoftReference<Charset> sr = cache.get(csn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (sr != null) {
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   128
            Charset cs = sr.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (cs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // Do we even support this charset?
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   134
        String cln = classMap.get(csn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (cln == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // Instantiate the charset and cache it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   142
            Class<?> c = Class.forName(packagePrefix + "." + cln,
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   143
                                       true,
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   144
                                       this.getClass().getClassLoader());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            Charset cs = (Charset)c.newInstance();
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   147
            cache.put(csn, new SoftReference<Charset>(cs));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } catch (InstantiationException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public final Charset charsetForName(String charsetName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return lookup(canonicalize(charsetName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public final Iterator<Charset> charsets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   167
        final ArrayList<String> ks;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            init();
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   170
            ks = new ArrayList<>(classMap.keySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return new Iterator<Charset>() {
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   174
                Iterator<String> i = ks.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    return i.hasNext();
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 Charset next() {
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   181
                    String csn = i.next();
10424
df8a25e78db3 6898310: (cs) Charset cache lookups should be synchronized
sherman
parents: 7668
diff changeset
   182
                    synchronized (AbstractCharsetProvider.this) {
df8a25e78db3 6898310: (cs) Charset cache lookups should be synchronized
sherman
parents: 7668
diff changeset
   183
                        return lookup(csn);
df8a25e78db3 6898310: (cs) Charset cache lookups should be synchronized
sherman
parents: 7668
diff changeset
   184
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
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
    public final String[] aliases(String charsetName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            init();
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   196
            return aliasNameMap.get(charsetName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}