jdk/src/share/classes/sun/nio/cs/US_ASCII.java
author sherman
Mon, 22 Jun 2009 19:22:47 -0700
changeset 3055 54b677070c61
parent 2294 4259115772f7
child 3714 6a4eb8f53f91
permissions -rw-r--r--
6847092: (cs) CharsetEncoder.isLegalReplacement of US_ASCII behaves differently since Summary: Updated the US_ASCII and ISO-8859-1 to fix the failure. Reviewed-by: alanb, martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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;
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
    34
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class US_ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    extends Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    implements HistoricallyNamedCharset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public US_ASCII() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        super("US-ASCII", StandardCharsets.aliases_US_ASCII);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public String historicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        return "ASCII";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public boolean contains(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        return (cs instanceof US_ASCII);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public CharsetDecoder newDecoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        return new Decoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public CharsetEncoder newEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return new Encoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
    61
    private static class Decoder extends CharsetDecoder
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
    62
                                 implements ArrayDecoder {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        private Decoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            super(cs, 1.0f, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        private CoderResult decodeArrayLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                            CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            byte[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            char[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    byte b = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    if (b >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        if (dp >= dl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        da[dp++] = (char)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        sp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    return CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        private CoderResult decodeBufferLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                             CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    byte b = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    if (b >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        if (!dst.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        dst.put((char)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        mark++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    return CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        protected CoderResult decodeLoop(ByteBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                         CharBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                return decodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                return decodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   132
        private char repl = '\uFFFD';
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   133
        protected void implReplaceWith(String newReplacement) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   134
            repl = newReplacement.charAt(0);
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
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   137
        public int decode(byte[] src, int sp, int len, char[] dst) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   138
            int dp = 0;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   139
            len = Math.min(len, dst.length);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   140
            while (dp < len) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   141
                byte b = src[sp++];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   142
                if (b >= 0)
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   143
                    dst[dp++] = (char)b;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   144
                else
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   145
                    dst[dp++] = repl;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   146
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   147
            return dp;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   148
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   151
    private static class Encoder extends CharsetEncoder
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   152
                                 implements ArrayEncoder {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        private Encoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            super(cs, 1.0f, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        public boolean canEncode(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return c < 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   162
        public boolean isLegalReplacement(byte[] repl) {
3055
54b677070c61 6847092: (cs) CharsetEncoder.isLegalReplacement of US_ASCII behaves differently since
sherman
parents: 2294
diff changeset
   163
            return (repl.length == 1 && repl[0] >= 0) ||
54b677070c61 6847092: (cs) CharsetEncoder.isLegalReplacement of US_ASCII behaves differently since
sherman
parents: 2294
diff changeset
   164
                   super.isLegalReplacement(repl);
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   165
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   166
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        private final Surrogate.Parser sgp = new Surrogate.Parser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        private CoderResult encodeArrayLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                            ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            char[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            assert (sp <= sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            sp = (sp <= sl ? sp : sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            byte[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            assert (dp <= dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            dp = (dp <= dl ? dp : dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    char c = sa[sp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    if (c < 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        if (dp >= dl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        da[dp] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        sp++; dp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    if (sgp.parse(c, sa, sp, sl) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        private CoderResult encodeBufferLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                             ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    char c = src.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    if (c < 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        if (!dst.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        dst.put((byte)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        mark++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    if (sgp.parse(c, src) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    return sgp.unmappableResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        protected CoderResult encodeLoop(CharBuffer src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                         ByteBuffer dst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (src.hasArray() && dst.hasArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                return encodeArrayLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                return encodeBufferLoop(src, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
2294
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   236
        private byte repl = (byte)'?';
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   237
        protected void implReplaceWith(byte[] newReplacement) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   238
            repl = newReplacement[0];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   239
        }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   240
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   241
        public int encode(char[] src, int sp, int len, byte[] dst) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   242
            int dp = 0;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   243
            int sl = sp + Math.min(len, dst.length);
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   244
            while (sp < sl) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   245
                char c = src[sp++];
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   246
                if (c < 0x80) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   247
                    dst[dp++] = (byte)c;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   248
                    continue;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   249
                }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   250
                if (Surrogate.isHigh(c) && sp < sl &&
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   251
                    Surrogate.isLow(src[sp])) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   252
                    if (len > dst.length) {
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   253
                        sl++;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   254
                        len--;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   255
                    }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   256
                    sp++;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   257
                }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   258
                dst[dp++] = repl;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   259
            }
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   260
            return dp;
4259115772f7 6636323: Optimize handling of builtin charsets
sherman
parents: 2
diff changeset
   261
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
}