jdk/src/java.base/share/classes/java/lang/StringCoding.java
author rriggs
Tue, 16 Feb 2016 11:36:20 -0500
changeset 35978 752d505da547
parent 34885 63d4a8c733f8
child 36929 0dc62e7c6a29
permissions -rw-r--r--
8148775: Spec for j.l.ProcessBuilder.Redirect.DISCARD need to be improved Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
     2
 * Copyright (c) 2000, 2015, 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;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    41
import jdk.internal.HotSpotIntrinsicCandidate;
2
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
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    46
import static java.lang.String.LATIN1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    47
import static java.lang.String.UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    48
import static java.lang.String.COMPACT_STRINGS;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    49
import static java.nio.charset.StandardCharsets.ISO_8859_1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    50
import static java.nio.charset.StandardCharsets.US_ASCII;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    51
import static java.nio.charset.StandardCharsets.UTF_8;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    52
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Utility class for string encoding and decoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
class StringCoding {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private StringCoding() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
41
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
    61
    /** The cached coders for each thread */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25991
diff changeset
    62
    private static final ThreadLocal<SoftReference<StringDecoder>> decoder =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    63
        new ThreadLocal<>();
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25991
diff changeset
    64
    private static final ThreadLocal<SoftReference<StringEncoder>> encoder =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    65
        new ThreadLocal<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static boolean warnUnsupportedCharset = true;
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> T deref(ThreadLocal<SoftReference<T>> tl) {
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
    70
        SoftReference<T> sr = tl.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (sr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return sr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
41
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
    76
    private static <T> void set(ThreadLocal<SoftReference<T>> tl, T ob) {
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 14342
diff changeset
    77
        tl.set(new SoftReference<>(ob));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // Trim the given byte array to the given length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    //
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    82
    private static byte[] safeTrim(byte[] ba, int len, boolean isTrusted) {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
    83
        if (len == ba.length && (isTrusted || System.getSecurityManager() == null))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return ba;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return Arrays.copyOf(ba, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static int scale(int len, float expansionFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // We need to perform double, not float, arithmetic; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // we lose low order bits when len is larger than 2**24.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return (int)(len * (double)expansionFactor);
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 Charset lookupCharset(String csn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (Charset.isSupported(csn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                return Charset.forName(csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            } catch (UnsupportedCharsetException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                throw new Error(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static void warnUnsupportedCharset(String csn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (warnUnsupportedCharset) {
34885
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   108
            // Use err(String) rather than the Logging API or System.err
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   109
            // since this method may be called during VM initialization
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   110
            // before either is available.
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   111
            err("WARNING: Default charset " + csn +
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   112
                " not supported, using ISO-8859-1 instead\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            warnUnsupportedCharset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   117
    static class Result {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   118
        byte[] value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   119
        byte coder;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   120
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   121
        Result with() {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   122
            coder = COMPACT_STRINGS ? LATIN1 : UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   123
            value = new byte[0];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   124
            return this;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   125
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   126
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   127
        Result with(char[] val, int off, int len) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   128
            if (String.COMPACT_STRINGS) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   129
                byte[] bs = StringUTF16.compress(val, off, len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   130
                if (bs != null) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   131
                    value = bs;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   132
                    coder = LATIN1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   133
                    return this;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   134
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   135
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   136
            coder = UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   137
            value = StringUTF16.toBytes(val, off, len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   138
            return this;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   139
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   140
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   141
        Result with(byte[] val, byte coder) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   142
            this.coder = coder;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   143
            value = val;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   144
            return this;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   145
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   146
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   147
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   148
    @HotSpotIntrinsicCandidate
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   149
    private static boolean hasNegatives(byte[] ba, int off, int len) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   150
        for (int i = off; i < off + len; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   151
            if (ba[i] < 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   152
                return true;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   153
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   154
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   155
        return false;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   156
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // -- Decoding --
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   159
    static class StringDecoder {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        private final String requestedCharsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        private final Charset cs;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   162
        private final boolean isASCIICompatible;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        private final CharsetDecoder cd;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   164
        protected final Result result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   166
        StringDecoder(Charset cs, String rcn) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            this.requestedCharsetName = rcn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            this.cs = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            this.cd = cs.newDecoder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                .onMalformedInput(CodingErrorAction.REPLACE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                .onUnmappableCharacter(CodingErrorAction.REPLACE);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   172
            this.result = new Result();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   173
            this.isASCIICompatible = (cd instanceof ArrayDecoder) &&
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   174
                    ((ArrayDecoder)cd).isASCIICompatible();
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
        String charsetName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (cs instanceof HistoricallyNamedCharset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                return ((HistoricallyNamedCharset)cs).historicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return cs.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        final String requestedCharsetName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return requestedCharsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   187
        Result decode(byte[] ba, int off, int len) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   188
            if (len == 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   189
                return result.with();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   190
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   191
            // fastpath for ascii compatible
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   192
            if (isASCIICompatible && !hasNegatives(ba, off, len)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   193
                if (COMPACT_STRINGS) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   194
                    return result.with(Arrays.copyOfRange(ba, off, off + len),
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   195
                                      LATIN1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   196
                } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   197
                    return result.with(StringLatin1.inflate(ba, off, len), UTF16);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   198
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   199
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            int en = scale(len, cd.maxCharsPerByte());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            char[] ca = new char[en];
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   202
            if (cd instanceof ArrayDecoder) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   203
                int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   204
                return result.with(ca, 0, clen);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   205
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   206
            cd.reset();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   207
            ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   208
            CharBuffer cb = CharBuffer.wrap(ca);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   209
            try {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   210
                CoderResult cr = cd.decode(bb, cb, true);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   211
                if (!cr.isUnderflow())
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   212
                    cr.throwException();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   213
                cr = cd.flush(cb);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   214
                if (!cr.isUnderflow())
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   215
                    cr.throwException();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   216
            } catch (CharacterCodingException x) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   217
                // Substitution is always enabled,
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   218
                // so this shouldn't happen
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   219
                throw new Error(x);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   220
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   221
            return result.with(ca, 0, cb.position());
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   222
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   223
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   224
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   225
    private static class StringDecoder8859_1 extends StringDecoder {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   226
        StringDecoder8859_1(Charset cs, String rcn) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   227
            super(cs, rcn);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   228
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   229
        Result decode(byte[] ba, int off, int len) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   230
            if (COMPACT_STRINGS) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   231
                return result.with(Arrays.copyOfRange(ba, off, off + len), LATIN1);
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   232
            } else {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   233
                return result.with(StringLatin1.inflate(ba, off, len), UTF16);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   238
    static Result decode(String charsetName, byte[] ba, int off, int len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        throws UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    {
41
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
   241
        StringDecoder sd = deref(decoder);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if ((sd == null) || !(csn.equals(sd.requestedCharsetName())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                              || csn.equals(sd.charsetName()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            sd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                Charset cs = lookupCharset(csn);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   248
                if (cs != null) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   249
                    if (cs == UTF_8) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   250
                        sd = new StringDecoderUTF8(cs, csn);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   251
                    } else if (cs == ISO_8859_1) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   252
                        sd = new StringDecoder8859_1(cs, csn);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   253
                    } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   254
                        sd = new StringDecoder(cs, csn);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   255
                    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   256
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            } catch (IllegalCharsetNameException x) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            if (sd == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                throw new UnsupportedEncodingException(csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            set(decoder, sd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return sd.decode(ba, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   265
    static Result decode(Charset cs, byte[] ba, int off, int len) {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   266
        // (1)We never cache the "external" cs, the only benefit of creating
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   267
        // an additional StringDe/Encoder object to wrap it is to share the
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 22581
diff changeset
   268
        // de/encode() method. These SD/E objects are short-lived, the young-gen
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 22581
diff changeset
   269
        // gc should be able to take care of them well. But the best approach
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   270
        // is still not to generate them if not really necessary.
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   271
        // (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
   272
        // 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
   273
        // optimization check of (sm==null && classLoader0==null) for both.
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   274
        // (3)getClass().getClassLoader0() is expensive
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   275
        // (4)There might be a timing gap in isTrusted setting. getClassLoader0()
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 22581
diff changeset
   276
        // is only checked (and then isTrusted gets set) when (SM==null). It is
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   277
        // 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
   278
        // when safeTrim() is invoked...the "safe" way to do is to redundant
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   279
        // check (... && (isTrusted || SM == null || getClassLoader0())) in trim
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 22581
diff changeset
   280
        // but it then can be argued that the SM is null when the operation
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   281
        // is started...
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   282
        if (cs == UTF_8) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   283
            return StringDecoderUTF8.decode(ba, off, len, new Result());
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   284
        }
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   285
        CharsetDecoder cd = cs.newDecoder();
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   286
        // ascii fastpath
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   287
        if (cs == ISO_8859_1 || ((cd instanceof ArrayDecoder) &&
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   288
                                 ((ArrayDecoder)cd).isASCIICompatible() &&
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   289
                                 !hasNegatives(ba, off, len))) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   290
             if (COMPACT_STRINGS) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   291
                 return new Result().with(Arrays.copyOfRange(ba, off, off + len),
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   292
                                          LATIN1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   293
             } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   294
                 return new Result().with(StringLatin1.inflate(ba, off, len), UTF16);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   295
             }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   296
        }
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   297
        int en = scale(len, cd.maxCharsPerByte());
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   298
        if (len == 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   299
            return new Result().with();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   300
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   301
        if (System.getSecurityManager() != null &&
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   302
            cs.getClass().getClassLoader0() != null) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   303
            ba =  Arrays.copyOfRange(ba, off, off + len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   304
            off = 0;
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   305
        }
9547
454881baaca0 7040220: java/char_encodin Optimize UTF-8 charset for String.getBytes()/new String(byte[])
sherman
parents: 9035
diff changeset
   306
        cd.onMalformedInput(CodingErrorAction.REPLACE)
454881baaca0 7040220: java/char_encodin Optimize UTF-8 charset for String.getBytes()/new String(byte[])
sherman
parents: 9035
diff changeset
   307
          .onUnmappableCharacter(CodingErrorAction.REPLACE)
454881baaca0 7040220: java/char_encodin Optimize UTF-8 charset for String.getBytes()/new String(byte[])
sherman
parents: 9035
diff changeset
   308
          .reset();
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   309
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   310
        char[] ca = new char[en];
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   311
        if (cd instanceof ArrayDecoder) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   312
            int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   313
            return new Result().with(ca, 0, clen);
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   314
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   315
        ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   316
        CharBuffer cb = CharBuffer.wrap(ca);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   317
        try {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   318
            CoderResult cr = cd.decode(bb, cb, true);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   319
            if (!cr.isUnderflow())
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   320
                cr.throwException();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   321
            cr = cd.flush(cb);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   322
            if (!cr.isUnderflow())
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   323
                cr.throwException();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   324
        } catch (CharacterCodingException x) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   325
            // Substitution is always enabled,
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   326
            // so this shouldn't happen
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   327
            throw new Error(x);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   328
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   329
        return new Result().with(ca, 0, cb.position());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   332
    static Result decode(byte[] ba, int off, int len) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        String csn = Charset.defaultCharset().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        try {
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 9547
diff changeset
   335
            // use charset name decode() variant which provides caching.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return decode(csn, ba, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            warnUnsupportedCharset(csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return decode("ISO-8859-1", ba, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        } catch (UnsupportedEncodingException x) {
34885
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   343
            // If this code is hit during VM initialization, err(String) is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            // the only way we will be able to get any kind of error message.
34885
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   345
            err("ISO-8859-1 charset not available: " + x.toString() + "\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            // If we can not find ISO-8859-1 (a required encoding) then things
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            // are seriously wrong with the installation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    // -- Encoding --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private static class StringEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        private Charset cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        private CharsetEncoder ce;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   357
        private final boolean isASCIICompatible;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        private final String requestedCharsetName;
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   359
        private final boolean isTrusted;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        private StringEncoder(Charset cs, String rcn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            this.requestedCharsetName = rcn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            this.cs = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            this.ce = cs.newEncoder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                .onMalformedInput(CodingErrorAction.REPLACE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                .onUnmappableCharacter(CodingErrorAction.REPLACE);
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   367
            this.isTrusted = (cs.getClass().getClassLoader0() == null);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   368
            this.isASCIICompatible = (ce instanceof ArrayEncoder) &&
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   369
                    ((ArrayEncoder)ce).isASCIICompatible();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        String charsetName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (cs instanceof HistoricallyNamedCharset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                return ((HistoricallyNamedCharset)cs).historicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return cs.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        final String requestedCharsetName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return requestedCharsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   382
        byte[] encode(byte coder, byte[] val) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   383
            // fastpath for ascii compatible
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   384
            if (coder == LATIN1 && isASCIICompatible &&
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   385
                !hasNegatives(val, 0, val.length)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   386
                return Arrays.copyOf(val, val.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   387
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   388
            int len = val.length >> coder;  // assume LATIN1=0/UTF16=1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            int en = scale(len, ce.maxBytesPerChar());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            byte[] ba = new byte[en];
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   391
            if (len == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                return ba;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   393
            }
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   394
            if (ce instanceof ArrayEncoder) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   395
                if (!isTrusted) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   396
                    val = Arrays.copyOf(val, val.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   397
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   398
                int blen = (coder == LATIN1 ) ? ((ArrayEncoder)ce).encodeFromLatin1(val, 0, len, ba)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   399
                                              : ((ArrayEncoder)ce).encodeFromUTF16(val, 0, len, ba);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   400
                if (blen != -1) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   401
                    return safeTrim(ba, blen, isTrusted);
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   402
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   404
            char[] ca = (coder == LATIN1 ) ? StringLatin1.toChars(val)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   405
                                           : StringUTF16.toChars(val);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   406
            ce.reset();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   407
            ByteBuffer bb = ByteBuffer.wrap(ba);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   408
            CharBuffer cb = CharBuffer.wrap(ca, 0, len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   409
            try {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   410
                CoderResult cr = ce.encode(cb, bb, true);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   411
                if (!cr.isUnderflow())
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   412
                    cr.throwException();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   413
                cr = ce.flush(bb);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   414
                if (!cr.isUnderflow())
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   415
                    cr.throwException();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   416
            } catch (CharacterCodingException x) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   417
                // Substitution is always enabled,
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   418
                // so this shouldn't happen
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   419
                throw new Error(x);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   420
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   421
            return safeTrim(ba, bb.position(), isTrusted);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   425
    @HotSpotIntrinsicCandidate
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   426
    private static int implEncodeISOArray(byte[] sa, int sp,
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   427
                                          byte[] da, int dp, int len) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   428
        int i = 0;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   429
        for (; i < len; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   430
            char c = StringUTF16.getChar(sa, sp++);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   431
            if (c > '\u00FF')
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   432
                break;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   433
            da[dp++] = (byte)c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   434
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   435
        return i;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   436
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   437
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   438
    static byte[] encode8859_1(byte coder, byte[] val) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   439
        if (coder == LATIN1) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   440
            return Arrays.copyOf(val, val.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   441
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   442
        int len = val.length >> 1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   443
        byte[] dst = new byte[len];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   444
        int dp = 0;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   445
        int sp = 0;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   446
        int sl = len;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   447
        while (sp < sl) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   448
            int ret = implEncodeISOArray(val, sp, dst, dp, len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   449
            sp = sp + ret;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   450
            dp = dp + ret;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   451
            if (ret != len) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   452
                char c = StringUTF16.getChar(val, sp++);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   453
                if (Character.isHighSurrogate(c) && sp < sl &&
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   454
                    Character.isLowSurrogate(StringUTF16.getChar(val, sp))) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   455
                    sp++;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   456
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   457
                dst[dp++] = '?';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   458
                len = sl - sp;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   459
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   460
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   461
        if (dp == dst.length) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   462
            return dst;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   463
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   464
        return Arrays.copyOf(dst, dp);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   465
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   466
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   467
    static byte[] encodeASCII(byte coder, byte[] val) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   468
        if (coder == LATIN1) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   469
            byte[] dst = new byte[val.length];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   470
            for (int i = 0; i < val.length; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   471
                if (val[i] < 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   472
                    dst[i] = '?';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   473
                } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   474
                    dst[i] = val[i];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   475
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   476
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   477
            return dst;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   478
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   479
        int len = val.length >> 1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   480
        byte[] dst = new byte[len];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   481
        int dp = 0;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   482
        for (int i = 0; i < len; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   483
            char c = StringUTF16.getChar(val, i);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   484
            if (c < 0x80) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   485
                dst[dp++] = (byte)c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   486
                continue;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   487
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   488
            if (Character.isHighSurrogate(c) && i + 1 < len &&
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   489
                Character.isLowSurrogate(StringUTF16.getChar(val, i + 1))) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   490
                i++;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   491
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   492
            dst[dp++] = '?';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   493
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   494
        if (len == dp) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   495
            return dst;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   496
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   497
        return Arrays.copyOf(dst, dp);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   498
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   499
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   500
   static byte[] encodeUTF8(byte coder, byte[] val) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   501
        int dp = 0;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   502
        byte[] dst;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   503
        if (coder == LATIN1) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   504
            dst = new byte[val.length << 1];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   505
            for (int sp = 0; sp < val.length; sp++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   506
                byte c = val[sp];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   507
                if (c < 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   508
                    dst[dp++] = (byte)(0xc0 | ((c & 0xff) >> 6));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   509
                    dst[dp++] = (byte)(0x80 | (c & 0x3f));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   510
                } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   511
                    dst[dp++] = c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   512
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   513
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   514
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   515
            int sp = 0;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   516
            int sl = val.length >> 1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   517
            dst = new byte[sl * 3];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   518
            char c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   519
            while (sp < sl && (c = StringUTF16.getChar(val, sp)) < '\u0080') {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   520
                // ascii fast loop;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   521
                dst[dp++] = (byte)c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   522
                sp++;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   523
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   524
            while (sp < sl) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   525
                c = StringUTF16.getChar(val, sp++);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   526
                if (c < 0x80) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   527
                    dst[dp++] = (byte)c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   528
                } else if (c < 0x800) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   529
                    dst[dp++] = (byte)(0xc0 | (c >> 6));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   530
                    dst[dp++] = (byte)(0x80 | (c & 0x3f));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   531
                } else if (Character.isSurrogate(c)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   532
                    int uc = -1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   533
                    char c2;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   534
                    if (Character.isHighSurrogate(c) && sp < sl &&
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   535
                        Character.isLowSurrogate(c2 = StringUTF16.getChar(val, sp))) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   536
                        uc = Character.toCodePoint(c, c2);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   537
                    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   538
                    if (uc < 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   539
                        dst[dp++] = '?';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   540
                    } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   541
                        dst[dp++] = (byte)(0xf0 | ((uc >> 18)));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   542
                        dst[dp++] = (byte)(0x80 | ((uc >> 12) & 0x3f));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   543
                        dst[dp++] = (byte)(0x80 | ((uc >>  6) & 0x3f));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   544
                        dst[dp++] = (byte)(0x80 | (uc & 0x3f));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   545
                        sp++;  // 2 chars
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   546
                    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   547
                } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   548
                    // 3 bytes, 16 bits
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   549
                    dst[dp++] = (byte)(0xe0 | ((c >> 12)));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   550
                    dst[dp++] = (byte)(0x80 | ((c >>  6) & 0x3f));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   551
                    dst[dp++] = (byte)(0x80 | (c & 0x3f));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   552
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   553
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   554
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   555
        if (dp == dst.length) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   556
            return dst;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   557
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   558
        return Arrays.copyOf(dst, dp);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   559
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   560
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   561
    static byte[] encode(String charsetName, byte coder, byte[] val)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        throws UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    {
41
dfebd2609e77 6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents: 2
diff changeset
   564
        StringEncoder se = deref(encoder);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if ((se == null) || !(csn.equals(se.requestedCharsetName())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                              || csn.equals(se.charsetName()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            se = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                Charset cs = lookupCharset(csn);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   571
                if (cs != null) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   572
                    if (cs == UTF_8) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   573
                        return encodeUTF8(coder, val);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   574
                    } else if (cs == ISO_8859_1) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   575
                        return encode8859_1(coder, val);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   576
                    } else if (cs == US_ASCII) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   577
                        return encodeASCII(coder, val);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   578
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    se = new StringEncoder(cs, csn);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   580
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            } catch (IllegalCharsetNameException x) {}
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   582
            if (se == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                throw new UnsupportedEncodingException (csn);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   584
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            set(encoder, se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   587
        return se.encode(coder, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   590
    static byte[] encode(Charset cs, byte coder, byte[] val) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   591
        if (cs == UTF_8) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   592
            return encodeUTF8(coder, val);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   593
        } else if (cs == ISO_8859_1) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   594
            return encode8859_1(coder, val);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   595
        } else if (cs == US_ASCII) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   596
            return encodeASCII(coder, val);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   597
        }
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   598
        CharsetEncoder ce = cs.newEncoder();
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   599
        // fastpath for ascii compatible
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   600
        if (coder == LATIN1 && (((ce instanceof ArrayEncoder) &&
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   601
                                 ((ArrayEncoder)ce).isASCIICompatible() &&
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   602
                                 !hasNegatives(val, 0, val.length)))) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   603
            return Arrays.copyOf(val, val.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   604
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   605
        int len = val.length >> coder;  // assume LATIN1=0/UTF16=1;
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   606
        int en = scale(len, ce.maxBytesPerChar());
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   607
        byte[] ba = new byte[en];
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   608
        if (len == 0) {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   609
            return ba;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   610
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   611
        boolean isTrusted = System.getSecurityManager() == null ||
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   612
                            cs.getClass().getClassLoader0() == null;
9547
454881baaca0 7040220: java/char_encodin Optimize UTF-8 charset for String.getBytes()/new String(byte[])
sherman
parents: 9035
diff changeset
   613
        ce.onMalformedInput(CodingErrorAction.REPLACE)
454881baaca0 7040220: java/char_encodin Optimize UTF-8 charset for String.getBytes()/new String(byte[])
sherman
parents: 9035
diff changeset
   614
          .onUnmappableCharacter(CodingErrorAction.REPLACE)
454881baaca0 7040220: java/char_encodin Optimize UTF-8 charset for String.getBytes()/new String(byte[])
sherman
parents: 9035
diff changeset
   615
          .reset();
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   616
        if (ce instanceof ArrayEncoder) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   617
            if (!isTrusted) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   618
                val = Arrays.copyOf(val, val.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   619
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   620
            int blen = (coder == LATIN1 ) ? ((ArrayEncoder)ce).encodeFromLatin1(val, 0, len, ba)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   621
                                          : ((ArrayEncoder)ce).encodeFromUTF16(val, 0, len, ba);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   622
            if (blen != -1) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   623
                return safeTrim(ba, blen, isTrusted);
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   624
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 715
diff changeset
   625
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   626
        char[] ca = (coder == LATIN1 ) ? StringLatin1.toChars(val)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   627
                                       : StringUTF16.toChars(val);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   628
        ByteBuffer bb = ByteBuffer.wrap(ba);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   629
        CharBuffer cb = CharBuffer.wrap(ca, 0, len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   630
        try {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   631
            CoderResult cr = ce.encode(cb, bb, true);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   632
            if (!cr.isUnderflow())
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   633
                cr.throwException();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   634
            cr = ce.flush(bb);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   635
            if (!cr.isUnderflow())
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   636
                cr.throwException();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   637
        } catch (CharacterCodingException x) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   638
            throw new Error(x);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   639
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   640
        return safeTrim(ba, bb.position(), isTrusted);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   643
    static byte[] encode(byte coder, byte[] val) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        String csn = Charset.defaultCharset().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        try {
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 9547
diff changeset
   646
            // use charset name encode() variant which provides caching.
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   647
            return encode(csn, coder, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            warnUnsupportedCharset(csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        try {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   652
            return encode("ISO-8859-1", coder, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        } catch (UnsupportedEncodingException x) {
34885
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   654
            // If this code is hit during VM initialization, err(String) is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            // the only way we will be able to get any kind of error message.
34885
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   656
            err("ISO-8859-1 charset not available: " + x.toString() + "\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            // If we can not find ISO-8859-1 (a required encoding) then things
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            // are seriously wrong with the installation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
34885
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   663
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   664
    /**
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   665
     *  Print a message directly to stderr, bypassing all character conversion
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   666
     *  methods.
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   667
     *  @param msg  message to print
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   668
     */
63d4a8c733f8 8146484: Examine sun.misc.MessageUtils
chegar
parents: 33663
diff changeset
   669
    private static native void err(String msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
}