src/java.base/share/classes/sun/nio/cs/SingleByte.java
author aleonard
Thu, 10 Oct 2019 10:28:55 +0100
changeset 58561 3968bf3673c5
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231717: Improve performance of charset decoding when charset is always compactable Reviewed-by: rriggs, redestad, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
     1
/*
58561
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
     2
 * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
     4
 *
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
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: 3714
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    10
 *
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    15
 * accompanied this code).
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    16
 *
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
diff changeset
    23
 * questions.
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    24
 */
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    25
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    26
package sun.nio.cs;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    27
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    28
import java.nio.Buffer;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    29
import java.nio.ByteBuffer;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    30
import java.nio.CharBuffer;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    31
import java.nio.charset.Charset;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    32
import java.nio.charset.CharsetDecoder;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    33
import java.nio.charset.CharsetEncoder;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    34
import java.nio.charset.CoderResult;
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
    35
import java.util.Arrays;
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    36
import static sun.nio.cs.CharsetMapping.*;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    37
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    38
public class SingleByte
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    39
{
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    40
    private static final CoderResult withResult(CoderResult cr,
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    41
                                                Buffer src, int sp,
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    42
                                                Buffer dst, int dp)
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    43
    {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    44
        src.position(sp - src.arrayOffset());
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    45
        dst.position(dp - dst.arrayOffset());
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    46
        return cr;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    47
    }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    48
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 28850
diff changeset
    49
    public static final class Decoder extends CharsetDecoder
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
    50
                                      implements ArrayDecoder {
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    51
        private final char[] b2c;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    52
        private final boolean isASCIICompatible;
58561
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
    53
        private final boolean isLatin1Decodable;
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    54
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    55
        public Decoder(Charset cs, char[] b2c) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    56
            super(cs, 1.0f, 1.0f);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    57
            this.b2c = b2c;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    58
            this.isASCIICompatible = false;
58561
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
    59
            this.isLatin1Decodable = false;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    60
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    61
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    62
        public Decoder(Charset cs, char[] b2c, boolean isASCIICompatible) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    63
            super(cs, 1.0f, 1.0f);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    64
            this.b2c = b2c;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    65
            this.isASCIICompatible = isASCIICompatible;
58561
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
    66
            this.isLatin1Decodable = false;
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
    67
        }
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
    68
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
    69
        public Decoder(Charset cs, char[] b2c, boolean isASCIICompatible, boolean isLatin1Decodable) {
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
    70
            super(cs, 1.0f, 1.0f);
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
    71
            this.b2c = b2c;
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
    72
            this.isASCIICompatible = isASCIICompatible;
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
    73
            this.isLatin1Decodable = isLatin1Decodable;
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    74
        }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    75
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    76
        private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    77
            byte[] sa = src.array();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    78
            int sp = src.arrayOffset() + src.position();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    79
            int sl = src.arrayOffset() + src.limit();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    80
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    81
            char[] da = dst.array();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    82
            int dp = dst.arrayOffset() + dst.position();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    83
            int dl = dst.arrayOffset() + dst.limit();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    84
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    85
            CoderResult cr = CoderResult.UNDERFLOW;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    86
            if ((dl - dp) < (sl - sp)) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    87
                sl = sp + (dl - dp);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    88
                cr = CoderResult.OVERFLOW;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    89
            }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    90
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    91
            while (sp < sl) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    92
                char c = decode(sa[sp]);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    93
                if (c == UNMAPPABLE_DECODING) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    94
                    return withResult(CoderResult.unmappableForLength(1),
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    95
                               src, sp, dst, dp);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    96
                }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    97
                da[dp++] = c;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    98
                sp++;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
    99
            }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   100
            return withResult(cr, src, sp, dst, dp);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   101
        }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   102
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   103
        private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   104
            int mark = src.position();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   105
            try {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   106
                while (src.hasRemaining()) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   107
                    char c = decode(src.get());
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   108
                    if (c == UNMAPPABLE_DECODING)
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   109
                        return CoderResult.unmappableForLength(1);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   110
                    if (!dst.hasRemaining())
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   111
                        return CoderResult.OVERFLOW;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   112
                    dst.put(c);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   113
                    mark++;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   114
                }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   115
                return CoderResult.UNDERFLOW;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   116
            } finally {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   117
                src.position(mark);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   118
            }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   119
        }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   120
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   121
        protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   122
            if (src.hasArray() && dst.hasArray())
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   123
                return decodeArrayLoop(src, dst);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   124
            else
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   125
                return decodeBufferLoop(src, dst);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   126
        }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   127
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   128
        public final char decode(int b) {
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   129
            return b2c[b + 128];
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   130
        }
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   131
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   132
        private char repl = '\uFFFD';
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   133
        protected void implReplaceWith(String newReplacement) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   134
            repl = newReplacement.charAt(0);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   135
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   136
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   137
        @Override
58561
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   138
        public int decodeToLatin1(byte[] src, int sp, int len, byte[] dst) {
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   139
            if (len > dst.length)
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   140
                len = dst.length;
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   141
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   142
            int dp = 0;
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   143
            while (dp < len) {
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   144
                dst[dp++] = (byte)decode(src[sp++]);
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   145
            }
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   146
            return dp;
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   147
        }
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   148
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   149
        @Override
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   150
        public int decode(byte[] src, int sp, int len, char[] dst) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   151
            if (len > dst.length)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   152
                len = dst.length;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   153
            int dp = 0;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   154
            while (dp < len) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   155
                dst[dp] = decode(src[sp++]);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   156
                if (dst[dp] == UNMAPPABLE_DECODING) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   157
                    dst[dp] = repl;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   158
                }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   159
                dp++;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   160
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   161
            return dp;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   162
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   163
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   164
        @Override
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   165
        public boolean isASCIICompatible() {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   166
            return isASCIICompatible;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   167
        }
58561
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   168
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   169
        @Override
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   170
        public boolean isLatin1Decodable() {
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   171
            return isLatin1Decodable;
3968bf3673c5 8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents: 47216
diff changeset
   172
        }
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   173
    }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   174
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 28850
diff changeset
   175
    public static final class Encoder extends CharsetEncoder
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   176
                                      implements ArrayEncoder {
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   177
        private Surrogate.Parser sgp;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   178
        private final char[] c2b;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   179
        private final char[] c2bIndex;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   180
        private final boolean isASCIICompatible;
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   181
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   182
        public Encoder(Charset cs, char[] c2b, char[] c2bIndex, boolean isASCIICompatible) {
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   183
            super(cs, 1.0f, 1.0f);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   184
            this.c2b = c2b;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   185
            this.c2bIndex = c2bIndex;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   186
            this.isASCIICompatible = isASCIICompatible;
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   187
        }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   188
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   189
        public boolean canEncode(char c) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   190
            return encode(c) != UNMAPPABLE_ENCODING;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   191
        }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   192
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   193
        public boolean isLegalReplacement(byte[] repl) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   194
            return ((repl.length == 1 && repl[0] == (byte)'?') ||
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   195
                    super.isLegalReplacement(repl));
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   196
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   197
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   198
        private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   199
            char[] sa = src.array();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   200
            int sp = src.arrayOffset() + src.position();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   201
            int sl = src.arrayOffset() + src.limit();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   202
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   203
            byte[] da = dst.array();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   204
            int dp = dst.arrayOffset() + dst.position();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   205
            int dl = dst.arrayOffset() + dst.limit();
28850
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   206
            int len  = Math.min(dl - dp, sl - sp);
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   207
28850
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   208
            while (len-- > 0) {
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   209
                char c = sa[sp];
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   210
                int b = encode(c);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   211
                if (b == UNMAPPABLE_ENCODING) {
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2294
diff changeset
   212
                    if (Character.isSurrogate(c)) {
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   213
                        if (sgp == null)
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   214
                            sgp = new Surrogate.Parser();
28850
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   215
                        if (sgp.parse(c, sa, sp, sl) < 0) {
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   216
                            return withResult(sgp.error(), src, sp, dst, dp);
28850
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   217
                        }
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   218
                        return withResult(sgp.unmappableResult(), src, sp, dst, dp);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   219
                    }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   220
                    return withResult(CoderResult.unmappableForLength(1),
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   221
                               src, sp, dst, dp);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   222
                }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   223
                da[dp++] = (byte)b;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   224
                sp++;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   225
            }
28850
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   226
            return withResult(sp < sl ? CoderResult.OVERFLOW : CoderResult.UNDERFLOW,
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   227
                              src, sp, dst, dp);
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   228
        }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   229
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   230
        private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   231
            int mark = src.position();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   232
            try {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   233
                while (src.hasRemaining()) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   234
                    char c = src.get();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   235
                    int b = encode(c);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   236
                    if (b == UNMAPPABLE_ENCODING) {
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2294
diff changeset
   237
                        if (Character.isSurrogate(c)) {
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   238
                            if (sgp == null)
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   239
                                sgp = new Surrogate.Parser();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   240
                            if (sgp.parse(c, src) < 0)
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   241
                                return sgp.error();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   242
                            return sgp.unmappableResult();
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   243
                        }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   244
                        return CoderResult.unmappableForLength(1);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   245
                    }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   246
                    if (!dst.hasRemaining())
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   247
                        return CoderResult.OVERFLOW;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   248
                    dst.put((byte)b);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   249
                    mark++;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   250
                }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   251
                return CoderResult.UNDERFLOW;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   252
            } finally {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   253
                src.position(mark);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   254
            }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   255
        }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   256
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   257
        protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   258
            if (src.hasArray() && dst.hasArray())
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   259
                return encodeArrayLoop(src, dst);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   260
            else
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   261
                return encodeBufferLoop(src, dst);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   262
        }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   263
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
   264
        public final int encode(char ch) {
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   265
            char index = c2bIndex[ch >> 8];
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   266
            if (index == UNMAPPABLE_ENCODING)
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   267
                return UNMAPPABLE_ENCODING;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   268
            return c2b[index + (ch & 0xff)];
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   269
        }
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   270
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   271
        private byte repl = (byte)'?';
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   272
        protected void implReplaceWith(byte[] newReplacement) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   273
            repl = newReplacement[0];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   274
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   275
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   276
        public int encode(char[] src, int sp, int len, byte[] dst) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   277
            int dp = 0;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   278
            int sl = sp + Math.min(len, dst.length);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   279
            while (sp < sl) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   280
                char c = src[sp++];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   281
                int b = encode(c);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   282
                if (b != UNMAPPABLE_ENCODING) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   283
                    dst[dp++] = (byte)b;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   284
                    continue;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   285
                }
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2294
diff changeset
   286
                if (Character.isHighSurrogate(c) && sp < sl &&
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2294
diff changeset
   287
                    Character.isLowSurrogate(src[sp])) {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   288
                    if (len > dst.length) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   289
                        sl++;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   290
                        len--;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   291
                    }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   292
                    sp++;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   293
                }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   294
                dst[dp++] = repl;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   295
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   296
            return dp;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 1713
diff changeset
   297
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   298
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   299
        @Override
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   300
        public int encodeFromLatin1(byte[] src, int sp, int len, byte[] dst) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   301
            int dp = 0;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   302
            int sl = sp + Math.min(len, dst.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   303
            while (sp < sl) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   304
                char c = (char)(src[sp++] & 0xff);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   305
                int b = encode(c);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   306
                if (b == UNMAPPABLE_ENCODING) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   307
                    dst[dp++] = repl;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   308
                } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   309
                    dst[dp++] = (byte)b;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   310
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   311
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   312
            return dp;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   313
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   314
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   315
        @Override
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   316
        public int encodeFromUTF16(byte[] src, int sp, int len, byte[] dst) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   317
            int dp = 0;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   318
            int sl = sp + Math.min(len, dst.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   319
            while (sp < sl) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   320
                char c = StringUTF16.getChar(src, sp++);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   321
                int b = encode(c);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   322
                if (b != UNMAPPABLE_ENCODING) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   323
                    dst[dp++] = (byte)b;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   324
                    continue;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   325
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   326
                if (Character.isHighSurrogate(c) && sp < sl &&
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   327
                    Character.isLowSurrogate(StringUTF16.getChar(src, sp))) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   328
                    if (len > dst.length) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   329
                        sl++;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   330
                        len--;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   331
                    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   332
                    sp++;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   333
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   334
                dst[dp++] = repl;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   335
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   336
            return dp;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   337
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   338
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   339
        @Override
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   340
        public boolean isASCIICompatible() {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   341
            return isASCIICompatible;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   342
        }
1713
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   343
    }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   344
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   345
    // init the c2b and c2bIndex tables from b2c.
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   346
    public static void initC2B(char[] b2c, char[] c2bNR,
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   347
                               char[] c2b, char[] c2bIndex) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   348
        for (int i = 0; i < c2bIndex.length; i++)
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   349
            c2bIndex[i] = UNMAPPABLE_ENCODING;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   350
        for (int i = 0; i < c2b.length; i++)
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   351
            c2b[i] = UNMAPPABLE_ENCODING;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   352
        int off = 0;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   353
        for (int i = 0; i < b2c.length; i++) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   354
            char c = b2c[i];
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   355
            if (c == UNMAPPABLE_DECODING)
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   356
                continue;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   357
            int index = (c >> 8);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   358
            if (c2bIndex[index] == UNMAPPABLE_ENCODING) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   359
                c2bIndex[index] = (char)off;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   360
                off += 0x100;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   361
            }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   362
            index = c2bIndex[index] + (c & 0xff);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   363
            c2b[index] = (char)((i>=0x80)?(i-0x80):(i+0x80));
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   364
        }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   365
        if (c2bNR != null) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   366
            // c-->b nr entries
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   367
            int i = 0;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   368
            while (i < c2bNR.length) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   369
                char b = c2bNR[i++];
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   370
                char c = c2bNR[i++];
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   371
                int index = (c >> 8);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   372
                if (c2bIndex[index] == UNMAPPABLE_ENCODING) {
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   373
                    c2bIndex[index] = (char)off;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   374
                    off += 0x100;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   375
                }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   376
                index = c2bIndex[index] + (c & 0xff);
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   377
                c2b[index] = b;
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   378
            }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   379
        }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   380
    }
f8b8bfa64fc1 6642323: Speeding up Single Byte Decoders
sherman
parents:
diff changeset
   381
}