jdk/src/java.base/share/classes/sun/nio/cs/US_ASCII.java
author redestad
Wed, 19 Jul 2017 14:40:50 +0200
changeset 45894 995421c69f66
parent 43790 b9e56c7fba7e
child 47026 94c45ad89b9c
permissions -rw-r--r--
8184665: Skip name and alias checks for standard Charsets Reviewed-by: sherman, rriggs, forax
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45894
995421c69f66 8184665: Skip name and alias checks for standard Charsets
redestad
parents: 43790
diff changeset
     2
 * Copyright (c) 2000, 2017, 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: 3714
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: 3714
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: 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.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.charset.CharsetDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.charset.CoderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class US_ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    extends Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    implements HistoricallyNamedCharset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public US_ASCII() {
45894
995421c69f66 8184665: Skip name and alias checks for standard Charsets
redestad
parents: 43790
diff changeset
    41
        super(StandardCharsets.US_ASCII, StandardCharsets.aliases_US_ASCII);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public String historicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        return "ASCII";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public boolean contains(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return (cs instanceof US_ASCII);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public CharsetDecoder newDecoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return new Decoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public CharsetEncoder newEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return new Encoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
    60
    private static class Decoder extends CharsetDecoder
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
    61
                                 implements ArrayDecoder {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        private Decoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            super(cs, 1.0f, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        private CoderResult decodeArrayLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                            CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            byte[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            char[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    byte b = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    if (b >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        if (dp >= dl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        da[dp++] = (char)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        sp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    return CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        private CoderResult decodeBufferLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                             CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    byte b = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    if (b >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        if (!dst.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        dst.put((char)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        mark++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    return CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        protected CoderResult decodeLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                         CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                return decodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return decodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   131
        private char repl = '\uFFFD';
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   132
        protected void implReplaceWith(String newReplacement) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   133
            repl = newReplacement.charAt(0);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   134
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   135
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   136
        public int decode(byte[] src, int sp, int len, char[] dst) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   137
            int dp = 0;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   138
            len = Math.min(len, dst.length);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   139
            while (dp < len) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   140
                byte b = src[sp++];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   141
                if (b >= 0)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   142
                    dst[dp++] = (char)b;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   143
                else
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   144
                    dst[dp++] = repl;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   145
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   146
            return dp;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   147
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25859
diff changeset
   148
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25859
diff changeset
   149
        public boolean isASCIICompatible() {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25859
diff changeset
   150
            return true;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25859
diff changeset
   151
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   154
    private static class Encoder extends CharsetEncoder
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   155
                                 implements ArrayEncoder {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        private Encoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            super(cs, 1.0f, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        public boolean canEncode(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return c < 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   165
        public boolean isLegalReplacement(byte[] repl) {
3055
54b677070c61 6847092: (cs) CharsetEncoder.isLegalReplacement of US_ASCII behaves differently since
sherman
parents: 2294
diff changeset
   166
            return (repl.length == 1 && repl[0] >= 0) ||
54b677070c61 6847092: (cs) CharsetEncoder.isLegalReplacement of US_ASCII behaves differently since
sherman
parents: 2294
diff changeset
   167
                   super.isLegalReplacement(repl);
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   168
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   169
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        private final Surrogate.Parser sgp = new Surrogate.Parser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        private CoderResult encodeArrayLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                            ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            char[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            byte[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    char c = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    if (c < 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        if (dp >= dl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        da[dp] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        sp++; dp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    if (sgp.parse(c, sa, sp, sl) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        private CoderResult encodeBufferLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                             ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    char c = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    if (c < 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        if (!dst.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        dst.put((byte)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        mark++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    if (sgp.parse(c, src) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        protected CoderResult encodeLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                         ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                return encodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                return encodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   239
        private byte repl = (byte)'?';
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   240
        protected void implReplaceWith(byte[] newReplacement) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   241
            repl = newReplacement[0];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   242
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   243
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   244
        public int encode(char[] src, int sp, int len, byte[] dst) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   245
            int dp = 0;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   246
            int sl = sp + Math.min(len, dst.length);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   247
            while (sp < sl) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   248
                char c = src[sp++];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   249
                if (c < 0x80) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   250
                    dst[dp++] = (byte)c;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   251
                    continue;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   252
                }
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3055
diff changeset
   253
                if (Character.isHighSurrogate(c) && sp < sl &&
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3055
diff changeset
   254
                    Character.isLowSurrogate(src[sp])) {
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   255
                    if (len > dst.length) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   256
                        sl++;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   257
                        len--;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   258
                    }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   259
                    sp++;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   260
                }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   261
                dst[dp++] = repl;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   262
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   263
            return dp;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   264
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25859
diff changeset
   265
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25859
diff changeset
   266
        public boolean isASCIICompatible() {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25859
diff changeset
   267
            return true;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 25859
diff changeset
   268
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
}