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 x_IBM1124 extends Charset {
030:
031: public x_IBM1124(String csName, String[] aliases) {
032: super (csName, aliases);
033: }
034:
035: public boolean contains(Charset cs) {
036: return cs.name().equalsIgnoreCase("x-ibm-1124_P100-1996");
037: }
038:
039: public CharsetDecoder newDecoder() {
040: return new Decoder(this );
041: }
042:
043: public CharsetEncoder newEncoder() {
044: return new Encoder(this );
045: }
046:
047: private static final class Decoder extends CharsetDecoder {
048: private Decoder(Charset cs) {
049: super (cs, 1, 1);
050:
051: }
052:
053: public native int nDecode(char[] array, int arrPosition,
054: int remaining, long outAddr, int absolutePos);
055:
056: protected CoderResult decodeLoop(ByteBuffer bb, CharBuffer cb) {
057: int cbRemaining = cb.remaining();
058: if (bb.isDirect() && bb.hasRemaining() && cb.hasArray()) {
059: int toProceed = bb.remaining();
060: int cbPos = cb.position();
061: int bbPos = bb.position();
062: boolean throwOverflow = false;
063: if (cbRemaining < toProceed) {
064: toProceed = cbRemaining;
065: throwOverflow = true;
066: }
067: int res = nDecode(cb.array(), cb.arrayOffset() + cbPos,
068: toProceed, AddressUtil
069: .getDirectBufferAddress(bb), bbPos);
070: bb.position(bbPos + res);
071: cb.position(cbPos + res);
072: if (throwOverflow)
073: return CoderResult.OVERFLOW;
074: } else {
075: if (bb.hasArray() && cb.hasArray()) {
076: int rem = bb.remaining();
077: rem = cbRemaining >= rem ? rem : cbRemaining;
078: byte[] bArr = bb.array();
079: char[] cArr = cb.array();
080: int bStart = bb.position();
081: int cStart = cb.position();
082: int i;
083: for (i = bStart; i < bStart + rem; i++) {
084: int in = (int) bArr[i];
085: if (in < 0 && in >= -95) {
086: int index = (int) in + 95;
087: cArr[cStart++] = (char) arr[index];
088: } else {
089: cArr[cStart++] = (char) (in & 0xFF);
090: }
091: }
092: bb.position(i);
093: cb.position(cStart);
094: if (rem == cbRemaining && bb.hasRemaining())
095: return CoderResult.OVERFLOW;
096: } else {
097: while (bb.hasRemaining()) {
098: if (cbRemaining == 0)
099: return CoderResult.OVERFLOW;
100: int in = (int) bb.get();
101: if (in < 0 && in >= -95) {
102: int index = (int) in + 95;
103: cb.put(arr[index]);
104: } else {
105: cb.put((char) (in & 0xFF));
106: }
107: cbRemaining--;
108: }
109: }
110: }
111: return CoderResult.UNDERFLOW;
112: }
113:
114: final static char[] arr = { 0x0401, 0x0402, 0x0490, 0x0404,
115: 0x0405, 0x0406, 0x0407, 0x0408, 0x0409, 0x040A, 0x040B,
116: 0x040C, 0x00AD, 0x040E, 0x040F, 0x0410, 0x0411, 0x0412,
117: 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419,
118: 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, 0x0420,
119: 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
120: 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E,
121: 0x042F, 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435,
122: 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C,
123: 0x043D, 0x043E, 0x043F, 0x0440, 0x0441, 0x0442, 0x0443,
124: 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A,
125: 0x044B, 0x044C, 0x044D, 0x044E, 0x044F, 0x2116, 0x0451,
126: 0x0452, 0x0491, 0x0454, 0x0455, 0x0456, 0x0457, 0x0458,
127: 0x0459, 0x045A, 0x045B, 0x045C, 0x00A7, 0x045E, 0x045F };
128: }
129:
130: private static final class Encoder extends CharsetEncoder {
131: private Encoder(Charset cs) {
132: super (cs, 1, 1);
133: }
134:
135: private native void nEncode(long outAddr, int absolutePos,
136: char[] array, int arrPosition, int[] res);
137:
138: protected CoderResult encodeLoop(CharBuffer cb, ByteBuffer bb) {
139: int bbRemaining = bb.remaining();
140: if (bb.isDirect() && cb.hasRemaining() && cb.hasArray()) {
141: int toProceed = cb.remaining();
142: int cbPos = cb.position();
143: int bbPos = bb.position();
144: boolean throwOverflow = false;
145: if (bbRemaining < toProceed) {
146: toProceed = bbRemaining;
147: throwOverflow = true;
148: }
149: int[] res = { toProceed, 0 };
150: nEncode(AddressUtil.getDirectBufferAddress(bb), bbPos,
151: cb.array(), cb.arrayOffset() + cbPos, res);
152: if (res[0] <= 0) {
153: bb.position(bbPos - res[0]);
154: cb.position(cbPos - res[0]);
155: if (res[1] != 0) {
156: if (res[1] < 0)
157: return CoderResult
158: .malformedForLength(-res[1]);
159: else
160: return CoderResult
161: .unmappableForLength(res[1]);
162: }
163: } else {
164: bb.position(bbPos + res[0]);
165: cb.position(cbPos + res[0]);
166: if (throwOverflow)
167: return CoderResult.OVERFLOW;
168: }
169: } else {
170: if (bb.hasArray() && cb.hasArray()) {
171: byte[] byteArr = bb.array();
172: char[] charArr = cb.array();
173: int rem = cb.remaining();
174: int byteArrStart = bb.position();
175: rem = bbRemaining <= rem ? bbRemaining : rem;
176: int x;
177: for (x = cb.position(); x < cb.position() + rem; x++) {
178: char c = charArr[x];
179: if (c > (char) 0x2116) {
180: if (c >= 0xD800 && c <= 0xDFFF) {
181: if (x + 1 < cb.limit()) {
182: char c1 = charArr[x + 1];
183: if (c1 >= 0xD800 && c1 <= 0xDFFF) {
184: cb.position(x);
185: bb.position(byteArrStart);
186: return CoderResult
187: .unmappableForLength(2);
188: }
189: } else {
190: cb.position(x);
191: bb.position(byteArrStart);
192: return CoderResult.UNDERFLOW;
193: }
194: cb.position(x);
195: bb.position(byteArrStart);
196: return CoderResult
197: .malformedForLength(1);
198: }
199: cb.position(x);
200: bb.position(byteArrStart);
201: return CoderResult.unmappableForLength(1);
202: } else {
203: if (c < 0xA1) {
204: byteArr[byteArrStart++] = (byte) c;
205: } else {
206: int index = (int) c >> 8;
207: index = encodeIndex[index];
208: if (index < 0) {
209: cb.position(x);
210: bb.position(byteArrStart);
211: return CoderResult
212: .unmappableForLength(1);
213: }
214: index <<= 8;
215: index += (int) c & 0xFF;
216: if ((byte) arr[index] != 0) {
217: byteArr[byteArrStart++] = (byte) arr[index];
218: } else {
219: cb.position(x);
220: bb.position(byteArrStart);
221: return CoderResult
222: .unmappableForLength(1);
223: }
224: }
225: }
226: }
227: cb.position(x);
228: bb.position(byteArrStart);
229: if (rem == bbRemaining && cb.hasRemaining()) {
230: return CoderResult.OVERFLOW;
231: }
232: } else {
233: while (cb.hasRemaining()) {
234: if (bbRemaining == 0)
235: return CoderResult.OVERFLOW;
236: char c = cb.get();
237: if (c > (char) 0x2116) {
238: if (c >= 0xD800 && c <= 0xDFFF) {
239: if (cb.hasRemaining()) {
240: char c1 = cb.get();
241: if (c1 >= 0xD800 && c1 <= 0xDFFF) {
242: cb.position(cb.position() - 2);
243: return CoderResult
244: .unmappableForLength(2);
245: } else {
246: cb.position(cb.position() - 1);
247: }
248: } else {
249: cb.position(cb.position() - 1);
250: return CoderResult.UNDERFLOW;
251: }
252: cb.position(cb.position() - 1);
253: return CoderResult
254: .malformedForLength(1);
255: }
256: cb.position(cb.position() - 1);
257: return CoderResult.unmappableForLength(1);
258: } else {
259: if (c < 0xA1) {
260: bb.put((byte) c);
261: } else {
262: int index = (int) c >> 8;
263: index = encodeIndex[index];
264: if (index < 0) {
265: cb.position(cb.position() - 1);
266: return CoderResult
267: .unmappableForLength(1);
268: }
269: index <<= 8;
270: index += (int) c & 0xFF;
271: if ((byte) arr[index] != 0) {
272: bb.put((byte) arr[index]);
273: } else {
274: cb.position(cb.position() - 1);
275: return CoderResult
276: .unmappableForLength(1);
277: }
278: }
279: bbRemaining--;
280: }
281: }
282: }
283: }
284: return CoderResult.UNDERFLOW;
285: }
286:
287: final static char arr[] = {
288:
289: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
290: 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
291: 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B,
292: 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24,
293: 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D,
294: 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
295: 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
296: 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
297: 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51,
298: 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A,
299: 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63,
300: 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C,
301: 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
302: 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E,
303: 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
304: 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90,
305: 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
306: 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0x00, 0x00,
307: 0x00, 0x00, 0x00, 0x00, 0xFD, 0x00, 0x00, 0x00, 0x00,
308: 0x00, 0xAD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
309: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
311: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
312: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
313: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
314: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
315: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
316: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
317: 0x00, 0x00, 0x00,
318:
319: 0x00, 0xA1, 0xA2, 0x00, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
320: 0xA9, 0xAA, 0xAB, 0xAC, 0x00, 0xAE, 0xAF, 0xB0, 0xB1,
321: 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
322: 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3,
323: 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC,
324: 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5,
325: 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE,
326: 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
327: 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0x00,
328: 0xF1, 0xF2, 0x00, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9,
329: 0xFA, 0xFB, 0xFC, 0x00, 0xFE, 0xFF, 0x00, 0x00, 0x00,
330: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
331: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
332: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
333: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
334: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
335: 0xA3, 0xF3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
336: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
337: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
338: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
339: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
340: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
341: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
342: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
343: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
344: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
346: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
347: 0x00, 0x00, 0x00, 0x00,
348:
349: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
350: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
351: 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00,
352: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
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, 0x00, 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 };
378:
379: final static int[] encodeIndex = { 0, -1, -1, -1, 1, -1, -1,
380: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
381: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, -1,
382: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
383: -1, -1, -1, -1, -1, -1, -1, -1, -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 };
398: }
399: }
|