1 /* 2 * Copyright (c) 2000, 2004, 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.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 super.isLegalReplacement(repl); 165 } 166 167 private final Surrogate.Parser sgp = new Surrogate.Parser(); 168 private CoderResult encodeArrayLoop(CharBuffer src, 169 ByteBuffer dst) 170 { 171 char[] sa = src.array(); 172 int sp = src.arrayOffset() + src.position(); 173 int sl = src.arrayOffset() + src.limit(); 174 assert (sp <= sl); 175 sp = (sp <= sl ? sp : sl); 176 byte[] da = dst.array(); 177 int dp = dst.arrayOffset() + dst.position(); 178 int dl = dst.arrayOffset() + dst.limit(); 179 assert (dp <= dl); 180 dp = (dp <= dl ? dp : dl); 181 182 try { 183 while (sp < sl) { 184 char c = sa[sp]; 185 if (c < 0x80) { 186 if (dp >= dl) 187 return CoderResult.OVERFLOW; 188 da[dp] = (byte)c; 189 sp++; dp++; 190 continue; 191 } 192 if (sgp.parse(c, sa, sp, sl) < 0) 193 return sgp.error(); 194 return sgp.unmappableResult(); 195 } 196 return CoderResult.UNDERFLOW; 197 } finally { 198 src.position(sp - src.arrayOffset()); 199 dst.position(dp - dst.arrayOffset()); 200 } 201 } 202 203 private CoderResult encodeBufferLoop(CharBuffer src, 204 ByteBuffer dst) 205 { 206 int mark = src.position(); 207 try { 208 while (src.hasRemaining()) { 209 char c = src.get(); 210 if (c < 0x80) { 211 if (!dst.hasRemaining()) 212 return CoderResult.OVERFLOW; 213 dst.put((byte)c); 214 mark++; 215 continue; 216 } 217 if (sgp.parse(c, src) < 0) 218 return sgp.error(); 219 return sgp.unmappableResult(); 220 } 221 return CoderResult.UNDERFLOW; 222 } finally { 223 src.position(mark); 224 } 225 } 226 227 protected CoderResult encodeLoop(CharBuffer src, 228 ByteBuffer dst) 229 { 230 if (src.hasArray() && dst.hasArray()) 231 return encodeArrayLoop(src, dst); 232 else 233 return encodeBufferLoop(src, dst); 234 } 235 236 private byte repl = (byte)'?'; 237 protected void implReplaceWith(byte[] newReplacement) { 238 repl = newReplacement[0]; 239 } 240 241 public int encode(char[] src, int sp, int len, byte[] dst) { 242 int dp = 0; 243 int sl = sp + Math.min(len, dst.length); 244 while (sp < sl) { 245 char c = src[sp++]; 246 if (c < 0x80) { 247 dst[dp++] = (byte)c; 248 continue; 249 } 250 if (Character.isHighSurrogate(c) && sp < sl && 251 Character.isLowSurrogate(src[sp])) { 252 if (len > dst.length) { 253 sl++; 254 len--; 255 } 256 sp++; 257 } 258 dst[dp++] = repl; 259 } 260 return dp; 261 } 262 } 263 264 }