jdk/src/share/classes/java/lang/StringCoding.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7975 f0de2d05f34c
child 9547 454881baaca0
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7975
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: 2294
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: 2294
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: 2294
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2294
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2294
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 java.lang;
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.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.charset.CharsetDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.charset.CharacterCodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.charset.CoderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.nio.charset.CodingErrorAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.charset.IllegalCharsetNameException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.nio.charset.UnsupportedCharsetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.misc.MessageUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.nio.cs.HistoricallyNamedCharset;
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
    43
import sun.nio.cs.ArrayDecoder;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
    44
import sun.nio.cs.ArrayEncoder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Utility class for string encoding and decoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
class StringCoding {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private StringCoding() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
41
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
    54
    /** The cached coders for each thread */
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
    55
    private final static ThreadLocal<SoftReference<StringDecoder>> decoder =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    56
        new ThreadLocal<>();
41
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
    57
    private final static ThreadLocal<SoftReference<StringEncoder>> encoder =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    58
        new ThreadLocal<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static boolean warnUnsupportedCharset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
41
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
    62
    private static <T> T deref(ThreadLocal<SoftReference<T>> tl) {
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
    63
        SoftReference<T> sr = tl.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (sr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return sr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
41
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
    69
    private static <T> void set(ThreadLocal<SoftReference<T>> tl, T ob) {
7975
f0de2d05f34c 7011095: revert diamond changes from 6880112 that occur in method args
smarks
parents: 7803
diff changeset
    70
        tl.set(new SoftReference<T>(ob));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // Trim the given byte array to the given length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    //
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
    75
    private static byte[] safeTrim(byte[] ba, int len, Charset cs, boolean isTrusted) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
    76
        if (len == ba.length && (isTrusted || System.getSecurityManager() == null))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return ba;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            return Arrays.copyOf(ba, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // Trim the given char array to the given length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    //
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
    84
    private static char[] safeTrim(char[] ca, int len,
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
    85
                                   Charset cs, boolean isTrusted) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
    86
        if (len == ca.length && (isTrusted || System.getSecurityManager() == null))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return ca;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return Arrays.copyOf(ca, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static int scale(int len, float expansionFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // We need to perform double, not float, arithmetic; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // we lose low order bits when len is larger than 2**24.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return (int)(len * (double)expansionFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static Charset lookupCharset(String csn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (Charset.isSupported(csn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                return Charset.forName(csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            } catch (UnsupportedCharsetException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                throw new Error(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static void warnUnsupportedCharset(String csn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (warnUnsupportedCharset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            // Use sun.misc.MessageUtils rather than the Logging API or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // System.err since this method may be called during VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // initialization before either is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            MessageUtils.err("WARNING: Default charset " + csn +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                             " not supported, using ISO-8859-1 instead");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            warnUnsupportedCharset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // -- Decoding --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static class StringDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        private final String requestedCharsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        private final Charset cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        private final CharsetDecoder cd;
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   126
        private final boolean isTrusted;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        private StringDecoder(Charset cs, String rcn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            this.requestedCharsetName = rcn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            this.cs = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            this.cd = cs.newDecoder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                .onMalformedInput(CodingErrorAction.REPLACE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                .onUnmappableCharacter(CodingErrorAction.REPLACE);
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   134
            this.isTrusted = (cs.getClass().getClassLoader0() == null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        String charsetName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (cs instanceof HistoricallyNamedCharset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                return ((HistoricallyNamedCharset)cs).historicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return cs.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        final String requestedCharsetName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return requestedCharsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        char[] decode(byte[] ba, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            int en = scale(len, cd.maxCharsPerByte());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            char[] ca = new char[en];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                return ca;
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   152
            if (cd instanceof ArrayDecoder) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   153
                int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   154
                return safeTrim(ca, clen, cs, isTrusted);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   155
            } else {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   156
                cd.reset();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   157
                ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   158
                CharBuffer cb = CharBuffer.wrap(ca);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   159
                try {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   160
                    CoderResult cr = cd.decode(bb, cb, true);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   161
                    if (!cr.isUnderflow())
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   162
                        cr.throwException();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   163
                    cr = cd.flush(cb);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   164
                    if (!cr.isUnderflow())
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   165
                        cr.throwException();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   166
                } catch (CharacterCodingException x) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   167
                    // Substitution is always enabled,
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   168
                    // so this shouldn't happen
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   169
                    throw new Error(x);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   170
                }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   171
                return safeTrim(ca, cb.position(), cs, isTrusted);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    static char[] decode(String charsetName, byte[] ba, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        throws UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    {
41
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
   179
        StringDecoder sd = deref(decoder);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if ((sd == null) || !(csn.equals(sd.requestedCharsetName())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                              || csn.equals(sd.charsetName()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            sd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                Charset cs = lookupCharset(csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                if (cs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    sd = new StringDecoder(cs, csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            } catch (IllegalCharsetNameException x) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (sd == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                throw new UnsupportedEncodingException(csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            set(decoder, sd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return sd.decode(ba, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    static char[] decode(Charset cs, byte[] ba, int off, int len) {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   197
        // (1)We never cache the "external" cs, the only benefit of creating
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   198
        // an additional StringDe/Encoder object to wrap it is to share the
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   199
        // de/encode() method. These SD/E objects are short-lifed, the young-gen
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   200
        // gc should be able to take care of them well. But the best approash
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   201
        // is still not to generate them if not really necessary.
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   202
        // (2)The defensive copy of the input byte/char[] has a big performance
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   203
        // impact, as well as the outgoing result byte/char[]. Need to do the
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   204
        // optimization check of (sm==null && classLoader0==null) for both.
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   205
        // (3)getClass().getClassLoader0() is expensive
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   206
        // (4)There might be a timing gap in isTrusted setting. getClassLoader0()
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   207
        // is only chcked (and then isTrusted gets set) when (SM==null). It is
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   208
        // possible that the SM==null for now but then SM is NOT null later
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   209
        // when safeTrim() is invoked...the "safe" way to do is to redundant
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   210
        // check (... && (isTrusted || SM == null || getClassLoader0())) in trim
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   211
        // but it then can be argued that the SM is null when the opertaion
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   212
        // is started...
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   213
        CharsetDecoder cd = cs.newDecoder();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   214
        int en = scale(len, cd.maxCharsPerByte());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   215
        char[] ca = new char[en];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   216
        if (len == 0)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   217
            return ca;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   218
        boolean isTrusted = false;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   219
        if (System.getSecurityManager() != null) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   220
            if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   221
                ba =  Arrays.copyOfRange(ba, off, off + len);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   222
                off = 0;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   223
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   224
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   225
        if (cd instanceof ArrayDecoder) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   226
            int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   227
            return safeTrim(ca, clen, cs, isTrusted);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   228
        } else {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   229
            cd.onMalformedInput(CodingErrorAction.REPLACE)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   230
              .onUnmappableCharacter(CodingErrorAction.REPLACE)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   231
              .reset();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   232
            ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   233
            CharBuffer cb = CharBuffer.wrap(ca);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   234
            try {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   235
                CoderResult cr = cd.decode(bb, cb, true);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   236
                if (!cr.isUnderflow())
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   237
                    cr.throwException();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   238
                cr = cd.flush(cb);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   239
                if (!cr.isUnderflow())
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   240
                    cr.throwException();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   241
            } catch (CharacterCodingException x) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   242
                // Substitution is always enabled,
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   243
                // so this shouldn't happen
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   244
                throw new Error(x);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   245
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   246
            return safeTrim(ca, cb.position(), cs, isTrusted);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   247
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    static char[] decode(byte[] ba, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        String csn = Charset.defaultCharset().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return decode(csn, ba, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            warnUnsupportedCharset(csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return decode("ISO-8859-1", ba, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            // If this code is hit during VM initialization, MessageUtils is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            // the only way we will be able to get any kind of error message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            MessageUtils.err("ISO-8859-1 charset not available: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                             + x.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // If we can not find ISO-8859-1 (a required encoding) then things
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            // are seriously wrong with the installation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    // -- Encoding --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static class StringEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        private Charset cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        private CharsetEncoder ce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        private final String requestedCharsetName;
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   276
        private final boolean isTrusted;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        private StringEncoder(Charset cs, String rcn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            this.requestedCharsetName = rcn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            this.cs = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            this.ce = cs.newEncoder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                .onMalformedInput(CodingErrorAction.REPLACE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                .onUnmappableCharacter(CodingErrorAction.REPLACE);
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   284
            this.isTrusted = (cs.getClass().getClassLoader0() == null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        String charsetName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (cs instanceof HistoricallyNamedCharset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                return ((HistoricallyNamedCharset)cs).historicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return cs.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        final String requestedCharsetName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return requestedCharsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        byte[] encode(char[] ca, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            int en = scale(len, ce.maxBytesPerChar());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            byte[] ba = new byte[en];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                return ba;
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   302
            if (ce instanceof ArrayEncoder) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   303
                int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   304
                return safeTrim(ba, blen, cs, isTrusted);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   305
            } else {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   306
                ce.reset();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   307
                ByteBuffer bb = ByteBuffer.wrap(ba);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   308
                CharBuffer cb = CharBuffer.wrap(ca, off, len);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   309
                try {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   310
                    CoderResult cr = ce.encode(cb, bb, true);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   311
                    if (!cr.isUnderflow())
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   312
                        cr.throwException();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   313
                    cr = ce.flush(bb);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   314
                    if (!cr.isUnderflow())
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   315
                        cr.throwException();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   316
                } catch (CharacterCodingException x) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   317
                    // Substitution is always enabled,
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   318
                    // so this shouldn't happen
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   319
                    throw new Error(x);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   320
                }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   321
                return safeTrim(ba, bb.position(), cs, isTrusted);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    static byte[] encode(String charsetName, char[] ca, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        throws UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    {
41
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
   329
        StringEncoder se = deref(encoder);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if ((se == null) || !(csn.equals(se.requestedCharsetName())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                              || csn.equals(se.charsetName()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            se = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                Charset cs = lookupCharset(csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (cs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    se = new StringEncoder(cs, csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            } catch (IllegalCharsetNameException x) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (se == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                throw new UnsupportedEncodingException (csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            set(encoder, se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return se.encode(ca, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    static byte[] encode(Charset cs, char[] ca, int off, int len) {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   347
        CharsetEncoder ce = cs.newEncoder();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   348
        int en = scale(len, ce.maxBytesPerChar());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   349
        byte[] ba = new byte[en];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   350
        if (len == 0)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   351
            return ba;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   352
        boolean isTrusted = false;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   353
        if (System.getSecurityManager() != null) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   354
            if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   355
                ca =  Arrays.copyOfRange(ca, off, off + len);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   356
                off = 0;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   357
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   358
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   359
        if (ce instanceof ArrayEncoder) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   360
            int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   361
            return safeTrim(ba, blen, cs, isTrusted);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   362
        } else {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   363
            ce.onMalformedInput(CodingErrorAction.REPLACE)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   364
              .onUnmappableCharacter(CodingErrorAction.REPLACE)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   365
              .reset();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   366
            ByteBuffer bb = ByteBuffer.wrap(ba);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   367
            CharBuffer cb = CharBuffer.wrap(ca, off, len);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   368
            try {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   369
                CoderResult cr = ce.encode(cb, bb, true);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   370
                if (!cr.isUnderflow())
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   371
                    cr.throwException();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   372
                cr = ce.flush(bb);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   373
                if (!cr.isUnderflow())
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   374
                    cr.throwException();
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   375
            } catch (CharacterCodingException x) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   376
                throw new Error(x);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   377
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   378
            return safeTrim(ba, bb.position(), cs, isTrusted);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   379
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    static byte[] encode(char[] ca, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        String csn = Charset.defaultCharset().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return encode(csn, ca, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            warnUnsupportedCharset(csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            return encode("ISO-8859-1", ca, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            // If this code is hit during VM initialization, MessageUtils is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            // the only way we will be able to get any kind of error message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            MessageUtils.err("ISO-8859-1 charset not available: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                             + x.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            // If we can not find ISO-8859-1 (a required encoding) then things
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            // are seriously wrong with the installation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
}