1 /*
   2  * Copyright 2000-2004 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any 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.Arrays;
  35 
  36 public class US_ASCII
  37     extends Charset
  38     implements HistoricallyNamedCharset
  39 {
  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                                  implements ArrayDecoder {
  63 
  64         private Decoder(Charset cs) {
  65             super(cs, 1.0f, 1.0f);
  66         }
  67 
  68         private CoderResult decodeArrayLoop(ByteBuffer src,
  69                                             CharBuffer dst)
  70         {
  71             byte[] sa = src.array();
  72             int sp = src.arrayOffset() + src.position();
  73             int sl = src.arrayOffset() + src.limit();
  74             assert (sp <= sl);
  75             sp = (sp <= sl ? sp : sl);
  76             char[] da = dst.array();
  77             int dp = dst.arrayOffset() + dst.position();
  78             int dl = dst.arrayOffset() + dst.limit();
  79             assert (dp <= dl);
  80             dp = (dp <= dl ? dp : dl);
  81 
  82             try {
  83                 while (sp < sl) {
  84                     byte b = sa[sp];
  85                     if (b >= 0) {
  86                         if (dp >= dl)
  87                             return CoderResult.OVERFLOW;
  88                         da[dp++] = (char)b;
  89                         sp++;
  90                         continue;
  91                     }
  92                     return CoderResult.malformedForLength(1);
  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 (b >= 0) {
 109                         if (!dst.hasRemaining())
 110                             return CoderResult.OVERFLOW;
 111                         dst.put((char)b);
 112                         mark++;
 113                         continue;
 114                     }
 115                     return CoderResult.malformedForLength(1);
 116                 }
 117                 return CoderResult.UNDERFLOW;
 118             } finally {
 119                 src.position(mark);
 120             }
 121         }
 122 
 123         protected CoderResult decodeLoop(ByteBuffer src,
 124                                          CharBuffer dst)
 125         {
 126             if (src.hasArray() && dst.hasArray())
 127                 return decodeArrayLoop(src, dst);
 128             else
 129                 return decodeBufferLoop(src, dst);
 130         }
 131 
 132         private char repl = '\uFFFD';
 133         protected void implReplaceWith(String newReplacement) {
 134             repl = newReplacement.charAt(0);
 135         }
 136 
 137         public int decode(byte[] src, int sp, int len, char[] dst) {
 138             int dp = 0;
 139             len = Math.min(len, dst.length);
 140             while (dp < len) {
 141                 byte b = src[sp++];
 142                 if (b >= 0)
 143                     dst[dp++] = (char)b;
 144                 else
 145                     dst[dp++] = repl;
 146             }
 147             return dp;
 148         }
 149     }
 150 
 151     private static class Encoder extends CharsetEncoder
 152                                  implements ArrayEncoder {
 153 
 154         private Encoder(Charset cs) {
 155             super(cs, 1.0f, 1.0f);
 156         }
 157 
 158         public boolean canEncode(char c) {
 159             return c < 0x80;
 160         }
 161 
 162         public boolean isLegalReplacement(byte[] repl) {
 163             return (repl.length == 1 && repl[0] >= 0);
 164         }
 165 
 166         private final Surrogate.Parser sgp = new Surrogate.Parser();
 167         private CoderResult encodeArrayLoop(CharBuffer src,
 168                                             ByteBuffer dst)
 169         {
 170             char[] sa = src.array();
 171             int sp = src.arrayOffset() + src.position();
 172             int sl = src.arrayOffset() + src.limit();
 173             assert (sp <= sl);
 174             sp = (sp <= sl ? sp : sl);
 175             byte[] da = dst.array();
 176             int dp = dst.arrayOffset() + dst.position();
 177             int dl = dst.arrayOffset() + dst.limit();
 178             assert (dp <= dl);
 179             dp = (dp <= dl ? dp : dl);
 180 
 181             try {
 182                 while (sp < sl) {
 183                     char c = sa[sp];
 184                     if (c < 0x80) {
 185                         if (dp >= dl)
 186                             return CoderResult.OVERFLOW;
 187                         da[dp] = (byte)c;
 188                         sp++; dp++;
 189                         continue;
 190                     }
 191                     if (sgp.parse(c, sa, sp, sl) < 0)
 192                         return sgp.error();
 193                     return sgp.unmappableResult();
 194                 }
 195                 return CoderResult.UNDERFLOW;
 196             } finally {
 197                 src.position(sp - src.arrayOffset());
 198                 dst.position(dp - dst.arrayOffset());
 199             }
 200         }
 201 
 202         private CoderResult encodeBufferLoop(CharBuffer src,
 203                                              ByteBuffer dst)
 204         {
 205             int mark = src.position();
 206             try {
 207                 while (src.hasRemaining()) {
 208                     char c = src.get();
 209                     if (c < 0x80) {
 210                         if (!dst.hasRemaining())
 211                             return CoderResult.OVERFLOW;
 212                         dst.put((byte)c);
 213                         mark++;
 214                         continue;
 215                     }
 216                     if (sgp.parse(c, src) < 0)
 217                         return sgp.error();
 218                     return sgp.unmappableResult();
 219                 }
 220                 return CoderResult.UNDERFLOW;
 221             } finally {
 222                 src.position(mark);
 223             }
 224         }
 225 
 226         protected CoderResult encodeLoop(CharBuffer src,
 227                                          ByteBuffer dst)
 228         {
 229             if (src.hasArray() && dst.hasArray())
 230                 return encodeArrayLoop(src, dst);
 231             else
 232                 return encodeBufferLoop(src, dst);
 233         }
 234 
 235         private byte repl = (byte)'?';
 236         protected void implReplaceWith(byte[] newReplacement) {
 237             repl = newReplacement[0];
 238         }
 239 
 240         public int encode(char[] src, int sp, int len, byte[] dst) {
 241             int dp = 0;
 242             int sl = sp + Math.min(len, dst.length);
 243             while (sp < sl) {
 244                 char c = src[sp++];
 245                 if (c < 0x80) {
 246                     dst[dp++] = (byte)c;
 247                     continue;
 248                 }
 249                 if (Surrogate.isHigh(c) && sp < sl &&
 250                     Surrogate.isLow(src[sp])) {
 251                     if (len > dst.length) {
 252                         sl++;
 253                         len--;
 254                     }
 255                     sp++;
 256                 }
 257                 dst[dp++] = repl;
 258             }
 259             return dp;
 260         }
 261     }
 262 
 263 }