]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/charset/src/com/ibm/icu/charset/Charset88591.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / charset / src / com / ibm / icu / charset / Charset88591.java
1 /**
2  *******************************************************************************
3  * Copyright (C) 2006-2011, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 package com.ibm.icu.charset;
9
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;
15
16 import com.ibm.icu.text.UnicodeSet;
17
18 class Charset88591 extends CharsetASCII {
19     public Charset88591(String icuCanonicalName, String javaCanonicalName, String[] aliases) {
20         super(icuCanonicalName, javaCanonicalName, aliases);
21     }
22
23     class CharsetDecoder88591 extends CharsetDecoderASCII {
24         public CharsetDecoder88591(CharsetICU cs) {
25             super(cs);
26         }
27
28         protected CoderResult decodeLoopCoreOptimized(ByteBuffer source, CharBuffer target,
29                 byte[] sourceArray, char[] targetArray, int oldSource, int offset, int limit) {
30
31             /*
32              * perform 88591 conversion from the source array to the target array. no range check is
33              * necessary.
34              */
35             for (int i = oldSource; i < limit; i++)
36                 targetArray[i + offset] = (char) (sourceArray[i] & 0xff);
37
38             return null;
39         }
40
41         protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target) {
42             byte ch;
43             /*
44              * perform 88591 conversion from the source buffer to the target buffer. no range check
45              * is necessary.
46              */
47             while (source.hasRemaining()) {
48                 ch = source.get();
49                 if (target.hasRemaining()) {
50                     target.put((char) (ch & 0xff));
51                 } else {
52                     return CoderResult.OVERFLOW;
53                 }
54             }
55             
56             return CoderResult.UNDERFLOW;
57         }
58     }
59
60     class CharsetEncoder88591 extends CharsetEncoderASCII {
61         public CharsetEncoder88591(CharsetICU cs) {
62             super(cs);
63         }
64
65         protected final CoderResult encodeLoopCoreOptimized(CharBuffer source, ByteBuffer target,
66                 char[] sourceArray, byte[] targetArray, int oldSource, int offset, int limit,
67                 boolean flush) {
68             int i, ch = 0;
69
70             /*
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
73              */
74             for (i = oldSource; i < limit; i++) {
75                 ch = (int) sourceArray[i];
76                 if ((ch & 0xff00) == 0) {
77                     targetArray[i + offset] = (byte) ch;
78                 } else {
79                     break;
80                 }
81             }
82
83             /*
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
87              */
88             if ((ch & 0xff00) != 0) {
89                 source.position((i + 1) - source.arrayOffset());
90                 target.position(i + offset);
91                 return encodeMalformedOrUnmappable(source, ch, flush);
92             } else
93                 return null;
94         }
95
96         protected final CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target, boolean flush) {
97             int ch;
98
99             /*
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
102              */
103             
104             while (source.hasRemaining()) {
105                 ch = (int) source.get();
106                 if ((ch & 0xff00) == 0) {
107                     if (target.hasRemaining()) {
108                         target.put((byte) ch);
109                     } else {
110                         return CoderResult.OVERFLOW;
111                     }
112                 } else {
113                     /*
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.
116                      */
117                     return encodeMalformedOrUnmappable(source, ch, flush);
118                 }
119             }
120             
121             return CoderResult.UNDERFLOW;
122         }
123
124     }
125
126     public CharsetDecoder newDecoder() {
127         return new CharsetDecoder88591(this);
128     }
129
130     public CharsetEncoder newEncoder() {
131         return new CharsetEncoder88591(this);
132     }
133     
134     void getUnicodeSetImpl( UnicodeSet setFillIn, int which){
135         setFillIn.add(0,0xff);
136      }
137 }