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 static final ISO_8859_1 INSTANCE = new ISO_8859_1(); 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 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 128 private static class Encoder extends CharsetEncoder { 129 130 private Encoder(Charset cs) { 131 super(cs, 1.0f, 1.0f); 132 } 133 134 public boolean canEncode(char c) { 135 return c <= '\u00FF'; 136 } 137 138 public boolean isLegalReplacement(byte[] repl) { 139 return true; // we accept any byte value 140 } 141 142 private final Surrogate.Parser sgp = new Surrogate.Parser(); 143 144 // Method possible replaced with a compiler intrinsic. 145 private static int encodeISOArray(char[] sa, int sp, 146 byte[] da, int dp, int len) { 147 if (len <= 0) { 148 return 0; 149 } 150 encodeISOArrayCheck(sa, sp, da, dp, len); 151 return implEncodeISOArray(sa, sp, da, dp, len); 152 } 153 154 @HotSpotIntrinsicCandidate 155 private static int implEncodeISOArray(char[] sa, int sp, 156 byte[] da, int dp, int len) 157 { 158 int i = 0; 159 for (; i < len; i++) { 160 char c = sa[sp++]; 161 if (c > '\u00FF') 162 break; 163 da[dp++] = (byte)c; 164 } 165 return i; 166 } 167 168 private static void encodeISOArrayCheck(char[] sa, int sp, 169 byte[] da, int dp, int len) { 170 Objects.requireNonNull(sa); 171 Objects.requireNonNull(da); 172 173 if (sp < 0 || sp >= sa.length) { 174 throw new ArrayIndexOutOfBoundsException(sp); 175 } 176 177 if (dp < 0 || dp >= da.length) { 178 throw new ArrayIndexOutOfBoundsException(dp); 179 } 180 181 int endIndexSP = sp + len - 1; 182 if (endIndexSP < 0 || endIndexSP >= sa.length) { 183 throw new ArrayIndexOutOfBoundsException(endIndexSP); 184 } 185 186 int endIndexDP = dp + len - 1; 187 if (endIndexDP < 0 || endIndexDP >= da.length) { 188 throw new ArrayIndexOutOfBoundsException(endIndexDP); 189 } 190 } 191 192 private CoderResult encodeArrayLoop(CharBuffer src, 193 ByteBuffer dst) 194 { 195 char[] sa = src.array(); 196 int soff = src.arrayOffset(); 197 int sp = soff + src.position(); 198 int sl = soff + src.limit(); 199 assert (sp <= sl); 200 sp = (sp <= sl ? sp : sl); 201 byte[] da = dst.array(); 202 int doff = dst.arrayOffset(); 203 int dp = doff + dst.position(); 204 int dl = doff + dst.limit(); 205 assert (dp <= dl); 206 dp = (dp <= dl ? dp : dl); 207 int dlen = dl - dp; 208 int slen = sl - sp; 209 int len = (dlen < slen) ? dlen : slen; 210 try { 211 int ret = encodeISOArray(sa, sp, da, dp, len); 212 sp = sp + ret; 213 dp = dp + ret; 214 if (ret != len) { 215 if (sgp.parse(sa[sp], sa, sp, sl) < 0) 216 return sgp.error(); 217 return sgp.unmappableResult(); 218 } 219 if (len < slen) 220 return CoderResult.OVERFLOW; 221 return CoderResult.UNDERFLOW; 222 } finally { 223 src.position(sp - soff); 224 dst.position(dp - doff); 225 } 226 } 227 228 private CoderResult encodeBufferLoop(CharBuffer src, 229 ByteBuffer dst) 230 { 231 int mark = src.position(); 232 try { 233 while (src.hasRemaining()) { 234 char c = src.get(); 235 if (c <= '\u00FF') { 236 if (!dst.hasRemaining()) 237 return CoderResult.OVERFLOW; 238 dst.put((byte)c); 239 mark++; 240 continue; 241 } 242 if (sgp.parse(c, src) < 0) 243 return sgp.error(); 244 return sgp.unmappableResult(); 245 } 246 return CoderResult.UNDERFLOW; 247 } finally { 248 src.position(mark); 249 } 250 } 251 252 protected CoderResult encodeLoop(CharBuffer src, 253 ByteBuffer dst) 254 { 255 if (src.hasArray() && dst.hasArray()) 256 return encodeArrayLoop(src, dst); 257 else 258 return encodeBufferLoop(src, dst); 259 } 260 } 261 }