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     public ISO_8859_1() {
  43         super("ISO-8859-1", StandardCharsets.aliases_ISO_8859_1());
  44     }
  45 
  46     public String historicalName() {
  47         return "ISO8859_1";
  48     }
  49 
  50     public boolean contains(Charset cs) {
  51         return ((cs instanceof US_ASCII)
  52                 || (cs instanceof ISO_8859_1));
  53     }
  54 
  55     public CharsetDecoder newDecoder() {
  56         return new Decoder(this);
  57     }
  58 
  59     public CharsetEncoder newEncoder() {
  60         return new Encoder(this);
  61     }
  62 
  63     private static class Decoder extends CharsetDecoder {
  64 
  65         private Decoder(Charset cs) {
  66             super(cs, 1.0f, 1.0f);
  67         }
  68 
  69         private CoderResult decodeArrayLoop(ByteBuffer src,
  70                                             CharBuffer dst)
  71         {
  72             byte[] sa = src.array();
  73             int sp = src.arrayOffset() + src.position();
  74             int sl = src.arrayOffset() + src.limit();
  75             assert (sp <= sl);
  76             sp = (sp <= sl ? sp : sl);
  77             char[] da = dst.array();
  78             int dp = dst.arrayOffset() + dst.position();
  79             int dl = dst.arrayOffset() + dst.limit();
  80             assert (dp <= dl);
  81             dp = (dp <= dl ? dp : dl);
  82 
  83             try {
  84                 while (sp < sl) {
  85                     byte b = sa[sp];
  86                     if (dp >= dl)
  87                         return CoderResult.OVERFLOW;
  88                     da[dp++] = (char)(b & 0xff);
  89                     sp++;
  90                 }
  91                 return CoderResult.UNDERFLOW;
  92             } finally {
  93                 src.position(sp - src.arrayOffset());
  94                 dst.position(dp - dst.arrayOffset());
  95             }
  96         }
  97 
  98         private CoderResult decodeBufferLoop(ByteBuffer src,
  99                                              CharBuffer dst)
 100         {
 101             int mark = src.position();
 102             try {
 103                 while (src.hasRemaining()) {
 104                     byte b = src.get();
 105                     if (!dst.hasRemaining())
 106                         return CoderResult.OVERFLOW;
 107                     dst.put((char)(b & 0xff));
 108                     mark++;
 109                 }
 110                 return CoderResult.UNDERFLOW;
 111             } finally {
 112                 src.position(mark);
 113             }
 114         }
 115 
 116         protected CoderResult decodeLoop(ByteBuffer src,
 117                                          CharBuffer dst)
 118         {
 119             if (src.hasArray() && dst.hasArray())
 120                 return decodeArrayLoop(src, dst);
 121             else
 122                 return decodeBufferLoop(src, dst);
 123         }
 124     }
 125 
 126     private static class Encoder extends CharsetEncoder {
 127 
 128         private Encoder(Charset cs) {
 129             super(cs, 1.0f, 1.0f);
 130         }
 131 
 132         public boolean canEncode(char c) {
 133             return c <= '\u00FF';
 134         }
 135 
 136         public boolean isLegalReplacement(byte[] repl) {
 137             return true;  // we accept any byte value
 138         }
 139 
 140         private final Surrogate.Parser sgp = new Surrogate.Parser();
 141 
 142         // Method possible replaced with a compiler intrinsic.
 143         private static int encodeISOArray(char[] sa, int sp,
 144                                           byte[] da, int dp, int len) {
 145             if (len <= 0) {
 146                 return 0;
 147             }
 148             encodeISOArrayCheck(sa, sp, da, dp, len);
 149             return implEncodeISOArray(sa, sp, da, dp, len);
 150         }
 151 
 152         @HotSpotIntrinsicCandidate
 153         private static int implEncodeISOArray(char[] sa, int sp,
 154                                               byte[] da, int dp, int len)
 155         {
 156             int i = 0;
 157             for (; i < len; i++) {
 158                 char c = sa[sp++];
 159                 if (c > '\u00FF')
 160                     break;
 161                 da[dp++] = (byte)c;
 162             }
 163             return i;
 164         }
 165 
 166         private static void encodeISOArrayCheck(char[] sa, int sp,
 167                                                 byte[] da, int dp, int len) {
 168             Objects.requireNonNull(sa);
 169             Objects.requireNonNull(da);
 170 
 171             if (sp < 0 || sp >= sa.length) {
 172                 throw new ArrayIndexOutOfBoundsException(sp);
 173             }
 174 
 175             if (dp < 0 || dp >= da.length) {
 176                 throw new ArrayIndexOutOfBoundsException(dp);
 177             }
 178 
 179             int endIndexSP = sp + len - 1;
 180             if (endIndexSP < 0 || endIndexSP >= sa.length) {
 181                 throw new ArrayIndexOutOfBoundsException(endIndexSP);
 182             }
 183 
 184             int endIndexDP = dp + len - 1;
 185             if (endIndexDP < 0 || endIndexDP >= da.length) {
 186                 throw new ArrayIndexOutOfBoundsException(endIndexDP);
 187             }
 188         }
 189 
 190         private CoderResult encodeArrayLoop(CharBuffer src,
 191                                             ByteBuffer dst)
 192         {
 193             char[] sa = src.array();
 194             int soff = src.arrayOffset();
 195             int sp = soff + src.position();
 196             int sl = soff + src.limit();
 197             assert (sp <= sl);
 198             sp = (sp <= sl ? sp : sl);
 199             byte[] da = dst.array();
 200             int doff = dst.arrayOffset();
 201             int dp = doff + dst.position();
 202             int dl = doff + dst.limit();
 203             assert (dp <= dl);
 204             dp = (dp <= dl ? dp : dl);
 205             int dlen = dl - dp;
 206             int slen = sl - sp;
 207             int len  = (dlen < slen) ? dlen : slen;
 208             try {
 209                 int ret = encodeISOArray(sa, sp, da, dp, len);
 210                 sp = sp + ret;
 211                 dp = dp + ret;
 212                 if (ret != len) {
 213                     if (sgp.parse(sa[sp], sa, sp, sl) < 0)
 214                         return sgp.error();
 215                     return sgp.unmappableResult();
 216                 }
 217                 if (len < slen)
 218                     return CoderResult.OVERFLOW;
 219                 return CoderResult.UNDERFLOW;
 220             } finally {
 221                 src.position(sp - soff);
 222                 dst.position(dp - doff);
 223             }
 224         }
 225 
 226         private CoderResult encodeBufferLoop(CharBuffer src,
 227                                              ByteBuffer dst)
 228         {
 229             int mark = src.position();
 230             try {
 231                 while (src.hasRemaining()) {
 232                     char c = src.get();
 233                     if (c <= '\u00FF') {
 234                         if (!dst.hasRemaining())
 235                             return CoderResult.OVERFLOW;
 236                         dst.put((byte)c);
 237                         mark++;
 238                         continue;
 239                     }
 240                     if (sgp.parse(c, src) < 0)
 241                         return sgp.error();
 242                     return sgp.unmappableResult();
 243                 }
 244                 return CoderResult.UNDERFLOW;
 245             } finally {
 246                 src.position(mark);
 247             }
 248         }
 249 
 250         protected CoderResult encodeLoop(CharBuffer src,
 251                                          ByteBuffer dst)
 252         {
 253             if (src.hasArray() && dst.hasArray())
 254                 return encodeArrayLoop(src, dst);
 255             else
 256                 return encodeBufferLoop(src, dst);
 257         }
 258     }
 259 }
--- EOF ---