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 
  35 public class US_ASCII
  36     extends Charset
  37     implements HistoricallyNamedCharset
  38 {
  39     public static final US_ASCII INSTANCE = new US_ASCII();
  40 
  41     public US_ASCII() {
  42         super("US-ASCII", StandardCharsets.aliases_US_ASCII());
  43     }
  44 
  45     public String historicalName() {
  46         return "ASCII";
  47     }
  48 
  49     public boolean contains(Charset cs) {
  50         return (cs instanceof US_ASCII);
  51     }
  52 
  53     public CharsetDecoder newDecoder() {
  54         return new Decoder(this);
  55     }
  56 
  57     public CharsetEncoder newEncoder() {
  58         return new Encoder(this);
  59     }
  60 
  61     private static class Decoder extends CharsetDecoder {
  62 
  63         private Decoder(Charset cs) {
  64             super(cs, 1.0f, 1.0f);
  65         }
  66 
  67         private CoderResult decodeArrayLoop(ByteBuffer src,
  68                                             CharBuffer dst)
  69         {
  70             byte[] sa = src.array();
  71             int sp = src.arrayOffset() + src.position();
  72             int sl = src.arrayOffset() + src.limit();
  73             assert (sp <= sl);
  74             sp = (sp <= sl ? sp : sl);
  75             char[] da = dst.array();
  76             int dp = dst.arrayOffset() + dst.position();
  77             int dl = dst.arrayOffset() + dst.limit();
  78             assert (dp <= dl);
  79             dp = (dp <= dl ? dp : dl);
  80 
  81             try {
  82                 while (sp < sl) {
  83                     byte b = sa[sp];
  84                     if (b >= 0) {
  85                         if (dp >= dl)
  86                             return CoderResult.OVERFLOW;
  87                         da[dp++] = (char)b;
  88                         sp++;
  89                         continue;
  90                     }
  91                     return CoderResult.malformedForLength(1);
  92                 }
  93                 return CoderResult.UNDERFLOW;
  94             } finally {
  95                 src.position(sp - src.arrayOffset());
  96                 dst.position(dp - dst.arrayOffset());
  97             }
  98         }
  99 
 100         private CoderResult decodeBufferLoop(ByteBuffer src,
 101                                              CharBuffer dst)
 102         {
 103             int mark = src.position();
 104             try {
 105                 while (src.hasRemaining()) {
 106                     byte b = src.get();
 107                     if (b >= 0) {
 108                         if (!dst.hasRemaining())
 109                             return CoderResult.OVERFLOW;
 110                         dst.put((char)b);
 111                         mark++;
 112                         continue;
 113                     }
 114                     return CoderResult.malformedForLength(1);
 115                 }
 116                 return CoderResult.UNDERFLOW;
 117             } finally {
 118                 src.position(mark);
 119             }
 120         }
 121 
 122         protected CoderResult decodeLoop(ByteBuffer src,
 123                                          CharBuffer dst)
 124         {
 125             if (src.hasArray() && dst.hasArray())
 126                 return decodeArrayLoop(src, dst);
 127             else
 128                 return decodeBufferLoop(src, dst);
 129         }
 130     }
 131 
 132     private static class Encoder extends CharsetEncoder {
 133 
 134         private Encoder(Charset cs) {
 135             super(cs, 1.0f, 1.0f);
 136         }
 137 
 138         public boolean canEncode(char c) {
 139             return c < 0x80;
 140         }
 141 
 142         public boolean isLegalReplacement(byte[] repl) {
 143             return (repl.length == 1 && repl[0] >= 0) ||
 144                    super.isLegalReplacement(repl);
 145         }
 146 
 147         private final Surrogate.Parser sgp = new Surrogate.Parser();
 148         private CoderResult encodeArrayLoop(CharBuffer src,
 149                                             ByteBuffer dst)
 150         {
 151             char[] sa = src.array();
 152             int sp = src.arrayOffset() + src.position();
 153             int sl = src.arrayOffset() + src.limit();
 154             assert (sp <= sl);
 155             sp = (sp <= sl ? sp : sl);
 156             byte[] da = dst.array();
 157             int dp = dst.arrayOffset() + dst.position();
 158             int dl = dst.arrayOffset() + dst.limit();
 159             assert (dp <= dl);
 160             dp = (dp <= dl ? dp : dl);
 161 
 162             try {
 163                 while (sp < sl) {
 164                     char c = sa[sp];
 165                     if (c < 0x80) {
 166                         if (dp >= dl)
 167                             return CoderResult.OVERFLOW;
 168                         da[dp] = (byte)c;
 169                         sp++; dp++;
 170                         continue;
 171                     }
 172                     if (sgp.parse(c, sa, sp, sl) < 0)
 173                         return sgp.error();
 174                     return sgp.unmappableResult();
 175                 }
 176                 return CoderResult.UNDERFLOW;
 177             } finally {
 178                 src.position(sp - src.arrayOffset());
 179                 dst.position(dp - dst.arrayOffset());
 180             }
 181         }
 182 
 183         private CoderResult encodeBufferLoop(CharBuffer src,
 184                                              ByteBuffer dst)
 185         {
 186             int mark = src.position();
 187             try {
 188                 while (src.hasRemaining()) {
 189                     char c = src.get();
 190                     if (c < 0x80) {
 191                         if (!dst.hasRemaining())
 192                             return CoderResult.OVERFLOW;
 193                         dst.put((byte)c);
 194                         mark++;
 195                         continue;
 196                     }
 197                     if (sgp.parse(c, src) < 0)
 198                         return sgp.error();
 199                     return sgp.unmappableResult();
 200                 }
 201                 return CoderResult.UNDERFLOW;
 202             } finally {
 203                 src.position(mark);
 204             }
 205         }
 206 
 207         protected CoderResult encodeLoop(CharBuffer src,
 208                                          ByteBuffer dst)
 209         {
 210             if (src.hasArray() && dst.hasArray())
 211                 return encodeArrayLoop(src, dst);
 212             else
 213                 return encodeBufferLoop(src, dst);
 214         }
 215 
 216     }
 217 }