1 /* 2 * Copyright (c) 2000, 2013, 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 import java.util.Objects; 36 37 import jdk.internal.HotSpotIntrinsicCandidate; 38 39 class ISO_8859_1 40 extends Charset 41 implements HistoricallyNamedCharset 42 { 43 44 public ISO_8859_1() { 45 super("ISO-8859-1", StandardCharsets.aliases_ISO_8859_1); 46 } 47 48 public String historicalName() { 49 return "ISO8859_1"; 50 } 51 52 public boolean contains(Charset cs) { 53 return ((cs instanceof US_ASCII) 54 || (cs instanceof ISO_8859_1)); 55 } 56 57 public CharsetDecoder newDecoder() { 58 return new Decoder(this); 59 } 60 61 public CharsetEncoder newEncoder() { 62 return new Encoder(this); 63 } 64 65 private static class Decoder extends CharsetDecoder 66 implements ArrayDecoder { 67 private Decoder(Charset cs) { 68 super(cs, 1.0f, 1.0f); 69 } 70 71 private CoderResult decodeArrayLoop(ByteBuffer src, 72 CharBuffer dst) 73 { 74 byte[] sa = src.array(); 75 int sp = src.arrayOffset() + src.position(); 76 int sl = src.arrayOffset() + src.limit(); 77 assert (sp <= sl); 78 sp = (sp <= sl ? sp : sl); 79 char[] da = dst.array(); 80 int dp = dst.arrayOffset() + dst.position(); 81 int dl = dst.arrayOffset() + dst.limit(); 82 assert (dp <= dl); 83 dp = (dp <= dl ? dp : dl); 84 85 try { 86 while (sp < sl) { 87 byte b = sa[sp]; 88 if (dp >= dl) 89 return CoderResult.OVERFLOW; 90 da[dp++] = (char)(b & 0xff); 91 sp++; 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 (!dst.hasRemaining()) 108 return CoderResult.OVERFLOW; 109 dst.put((char)(b & 0xff)); 110 mark++; 111 } 112 return CoderResult.UNDERFLOW; 113 } finally { 114 src.position(mark); 115 } 116 } 117 118 protected CoderResult decodeLoop(ByteBuffer src, 119 CharBuffer dst) 120 { 121 if (src.hasArray() && dst.hasArray()) 122 return decodeArrayLoop(src, dst); 123 else 124 return decodeBufferLoop(src, dst); 125 } 126 127 public int decode(byte[] src, int sp, int len, char[] dst) { 128 if (len > dst.length) 129 len = dst.length; 130 int dp = 0; 131 while (dp < len) 132 dst[dp++] = (char)(src[sp++] & 0xff); 133 return dp; 134 } 135 } 136 137 private static class Encoder extends CharsetEncoder 138 implements ArrayEncoder { 139 private Encoder(Charset cs) { 140 super(cs, 1.0f, 1.0f); 141 } 142 143 public boolean canEncode(char c) { 144 return c <= '\u00FF'; 145 } 146 147 public boolean isLegalReplacement(byte[] repl) { 148 return true; // we accept any byte value 149 } 150 151 private final Surrogate.Parser sgp = new Surrogate.Parser(); 152 153 // Method possible replaced with a compiler intrinsic. 154 private static int encodeISOArray(char[] sa, int sp, 155 byte[] da, int dp, int len) { 156 encodeISOArrayCheck(sa, sp, da, dp, len); 157 return implEncodeISOArray(sa, sp, da, dp, len); 158 } 159 160 @HotSpotIntrinsicCandidate 161 private static int implEncodeISOArray(char[] sa, int sp, 162 byte[] da, int dp, int len) 163 { 164 int i = 0; 165 for (; i < len; i++) { 166 char c = sa[sp++]; 167 if (c > '\u00FF') 168 break; 169 da[dp++] = (byte)c; 170 } 171 return i; 172 } 173 174 private static void encodeISOArrayCheck(char[] sa, int sp, 175 byte[] da, int dp, int len) { 176 if (len <= 0) { 177 return; // not an error because encodeISOArrayImpl won't execute if len <= 0 178 } 179 180 Objects.requireNonNull(sa); 181 Objects.requireNonNull(da); 182 183 if (sp < 0 || sp >= sa.length) { 184 throw new ArrayIndexOutOfBoundsException(sp); 185 } 186 187 if (dp < 0 || dp >= da.length) { 188 throw new ArrayIndexOutOfBoundsException(dp); 189 } 190 191 int endIndexSP = sp + len - 1; 192 if (endIndexSP < 0 || endIndexSP >= sa.length) { 193 throw new ArrayIndexOutOfBoundsException(endIndexSP); 194 } 195 196 int endIndexDP = dp + len - 1; 197 if (endIndexDP < 0 || endIndexDP >= da.length) { 198 throw new ArrayIndexOutOfBoundsException(endIndexDP); 199 } 200 } 201 202 private CoderResult encodeArrayLoop(CharBuffer src, 203 ByteBuffer dst) 204 { 205 char[] sa = src.array(); 206 int soff = src.arrayOffset(); 207 int sp = soff + src.position(); 208 int sl = soff + src.limit(); 209 assert (sp <= sl); 210 sp = (sp <= sl ? sp : sl); 211 byte[] da = dst.array(); 212 int doff = dst.arrayOffset(); 213 int dp = doff + dst.position(); 214 int dl = doff + dst.limit(); 215 assert (dp <= dl); 216 dp = (dp <= dl ? dp : dl); 217 int dlen = dl - dp; 218 int slen = sl - sp; 219 int len = (dlen < slen) ? dlen : slen; 220 try { 221 int ret = encodeISOArray(sa, sp, da, dp, len); 222 sp = sp + ret; 223 dp = dp + ret; 224 if (ret != len) { 225 if (sgp.parse(sa[sp], sa, sp, sl) < 0) 226 return sgp.error(); 227 return sgp.unmappableResult(); 228 } 229 if (len < slen) 230 return CoderResult.OVERFLOW; 231 return CoderResult.UNDERFLOW; 232 } finally { 233 src.position(sp - soff); 234 dst.position(dp - doff); 235 } 236 } 237 238 private CoderResult encodeBufferLoop(CharBuffer src, 239 ByteBuffer dst) 240 { 241 int mark = src.position(); 242 try { 243 while (src.hasRemaining()) { 244 char c = src.get(); 245 if (c <= '\u00FF') { 246 if (!dst.hasRemaining()) 247 return CoderResult.OVERFLOW; 248 dst.put((byte)c); 249 mark++; 250 continue; 251 } 252 if (sgp.parse(c, src) < 0) 253 return sgp.error(); 254 return sgp.unmappableResult(); 255 } 256 return CoderResult.UNDERFLOW; 257 } finally { 258 src.position(mark); 259 } 260 } 261 262 protected CoderResult encodeLoop(CharBuffer src, 263 ByteBuffer dst) 264 { 265 if (src.hasArray() && dst.hasArray()) 266 return encodeArrayLoop(src, dst); 267 else 268 return encodeBufferLoop(src, dst); 269 } 270 271 private byte repl = (byte)'?'; 272 protected void implReplaceWith(byte[] newReplacement) { 273 repl = newReplacement[0]; 274 } 275 276 public int encode(char[] src, int sp, int len, byte[] dst) { 277 int dp = 0; 278 int slen = Math.min(len, dst.length); 279 int sl = sp + slen; 280 while (sp < sl) { 281 int ret = encodeISOArray(src, sp, dst, dp, slen); 282 sp = sp + ret; 283 dp = dp + ret; 284 if (ret != slen) { 285 char c = src[sp++]; 286 if (Character.isHighSurrogate(c) && sp < sl && 287 Character.isLowSurrogate(src[sp])) { 288 if (len > dst.length) { 289 sl++; 290 len--; 291 } 292 sp++; 293 } 294 dst[dp++] = repl; 295 slen = Math.min((sl - sp), (dst.length - dp)); 296 } 297 } 298 return dp; 299 } 300 } 301 }