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 class ISO_8859_1
37 extends Charset
38 implements HistoricallyNamedCharset
39 {
40
41 public ISO_8859_1() {
42 super("ISO-8859-1", StandardCharsets.aliases_ISO_8859_1);
43 }
44
45 public String historicalName() {
46 return "ISO8859_1";
47 }
48
49 public boolean contains(Charset cs) {
50 return ((cs instanceof US_ASCII)
51 || (cs instanceof ISO_8859_1));
52 }
53
54 public CharsetDecoder newDecoder() {
130 return dp;
131 }
132 }
133
134 private static class Encoder extends CharsetEncoder
135 implements ArrayEncoder {
136 private Encoder(Charset cs) {
137 super(cs, 1.0f, 1.0f);
138 }
139
140 public boolean canEncode(char c) {
141 return c <= '\u00FF';
142 }
143
144 public boolean isLegalReplacement(byte[] repl) {
145 return true; // we accept any byte value
146 }
147
148 private final Surrogate.Parser sgp = new Surrogate.Parser();
149
150 // JVM may replace this method with intrinsic code.
151 private static int encodeISOArray(char[] sa, int sp,
152 byte[] da, int dp, int len)
153 {
154 int i = 0;
155 for (; i < len; i++) {
156 char c = sa[sp++];
157 if (c > '\u00FF')
158 break;
159 da[dp++] = (byte)c;
160 }
161 return i;
162 }
163
164 private CoderResult encodeArrayLoop(CharBuffer src,
165 ByteBuffer dst)
166 {
167 char[] sa = src.array();
168 int soff = src.arrayOffset();
169 int sp = soff + src.position();
170 int sl = soff + src.limit();
171 assert (sp <= sl);
172 sp = (sp <= sl ? sp : sl);
173 byte[] da = dst.array();
174 int doff = dst.arrayOffset();
175 int dp = doff + dst.position();
176 int dl = doff + dst.limit();
177 assert (dp <= dl);
178 dp = (dp <= dl ? dp : dl);
179 int dlen = dl - dp;
180 int slen = sl - sp;
181 int len = (dlen < slen) ? dlen : slen;
|
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() {
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;
|