2 *******************************************************************************
3 * Copyright (C) 2006-2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 package com.ibm.icu.charset;
10 import java.nio.ByteBuffer;
11 import java.nio.CharBuffer;
12 import java.nio.charset.CharsetDecoder;
13 import java.nio.charset.CharsetEncoder;
14 import java.nio.charset.CoderResult;
16 import com.ibm.icu.text.UnicodeSet;
18 class Charset88591 extends CharsetASCII {
19 public Charset88591(String icuCanonicalName, String javaCanonicalName, String[] aliases) {
20 super(icuCanonicalName, javaCanonicalName, aliases);
23 class CharsetDecoder88591 extends CharsetDecoderASCII {
24 public CharsetDecoder88591(CharsetICU cs) {
28 protected CoderResult decodeLoopCoreOptimized(ByteBuffer source, CharBuffer target,
29 byte[] sourceArray, char[] targetArray, int oldSource, int offset, int limit) {
32 * perform 88591 conversion from the source array to the target array. no range check is
35 for (int i = oldSource; i < limit; i++)
36 targetArray[i + offset] = (char) (sourceArray[i] & 0xff);
41 protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target) {
44 * perform 88591 conversion from the source buffer to the target buffer. no range check
47 while (source.hasRemaining()) {
49 if (target.hasRemaining()) {
50 target.put((char) (ch & 0xff));
52 return CoderResult.OVERFLOW;
56 return CoderResult.UNDERFLOW;
60 class CharsetEncoder88591 extends CharsetEncoderASCII {
61 public CharsetEncoder88591(CharsetICU cs) {
65 protected final CoderResult encodeLoopCoreOptimized(CharBuffer source, ByteBuffer target,
66 char[] sourceArray, byte[] targetArray, int oldSource, int offset, int limit,
71 * perform 88591 conversion from the source array to the target array, making sure each
72 * char in the source is within the correct range
74 for (i = oldSource; i < limit; i++) {
75 ch = (int) sourceArray[i];
76 if ((ch & 0xff00) == 0) {
77 targetArray[i + offset] = (byte) ch;
84 * if some byte was not in the correct range, we need to deal with this byte by calling
85 * encodeMalformedOrUnmappable and move the source and target positions to reflect the
86 * early termination of the loop
88 if ((ch & 0xff00) != 0) {
89 source.position((i + 1) - source.arrayOffset());
90 target.position(i + offset);
91 return encodeMalformedOrUnmappable(source, ch, flush);
96 protected final CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target, boolean flush) {
100 * perform 88591 conversion from the source buffer to the target buffer, making sure
101 * each char in the source is within the correct range
104 while (source.hasRemaining()) {
105 ch = (int) source.get();
106 if ((ch & 0xff00) == 0) {
107 if (target.hasRemaining()) {
108 target.put((byte) ch);
110 return CoderResult.OVERFLOW;
114 * if we reach here, it's because a character was not in the correct range, and we need
115 * to deal with this by calling encodeMalformedOrUnmappable.
117 return encodeMalformedOrUnmappable(source, ch, flush);
121 return CoderResult.UNDERFLOW;
126 public CharsetDecoder newDecoder() {
127 return new CharsetDecoder88591(this);
130 public CharsetEncoder newEncoder() {
131 return new CharsetEncoder88591(this);
134 void getUnicodeSetImpl( UnicodeSet setFillIn, int which){
135 setFillIn.add(0,0xff);