src/jdk.charsets/share/classes/sun/nio/cs/ext/AbstractCharsetProvider.java
author mgronlun
Mon, 09 Sep 2019 13:26:35 +0200
branchJEP-349-branch
changeset 58049 10ecdb5d3574
parent 47216 71c04702a3d5
permissions -rw-r--r--
JavaThread name
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
30896
7d150914095e 8081452: Move sun.nio.cs.AbstractCharsetProvider into jdk.charset/sun.nio.cs.ext
sherman
parents: 28969
diff changeset
    26
package sun.nio.cs.ext;
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Abstract base class for charset providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class AbstractCharsetProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    extends CharsetProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* Maps canonical names to class names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    50
    private Map<String,String> classMap
36411
f0cd8358b5ea 8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents: 30896
diff changeset
    51
        = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /* Maps alias names to canonical names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    55
    private Map<String,String> aliasMap
36411
f0cd8358b5ea 8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents: 30896
diff changeset
    56
        = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /* Maps canonical names to alias-name arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    60
    private Map<String,String[]> aliasNameMap
36411
f0cd8358b5ea 8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents: 30896
diff changeset
    61
        = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /* Maps canonical names to soft references that hold cached instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    65
    private Map<String,SoftReference<Charset>> cache
36411
f0cd8358b5ea 8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents: 30896
diff changeset
    66
        = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private String packagePrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected AbstractCharsetProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        packagePrefix = "sun.nio.cs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected AbstractCharsetProvider(String pkgPrefixName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        packagePrefix = pkgPrefixName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /* Add an entry to the given map, but only if no mapping yet exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * for the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    81
    private static <K,V> void put(Map<K,V> m, K name, V value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (!m.containsKey(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            m.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    86
    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
    87
        V x  = m.remove(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        assert (x != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /* Declare support for the given charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected void charset(String name, String className, String[] aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            put(classMap, name, className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            for (int i = 0; i < aliases.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                put(aliasMap, aliases[i], name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            put(aliasNameMap, name, aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            cache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected void deleteCharset(String name, String[] aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            remove(classMap, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            for (int i = 0; i < aliases.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                remove(aliasMap, aliases[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            remove(aliasNameMap, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            cache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
28969
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 25859
diff changeset
   113
    protected boolean hasCharset(String name) {
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 25859
diff changeset
   114
        synchronized (this) {
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 25859
diff changeset
   115
            return classMap.containsKey(name);
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 25859
diff changeset
   116
        }
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 25859
diff changeset
   117
    }
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 25859
diff changeset
   118
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /* Late initialization hook, needed by some providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected void init() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    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
   124
        String acn = aliasMap.get(charsetName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return (acn != null) ? acn : charsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private Charset lookup(String csn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // 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
   131
        SoftReference<Charset> sr = cache.get(csn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        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
   133
            Charset cs = sr.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (cs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // 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
   139
        String cln = classMap.get(csn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (cln == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // Instantiate the charset and cache it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   147
            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
   148
                                       true,
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   149
                                       this.getClass().getClassLoader());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 36411
diff changeset
   151
            @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            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
   153
            cache.put(csn, new SoftReference<Charset>(cs));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } catch (InstantiationException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public final Charset charsetForName(String charsetName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return lookup(canonicalize(charsetName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public final Iterator<Charset> charsets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   173
        final ArrayList<String> ks;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            init();
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   176
            ks = new ArrayList<>(classMap.keySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        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
   180
                Iterator<String> i = ks.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    return i.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                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
   187
                    String csn = i.next();
10424
df8a25e78db3 6898310: (cs) Charset cache lookups should be synchronized
sherman
parents: 7668
diff changeset
   188
                    synchronized (AbstractCharsetProvider.this) {
df8a25e78db3 6898310: (cs) Charset cache lookups should be synchronized
sherman
parents: 7668
diff changeset
   189
                        return lookup(csn);
df8a25e78db3 6898310: (cs) Charset cache lookups should be synchronized
sherman
parents: 7668
diff changeset
   190
                    }
2
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 void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public final String[] aliases(String charsetName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            init();
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
   202
            return aliasNameMap.get(charsetName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}