8200310: Avoid charset lookup machinery in java.nio.charset.StandardCharsets
Reviewed-by: sherman

   1 /*
   2  * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.cs;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.nio.CharBuffer;
  30 import java.nio.charset.Charset;
  31 import java.nio.charset.CharsetDecoder;
  32 import java.nio.charset.CharsetEncoder;
  33 import java.nio.charset.CoderResult;
  34 import java.util.Objects;
  35 
  36 import jdk.internal.HotSpotIntrinsicCandidate;
  37 
  38 public class ISO_8859_1
  39     extends Charset
  40     implements HistoricallyNamedCharset
  41 {
  42 
  43     public static final ISO_8859_1 INSTANCE = new ISO_8859_1();
  44 
  45     public ISO_8859_1() {
  46         super("ISO-8859-1", StandardCharsets.aliases_ISO_8859_1());
  47     }
  48 
  49     public String historicalName() {
  50         return "ISO8859_1";
  51     }
  52 
  53     public boolean contains(Charset cs) {
  54         return ((cs instanceof US_ASCII)
  55                 || (cs instanceof ISO_8859_1));
  56     }
  57 
  58     public CharsetDecoder newDecoder() {
  59         return new Decoder(this);
  60     }
  61 
  62     public CharsetEncoder newEncoder() {
  63         return new Encoder(this);
  64     }
  65 
  66     private static class Decoder extends CharsetDecoder {
  67 
  68         private Decoder(Charset cs) {
  69             super(cs, 1.0f, 1.0f);
  70         }
  71 
  72         private CoderResult decodeArrayLoop(ByteBuffer src,
  73                                             CharBuffer dst)
  74         {
  75             byte[] sa = src.array();
  76             int sp = src.arrayOffset() + src.position();
  77             int sl = src.arrayOffset() + src.limit();
  78             assert (sp <= sl);
  79             sp = (sp <= sl ? sp : sl);
  80             char[] da = dst.array();
  81             int dp = dst.arrayOffset() + dst.position();
  82             int dl = dst.arrayOffset() + dst.limit();
  83             assert (dp <= dl);
  84             dp = (dp <= dl ? dp : dl);
  85 
  86             try {
  87                 while (sp < sl) {
  88                     byte b = sa[sp];
  89                     if (dp >= dl)
  90                         return CoderResult.OVERFLOW;
  91                     da[dp++] = (char)(b & 0xff);
  92                     sp++;
  93                 }
  94                 return CoderResult.UNDERFLOW;
  95             } finally {
  96                 src.position(sp - src.arrayOffset());
  97                 dst.position(dp - dst.arrayOffset());
  98             }
  99         }
 100 
 101         private CoderResult decodeBufferLoop(ByteBuffer src,
 102                                              CharBuffer dst)
 103         {
 104             int mark = src.position();
 105             try {
 106                 while (src.hasRemaining()) {
 107                     byte b = src.get();
 108                     if (!dst.hasRemaining())
 109                         return CoderResult.OVERFLOW;
 110                     dst.put((char)(b & 0xff));
 111                     mark++;
 112                 }
 113                 return CoderResult.UNDERFLOW;
 114             } finally {
 115                 src.position(mark);
 116             }
 117         }
 118 
 119         protected CoderResult decodeLoop(ByteBuffer src,
 120                                          CharBuffer dst)
 121         {
 122             if (src.hasArray() && dst.hasArray())
 123                 return decodeArrayLoop(src, dst);
 124             else
 125                 return decodeBufferLoop(src, dst);
 126         }
 127     }
 128 
 129     private static class Encoder extends CharsetEncoder {
 130 
 131         private Encoder(Charset cs) {
 132             super(cs, 1.0f, 1.0f);
 133         }
 134 
 135         public boolean canEncode(char c) {
 136             return c <= '\u00FF';
 137         }
 138 
 139         public boolean isLegalReplacement(byte[] repl) {
 140             return true;  // we accept any byte value
 141         }
 142 
 143         private final Surrogate.Parser sgp = new Surrogate.Parser();
 144 
 145         // Method possible replaced with a compiler intrinsic.
 146         private static int encodeISOArray(char[] sa, int sp,
 147                                           byte[] da, int dp, int len) {
 148             if (len <= 0) {
 149                 return 0;
 150             }
 151             encodeISOArrayCheck(sa, sp, da, dp, len);
 152             return implEncodeISOArray(sa, sp, da, dp, len);
 153         }
 154 
 155         @HotSpotIntrinsicCandidate
 156         private static int implEncodeISOArray(char[] sa, int sp,
 157                                               byte[] da, int dp, int len)
 158         {
 159             int i = 0;
 160             for (; i < len; i++) {
 161                 char c = sa[sp++];
 162                 if (c > '\u00FF')
 163                     break;
 164                 da[dp++] = (byte)c;
 165             }
 166             return i;
 167         }
 168 
 169         private static void encodeISOArrayCheck(char[] sa, int sp,
 170                                                 byte[] da, int dp, int len) {
 171             Objects.requireNonNull(sa);
 172             Objects.requireNonNull(da);
 173 
 174             if (sp < 0 || sp >= sa.length) {
 175                 throw new ArrayIndexOutOfBoundsException(sp);
 176             }
 177 
 178             if (dp < 0 || dp >= da.length) {
 179                 throw new ArrayIndexOutOfBoundsException(dp);
 180             }
 181 
 182             int endIndexSP = sp + len - 1;
 183             if (endIndexSP < 0 || endIndexSP >= sa.length) {
 184                 throw new ArrayIndexOutOfBoundsException(endIndexSP);
 185             }
 186 
 187             int endIndexDP = dp + len - 1;
 188             if (endIndexDP < 0 || endIndexDP >= da.length) {
 189                 throw new ArrayIndexOutOfBoundsException(endIndexDP);
 190             }
 191         }
 192 
 193         private CoderResult encodeArrayLoop(CharBuffer src,
 194                                             ByteBuffer dst)
 195         {
 196             char[] sa = src.array();
 197             int soff = src.arrayOffset();
 198             int sp = soff + src.position();
 199             int sl = soff + src.limit();
 200             assert (sp <= sl);
 201             sp = (sp <= sl ? sp : sl);
 202             byte[] da = dst.array();
 203             int doff = dst.arrayOffset();
 204             int dp = doff + dst.position();
 205             int dl = doff + dst.limit();
 206             assert (dp <= dl);
 207             dp = (dp <= dl ? dp : dl);
 208             int dlen = dl - dp;
 209             int slen = sl - sp;
 210             int len  = (dlen < slen) ? dlen : slen;
 211             try {
 212                 int ret = encodeISOArray(sa, sp, da, dp, len);
 213                 sp = sp + ret;
 214                 dp = dp + ret;
 215                 if (ret != len) {
 216                     if (sgp.parse(sa[sp], sa, sp, sl) < 0)
 217                         return sgp.error();
 218                     return sgp.unmappableResult();
 219                 }
 220                 if (len < slen)
 221                     return CoderResult.OVERFLOW;
 222                 return CoderResult.UNDERFLOW;
 223             } finally {
 224                 src.position(sp - soff);
 225                 dst.position(dp - doff);
 226             }
 227         }
 228 
 229         private CoderResult encodeBufferLoop(CharBuffer src,
 230                                              ByteBuffer dst)
 231         {
 232             int mark = src.position();
 233             try {
 234                 while (src.hasRemaining()) {
 235                     char c = src.get();
 236                     if (c <= '\u00FF') {
 237                         if (!dst.hasRemaining())
 238                             return CoderResult.OVERFLOW;
 239                         dst.put((byte)c);
 240                         mark++;
 241                         continue;
 242                     }
 243                     if (sgp.parse(c, src) < 0)
 244                         return sgp.error();
 245                     return sgp.unmappableResult();
 246                 }
 247                 return CoderResult.UNDERFLOW;
 248             } finally {
 249                 src.position(mark);
 250             }
 251         }
 252 
 253         protected CoderResult encodeLoop(CharBuffer src,
 254                                          ByteBuffer dst)
 255         {
 256             if (src.hasArray() && dst.hasArray())
 257                 return encodeArrayLoop(src, dst);
 258             else
 259                 return encodeBufferLoop(src, dst);
 260         }
 261     }
 262 }
--- EOF ---