001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.niochar.charset.additional;
019:
020: import java.nio.charset.Charset;
021: import java.nio.charset.CoderResult;
022: import java.nio.charset.CharsetDecoder;
023: import java.nio.charset.CharsetEncoder;
024: import java.nio.CharBuffer;
025: import java.nio.ByteBuffer;
026:
027: import org.apache.harmony.nio.AddressUtil;
028:
029: public class IBM01143 extends Charset {
030:
031: public IBM01143(String csName, String[] aliases) {
032: super (csName, aliases);
033: }
034:
035: public boolean contains(Charset cs) {
036: return cs.name().equalsIgnoreCase("IBM01140")
037: || cs.name().equalsIgnoreCase("IBM01141")
038: || cs.name().equalsIgnoreCase("IBM01142")
039: || cs.name().equalsIgnoreCase("IBM01143")
040: || cs.name().equalsIgnoreCase("IBM01144")
041: || cs.name().equalsIgnoreCase("IBM01145")
042: || cs.name().equalsIgnoreCase("IBM01146")
043: || cs.name().equalsIgnoreCase("IBM01147")
044: || cs.name().equalsIgnoreCase("IBM01148")
045: || cs.name().equalsIgnoreCase("IBM01149")
046: || cs.name().equalsIgnoreCase("IBM367")
047: || cs.name().equalsIgnoreCase("US-ASCII");
048: }
049:
050: public CharsetDecoder newDecoder() {
051: return new Decoder(this );
052: }
053:
054: public CharsetEncoder newEncoder() {
055: return new Encoder(this );
056: }
057:
058: private static final class Decoder extends CharsetDecoder {
059: private Decoder(Charset cs) {
060: super (cs, 1, 1);
061:
062: }
063:
064: public native int nDecode(char[] array, int arrPosition,
065: int remaining, long outAddr, int absolutePos);
066:
067: protected CoderResult decodeLoop(ByteBuffer bb, CharBuffer cb) {
068: int cbRemaining = cb.remaining();
069: if (bb.isDirect() && bb.hasRemaining() && cb.hasArray()) {
070: int toProceed = bb.remaining();
071: int cbPos = cb.position();
072: int bbPos = bb.position();
073: boolean throwOverflow = false;
074: if (cbRemaining < toProceed) {
075: toProceed = cbRemaining;
076: throwOverflow = true;
077: }
078: int res = nDecode(cb.array(), cb.arrayOffset() + cbPos,
079: toProceed, AddressUtil
080: .getDirectBufferAddress(bb), bbPos);
081: bb.position(bbPos + res);
082: cb.position(cbPos + res);
083: if (throwOverflow)
084: return CoderResult.OVERFLOW;
085: } else {
086: if (bb.hasArray() && cb.hasArray()) {
087: int rem = bb.remaining();
088: rem = cbRemaining >= rem ? rem : cbRemaining;
089: byte[] bArr = bb.array();
090: char[] cArr = cb.array();
091: int bStart = bb.position();
092: int cStart = cb.position();
093: int i;
094: for (i = bStart; i < bStart + rem; i++) {
095: char in = (char) (bArr[i] & 0xFF);
096: if (in >= 4) {
097: int index = (int) in - 4;
098: cArr[cStart++] = (char) arr[index];
099: } else {
100: cArr[cStart++] = (char) (in & 0xFF);
101: }
102: }
103: bb.position(i);
104: cb.position(cStart);
105: if (rem == cbRemaining && bb.hasRemaining())
106: return CoderResult.OVERFLOW;
107: } else {
108: while (bb.hasRemaining()) {
109: if (cbRemaining == 0)
110: return CoderResult.OVERFLOW;
111: char in = (char) (bb.get() & 0xFF);
112: if (in >= 4) {
113: int index = (int) in - 4;
114: cb.put(arr[index]);
115: } else {
116: cb.put((char) (in & 0xFF));
117: }
118: cbRemaining--;
119: }
120: }
121: }
122: return CoderResult.UNDERFLOW;
123: }
124:
125: final static char[] arr = { 0x009C, 0x0009, 0x0086, 0x007F,
126: 0x0097, 0x008D, 0x008E, 0x000B, 0x000C, 0x000D, 0x000E,
127: 0x000F, 0x0010, 0x0011, 0x0012, 0x0013, 0x009D, 0x0085,
128: 0x0008, 0x0087, 0x0018, 0x0019, 0x0092, 0x008F, 0x001C,
129: 0x001D, 0x001E, 0x001F, 0x0080, 0x0081, 0x0082, 0x0083,
130: 0x0084, 0x000A, 0x0017, 0x001B, 0x0088, 0x0089, 0x008A,
131: 0x008B, 0x008C, 0x0005, 0x0006, 0x0007, 0x0090, 0x0091,
132: 0x0016, 0x0093, 0x0094, 0x0095, 0x0096, 0x0004, 0x0098,
133: 0x0099, 0x009A, 0x009B, 0x0014, 0x0015, 0x009E, 0x001A,
134: 0x0020, 0x00A0, 0x00E2, 0x007B, 0x00E0, 0x00E1, 0x00E3,
135: 0x007D, 0x00E7, 0x00F1, 0x00A7, 0x002E, 0x003C, 0x0028,
136: 0x002B, 0x0021, 0x0026, 0x0060, 0x00EA, 0x00EB, 0x00E8,
137: 0x00ED, 0x00EE, 0x00EF, 0x00EC, 0x00DF, 0x20AC, 0x00C5,
138: 0x002A, 0x0029, 0x003B, 0x005E, 0x002D, 0x002F, 0x00C2,
139: 0x0023, 0x00C0, 0x00C1, 0x00C3, 0x0024, 0x00C7, 0x00D1,
140: 0x00F6, 0x002C, 0x0025, 0x005F, 0x003E, 0x003F, 0x00F8,
141: 0x005C, 0x00CA, 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF,
142: 0x00CC, 0x00E9, 0x003A, 0x00C4, 0x00D6, 0x0027, 0x003D,
143: 0x0022, 0x00D8, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065,
144: 0x0066, 0x0067, 0x0068, 0x0069, 0x00AB, 0x00BB, 0x00F0,
145: 0x00FD, 0x00FE, 0x00B1, 0x00B0, 0x006A, 0x006B, 0x006C,
146: 0x006D, 0x006E, 0x006F, 0x0070, 0x0071, 0x0072, 0x00AA,
147: 0x00BA, 0x00E6, 0x00B8, 0x00C6, 0x005D, 0x00B5, 0x00FC,
148: 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079,
149: 0x007A, 0x00A1, 0x00BF, 0x00D0, 0x00DD, 0x00DE, 0x00AE,
150: 0x00A2, 0x00A3, 0x00A5, 0x00B7, 0x00A9, 0x005B, 0x00B6,
151: 0x00BC, 0x00BD, 0x00BE, 0x00AC, 0x007C, 0x00AF, 0x00A8,
152: 0x00B4, 0x00D7, 0x00E4, 0x0041, 0x0042, 0x0043, 0x0044,
153: 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x00AD, 0x00F4,
154: 0x00A6, 0x00F2, 0x00F3, 0x00F5, 0x00E5, 0x004A, 0x004B,
155: 0x004C, 0x004D, 0x004E, 0x004F, 0x0050, 0x0051, 0x0052,
156: 0x00B9, 0x00FB, 0x007E, 0x00F9, 0x00FA, 0x00FF, 0x00C9,
157: 0x00F7, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058,
158: 0x0059, 0x005A, 0x00B2, 0x00D4, 0x0040, 0x00D2, 0x00D3,
159: 0x00D5, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
160: 0x0036, 0x0037, 0x0038, 0x0039, 0x00B3, 0x00DB, 0x00DC,
161: 0x00D9, 0x00DA, 0x009F };
162: }
163:
164: private static final class Encoder extends CharsetEncoder {
165: private Encoder(Charset cs) {
166: super (cs, 1, 1);
167: }
168:
169: private native void nEncode(long outAddr, int absolutePos,
170: char[] array, int arrPosition, int[] res);
171:
172: protected CoderResult encodeLoop(CharBuffer cb, ByteBuffer bb) {
173: int bbRemaining = bb.remaining();
174: if (bb.isDirect() && cb.hasRemaining() && cb.hasArray()) {
175: int toProceed = cb.remaining();
176: int cbPos = cb.position();
177: int bbPos = bb.position();
178: boolean throwOverflow = false;
179: if (bbRemaining < toProceed) {
180: toProceed = bbRemaining;
181: throwOverflow = true;
182: }
183: int[] res = { toProceed, 0 };
184: nEncode(AddressUtil.getDirectBufferAddress(bb), bbPos,
185: cb.array(), cb.arrayOffset() + cbPos, res);
186: if (res[0] <= 0) {
187: bb.position(bbPos - res[0]);
188: cb.position(cbPos - res[0]);
189: if (res[1] != 0) {
190: if (res[1] < 0)
191: return CoderResult
192: .malformedForLength(-res[1]);
193: else
194: return CoderResult
195: .unmappableForLength(res[1]);
196: }
197: } else {
198: bb.position(bbPos + res[0]);
199: cb.position(cbPos + res[0]);
200: if (throwOverflow)
201: return CoderResult.OVERFLOW;
202: }
203: } else {
204: if (bb.hasArray() && cb.hasArray()) {
205: byte[] byteArr = bb.array();
206: char[] charArr = cb.array();
207: int rem = cb.remaining();
208: int byteArrStart = bb.position();
209: rem = bbRemaining <= rem ? bbRemaining : rem;
210: int x;
211: for (x = cb.position(); x < cb.position() + rem; x++) {
212: char c = charArr[x];
213: if (c > (char) 0x20AC) {
214: if (c >= 0xD800 && c <= 0xDFFF) {
215: if (x + 1 < cb.limit()) {
216: char c1 = charArr[x + 1];
217: if (c1 >= 0xD800 && c1 <= 0xDFFF) {
218: cb.position(x);
219: bb.position(byteArrStart);
220: return CoderResult
221: .unmappableForLength(2);
222: }
223: } else {
224: cb.position(x);
225: bb.position(byteArrStart);
226: return CoderResult.UNDERFLOW;
227: }
228: cb.position(x);
229: bb.position(byteArrStart);
230: return CoderResult
231: .malformedForLength(1);
232: }
233: cb.position(x);
234: bb.position(byteArrStart);
235: return CoderResult.unmappableForLength(1);
236: } else {
237: if (c < 0x04) {
238: byteArr[byteArrStart++] = (byte) c;
239: } else {
240: int index = (int) c >> 8;
241: index = encodeIndex[index];
242: if (index < 0) {
243: cb.position(x);
244: bb.position(byteArrStart);
245: return CoderResult
246: .unmappableForLength(1);
247: }
248: index <<= 8;
249: index += (int) c & 0xFF;
250: if ((byte) arr[index] != 0) {
251: byteArr[byteArrStart++] = (byte) arr[index];
252: } else {
253: cb.position(x);
254: bb.position(byteArrStart);
255: return CoderResult
256: .unmappableForLength(1);
257: }
258: }
259: }
260: }
261: cb.position(x);
262: bb.position(byteArrStart);
263: if (rem == bbRemaining && cb.hasRemaining()) {
264: return CoderResult.OVERFLOW;
265: }
266: } else {
267: while (cb.hasRemaining()) {
268: if (bbRemaining == 0)
269: return CoderResult.OVERFLOW;
270: char c = cb.get();
271: if (c > (char) 0x20AC) {
272: if (c >= 0xD800 && c <= 0xDFFF) {
273: if (cb.hasRemaining()) {
274: char c1 = cb.get();
275: if (c1 >= 0xD800 && c1 <= 0xDFFF) {
276: cb.position(cb.position() - 2);
277: return CoderResult
278: .unmappableForLength(2);
279: } else {
280: cb.position(cb.position() - 1);
281: }
282: } else {
283: cb.position(cb.position() - 1);
284: return CoderResult.UNDERFLOW;
285: }
286: cb.position(cb.position() - 1);
287: return CoderResult
288: .malformedForLength(1);
289: }
290: cb.position(cb.position() - 1);
291: return CoderResult.unmappableForLength(1);
292: } else {
293: if (c < 0x04) {
294: bb.put((byte) c);
295: } else {
296: int index = (int) c >> 8;
297: index = encodeIndex[index];
298: if (index < 0) {
299: cb.position(cb.position() - 1);
300: return CoderResult
301: .unmappableForLength(1);
302: }
303: index <<= 8;
304: index += (int) c & 0xFF;
305: if ((byte) arr[index] != 0) {
306: bb.put((byte) arr[index]);
307: } else {
308: cb.position(cb.position() - 1);
309: return CoderResult
310: .unmappableForLength(1);
311: }
312: }
313: bbRemaining--;
314: }
315: }
316: }
317: }
318: return CoderResult.UNDERFLOW;
319: }
320:
321: final static char arr[] = {
322:
323: 0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F, 0x16, 0x05,
324: 0x25, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
325: 0x13, 0x3C, 0x3D, 0x32, 0x26, 0x18, 0x19, 0x3F, 0x27,
326: 0x1C, 0x1D, 0x1E, 0x1F, 0x40, 0x4F, 0x7F, 0x63, 0x67,
327: 0x6C, 0x50, 0x7D, 0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60,
328: 0x4B, 0x61, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6,
329: 0xF7, 0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
330: 0xEC, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8,
331: 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8,
332: 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9,
333: 0xB5, 0x71, 0x9F, 0x5F, 0x6D, 0x51, 0x81, 0x82, 0x83,
334: 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93,
335: 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4,
336: 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0x43, 0xBB, 0x47, 0xDC,
337: 0x07, 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17,
338: 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x09, 0x0A, 0x1B, 0x30,
339: 0x31, 0x1A, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39,
340: 0x3A, 0x3B, 0x04, 0x14, 0x3E, 0xFF, 0x41, 0xAA, 0xB0,
341: 0xB1, 0x00, 0xB2, 0xCC, 0x4A, 0xBD, 0xB4, 0x9A, 0x8A,
342: 0xBA, 0xCA, 0xAF, 0xBC, 0x90, 0x8F, 0xEA, 0xFA, 0xBE,
343: 0xA0, 0xB6, 0xB3, 0x9D, 0xDA, 0x9B, 0x8B, 0xB7, 0xB8,
344: 0xB9, 0xAB, 0x64, 0x65, 0x62, 0x66, 0x7B, 0x5B, 0x9E,
345: 0x68, 0x74, 0xE0, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77,
346: 0xAC, 0x69, 0xED, 0xEE, 0xEB, 0xEF, 0x7C, 0xBF, 0x80,
347: 0xFD, 0xFE, 0xFB, 0xFC, 0xAD, 0xAE, 0x59, 0x44, 0x45,
348: 0x42, 0x46, 0xC0, 0xD0, 0x9C, 0x48, 0x54, 0x79, 0x52,
349: 0x53, 0x58, 0x55, 0x56, 0x57, 0x8C, 0x49, 0xCD, 0xCE,
350: 0xCB, 0xCF, 0x6A, 0xE1, 0x70, 0xDD, 0xDE, 0xDB, 0xA1,
351: 0x8D, 0x8E, 0xDF,
352:
353: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
354: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
355: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
356: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
357: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
358: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
359: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
360: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
361: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
362: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
363: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
364: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
365: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
366: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
367: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
368: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
369: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
370: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
371: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
372: 0x00, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
373: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
374: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
375: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
376: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
377: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
378: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
379: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
380: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
381: 0x00, 0x00, 0x00, 0x00 };
382:
383: final static int[] encodeIndex = { 0, -1, -1, -1, -1, -1, -1,
384: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
385: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1,
386: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
387: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
388: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
389: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
390: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
391: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
392: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
393: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
394: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
395: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
396: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
397: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
398: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
399: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
400: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
401: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
402: }
403: }
|