1/*
2 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
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/*
27 */
28
29
30package sun.nio.cs.ext;
31
32import java.nio.charset.Charset;
33import java.nio.CharBuffer;
34import java.nio.ByteBuffer;
35import java.nio.charset.CharsetDecoder;
36import java.nio.charset.CharsetEncoder;
37import java.nio.charset.CoderResult;
38import sun.nio.cs.Surrogate;
39import sun.nio.cs.HistoricallyNamedCharset;
40
41public class ISCII91 extends Charset implements HistoricallyNamedCharset
42{
43    private static final char NUKTA_CHAR = '\u093c';
44    private static final char HALANT_CHAR = '\u094d';
45    private static final byte NO_CHAR = (byte)255;
46
47    public ISCII91() {
48        super("x-ISCII91", ExtendedCharsets.aliasesFor("x-ISCII91"));
49    }
50
51    public String historicalName() {
52        return "ISCII91";
53    }
54
55    public boolean contains(Charset cs) {
56        return ((cs.name().equals("US-ASCII"))
57                || (cs instanceof ISCII91));
58    }
59
60    public CharsetDecoder newDecoder() {
61        return new Decoder(this);
62    }
63
64    public CharsetEncoder newEncoder() {
65        return new Encoder(this);
66    }
67
68    private static final char[] directMapTable = {
69        '\u0000', // ascii character
70        '\u0001', // ascii character
71        '\u0002', // ascii character
72        '\u0003', // ascii character
73        '\u0004', // ascii character
74        '\u0005', // ascii character
75        '\u0006', // ascii character
76        '\u0007', // ascii character
77        '\u0008', // ascii character
78        '\u0009', // ascii character
79        '\012', // ascii character
80        '\u000b', // ascii character
81        '\u000c', // ascii character
82        '\015', // ascii character
83        '\u000e', // ascii character
84        '\u000f', // ascii character
85        '\u0010', // ascii character
86        '\u0011', // ascii character
87        '\u0012', // ascii character
88        '\u0013', // ascii character
89        '\u0014', // ascii character
90        '\u0015', // ascii character
91        '\u0016', // ascii character
92        '\u0017', // ascii character
93        '\u0018', // ascii character
94        '\u0019', // ascii character
95        '\u001a', // ascii character
96        '\u001b', // ascii character
97        '\u001c', // ascii character
98        '\u001d', // ascii character
99        '\u001e', // ascii character
100        '\u001f', // ascii character
101        '\u0020', // ascii character
102        '\u0021', // ascii character
103        '\u0022', // ascii character
104        '\u0023', // ascii character
105        '\u0024', // ascii character
106        '\u0025', // ascii character
107        '\u0026', // ascii character
108        (char)0x0027, // '\u0027' control -- ascii character
109        '\u0028', // ascii character
110        '\u0029', // ascii character
111        '\u002a', // ascii character
112        '\u002b', // ascii character
113        '\u002c', // ascii character
114        '\u002d', // ascii character
115        '\u002e', // ascii character
116        '\u002f', // ascii character
117        '\u0030', // ascii character
118        '\u0031', // ascii character
119        '\u0032', // ascii character
120        '\u0033', // ascii character
121        '\u0034', // ascii character
122        '\u0035', // ascii character
123        '\u0036', // ascii character
124        '\u0037', // ascii character
125        '\u0038', // ascii character
126        '\u0039', // ascii character
127        '\u003a', // ascii character
128        '\u003b', // ascii character
129        '\u003c', // ascii character
130        '\u003d', // ascii character
131        '\u003e', // ascii character
132        '\u003f', // ascii character
133        '\u0040', // ascii character
134        '\u0041', // ascii character
135        '\u0042', // ascii character
136        '\u0043', // ascii character
137        '\u0044', // ascii character
138        '\u0045', // ascii character
139        '\u0046', // ascii character
140        '\u0047', // ascii character
141        '\u0048', // ascii character
142        '\u0049', // ascii character
143        '\u004a', // ascii character
144        '\u004b', // ascii character
145        '\u004c', // ascii character
146        '\u004d', // ascii character
147        '\u004e', // ascii character
148        '\u004f', // ascii character
149        '\u0050', // ascii character
150        '\u0051', // ascii character
151        '\u0052', // ascii character
152        '\u0053', // ascii character
153        '\u0054', // ascii character
154        '\u0055', // ascii character
155        '\u0056', // ascii character
156        '\u0057', // ascii character
157        '\u0058', // ascii character
158        '\u0059', // ascii character
159        '\u005a', // ascii character
160        '\u005b', // ascii character
161        '\\',// '\u005c' -- ascii character
162        '\u005d', // ascii character
163        '\u005e', // ascii character
164        '\u005f', // ascii character
165        '\u0060', // ascii character
166        '\u0061', // ascii character
167        '\u0062', // ascii character
168        '\u0063', // ascii character
169        '\u0064', // ascii character
170        '\u0065', // ascii character
171        '\u0066', // ascii character
172        '\u0067', // ascii character
173        '\u0068', // ascii character
174        '\u0069', // ascii character
175        '\u006a', // ascii character
176        '\u006b', // ascii character
177        '\u006c', // ascii character
178        '\u006d', // ascii character
179        '\u006e', // ascii character
180        '\u006f', // ascii character
181        '\u0070', // ascii character
182        '\u0071', // ascii character
183        '\u0072', // ascii character
184        '\u0073', // ascii character
185        '\u0074', // ascii character
186        '\u0075', // ascii character
187        '\u0076', // ascii character
188        '\u0077', // ascii character
189        '\u0078', // ascii character
190        '\u0079', // ascii character
191        '\u007a', // ascii character
192        '\u007b', // ascii character
193        '\u007c', // ascii character
194        '\u007d', // ascii character
195        '\u007e', // ascii character
196        '\u007f', // ascii character
197        '\uffff', // unknown character
198        '\uffff', // unknown character
199        '\uffff', // unknown character
200        '\uffff', // unknown character
201        '\uffff', // unknown character
202        '\uffff', // unknown character
203        '\uffff', // unknown character
204        '\uffff', // unknown character
205        '\uffff', // unknown character
206        '\uffff', // unknown character
207        '\uffff', // unknown character
208        '\uffff', // unknown character
209        '\uffff', // unknown character
210        '\uffff', // unknown character
211        '\uffff', // unknown character
212        '\uffff', // unknown character
213        '\uffff', // unknown character
214        '\uffff', // unknown character
215        '\uffff', // unknown character
216        '\uffff', // unknown character
217        '\uffff', // unknown character
218        '\uffff', // unknown character
219        '\uffff', // unknown character
220        '\uffff', // unknown character
221        '\uffff', // unknown character
222        '\uffff', // unknown character
223        '\uffff', // unknown character
224        '\uffff', // unknown character
225        '\uffff', // unknown character
226        '\uffff', // unknown character
227        '\uffff', // unknown character
228        '\uffff', // unknown character
229        '\u0901', // a1 -- Vowel-modifier CHANDRABINDU
230        '\u0902', // a2 -- Vowel-modifier ANUSWAR
231        '\u0903', // a3 -- Vowel-modifier VISARG
232
233        '\u0905', // a4 -- Vowel A
234        '\u0906', // a5 -- Vowel AA
235        '\u0907', // a6 -- Vowel I
236        '\u0908', // a7 -- Vowel II
237        '\u0909', // a8 -- Vowel U
238        '\u090a', // a9 -- Vowel UU
239        '\u090b', // aa -- Vowel RI
240        '\u090e', // ab -- Vowel E ( Southern Scripts )
241        '\u090f', // ac -- Vowel EY
242        '\u0910', // ad -- Vowel AI
243        '\u090d', // ae -- Vowel AYE ( Devanagari Script )
244        '\u0912', // af -- Vowel O ( Southern Scripts )
245        '\u0913', // b0 -- Vowel OW
246        '\u0914', // b1 -- Vowel AU
247        '\u0911', // b2 -- Vowel AWE ( Devanagari Script )
248        '\u0915', // b3 -- Consonant KA
249        '\u0916', // b4 -- Consonant KHA
250        '\u0917', // b5 -- Consonant GA
251        '\u0918', // b6 -- Consonant GHA
252        '\u0919', // b7 -- Consonant NGA
253        '\u091a', // b8 -- Consonant CHA
254        '\u091b', // b9 -- Consonant CHHA
255        '\u091c', // ba -- Consonant JA
256        '\u091d', // bb -- Consonant JHA
257        '\u091e', // bc -- Consonant JNA
258        '\u091f', // bd -- Consonant Hard TA
259        '\u0920', // be -- Consonant Hard THA
260        '\u0921', // bf -- Consonant Hard DA
261        '\u0922', // c0 -- Consonant Hard DHA
262        '\u0923', // c1 -- Consonant Hard NA
263        '\u0924', // c2 -- Consonant Soft TA
264        '\u0925', // c3 -- Consonant Soft THA
265        '\u0926', // c4 -- Consonant Soft DA
266        '\u0927', // c5 -- Consonant Soft DHA
267        '\u0928', // c6 -- Consonant Soft NA
268        '\u0929', // c7 -- Consonant NA ( Tamil )
269        '\u092a', // c8 -- Consonant PA
270        '\u092b', // c9 -- Consonant PHA
271        '\u092c', // ca -- Consonant BA
272        '\u092d', // cb -- Consonant BHA
273        '\u092e', // cc -- Consonant MA
274        '\u092f', // cd -- Consonant YA
275        '\u095f', // ce -- Consonant JYA ( Bengali, Assamese & Oriya )
276        '\u0930', // cf -- Consonant RA
277        '\u0931', // d0 -- Consonant Hard RA ( Southern Scripts )
278        '\u0932', // d1 -- Consonant LA
279        '\u0933', // d2 -- Consonant Hard LA
280        '\u0934', // d3 -- Consonant ZHA ( Tamil & Malayalam )
281        '\u0935', // d4 -- Consonant VA
282        '\u0936', // d5 -- Consonant SHA
283        '\u0937', // d6 -- Consonant Hard SHA
284        '\u0938', // d7 -- Consonant SA
285        '\u0939', // d8 -- Consonant HA
286
287        '\u200d', // d9 -- Consonant INVISIBLE
288        '\u093e', // da -- Vowel Sign AA
289
290        '\u093f', // db -- Vowel Sign I
291        '\u0940', // dc -- Vowel Sign II
292        '\u0941', // dd -- Vowel Sign U
293        '\u0942', // de -- Vowel Sign UU
294        '\u0943', // df -- Vowel Sign RI
295        '\u0946', // e0 -- Vowel Sign E ( Southern Scripts )
296        '\u0947', // e1 -- Vowel Sign EY
297        '\u0948', // e2 -- Vowel Sign AI
298        '\u0945', // e3 -- Vowel Sign AYE ( Devanagari Script )
299        '\u094a', // e4 -- Vowel Sign O ( Southern Scripts )
300        '\u094b', // e5 -- Vowel Sign OW
301        '\u094c', // e6 -- Vowel Sign AU
302        '\u0949', // e7 -- Vowel Sign AWE ( Devanagari Script )
303
304        '\u094d', // e8 -- Vowel Omission Sign ( Halant )
305        '\u093c', // e9 -- Diacritic Sign ( Nukta )
306        '\u0964', // ea -- Full Stop ( Viram, Northern Scripts )
307
308        '\uffff', // eb -- This position shall not be used
309        '\uffff', // ec -- This position shall not be used
310        '\uffff', // ed -- This position shall not be used
311        '\uffff', // ee -- This position shall not be used
312
313        '\ufffd', // ef -- Attribute Code ( ATR )
314        '\ufffd', // f0 -- Extension Code ( EXT )
315
316        '\u0966', // f1 -- Digit 0
317        '\u0967', // f2 -- Digit 1
318        '\u0968', // f3 -- Digit 2
319        '\u0969', // f4 -- Digit 3
320        '\u096a', // f5 -- Digit 4
321        '\u096b', // f6 -- Digit 5
322        '\u096c', // f7 -- Digit 6
323        '\u096d', // f8 -- Digit 7
324        '\u096e', // f9 -- Digit 8
325        '\u096f', // fa -- Digit 9
326
327        '\uffff', // fb -- This position shall not be used
328        '\uffff', // fc -- This position shall not be used
329        '\uffff', // fd -- This position shall not be used
330        '\uffff', // fe -- This position shall not be used
331        '\uffff'  // ff -- This position shall not be used
332    }; //end of table definition
333
334    private static final byte[] encoderMappingTable = {
335    NO_CHAR,NO_CHAR, //0900 <reserved>
336    (byte)161,NO_CHAR, //0901 -- DEVANAGARI SIGN CANDRABINDU = anunasika
337    (byte)162,NO_CHAR, //0902 -- DEVANAGARI SIGN ANUSVARA = bindu
338    (byte)163,NO_CHAR, //0903 -- DEVANAGARI SIGN VISARGA
339    NO_CHAR,NO_CHAR, //0904 <reserved>
340    (byte)164,NO_CHAR, //0905 -- DEVANAGARI LETTER A
341    (byte)165,NO_CHAR, //0906 -- DEVANAGARI LETTER AA
342    (byte)166,NO_CHAR, //0907 -- DEVANAGARI LETTER I
343    (byte)167,NO_CHAR, //0908 -- DEVANAGARI LETTER II
344    (byte)168,NO_CHAR, //0909 -- DEVANAGARI LETTER U
345    (byte)169,NO_CHAR, //090a -- DEVANAGARI LETTER UU
346    (byte)170,NO_CHAR, //090b -- DEVANAGARI LETTER VOCALIC R
347    (byte)166,(byte)233, //090c -- DEVANAGARI LETTER VOVALIC L
348    (byte)174,NO_CHAR, //090d -- DEVANAGARI LETTER CANDRA E
349    (byte)171,NO_CHAR, //090e -- DEVANAGARI LETTER SHORT E
350    (byte)172,NO_CHAR, //090f -- DEVANAGARI LETTER E
351    (byte)173,NO_CHAR, //0910 -- DEVANAGARI LETTER AI
352    (byte)178,NO_CHAR, //0911 -- DEVANAGARI LETTER CANDRA O
353    (byte)175,NO_CHAR, //0912 -- DEVANAGARI LETTER SHORT O
354    (byte)176,NO_CHAR, //0913 -- DEVANAGARI LETTER O
355    (byte)177,NO_CHAR, //0914 -- DEVANAGARI LETTER AU
356    (byte)179,NO_CHAR, //0915 -- DEVANAGARI LETTER KA
357    (byte)180,NO_CHAR, //0916 -- DEVANAGARI LETTER KHA
358    (byte)181,NO_CHAR, //0917 -- DEVANAGARI LETTER GA
359    (byte)182,NO_CHAR, //0918 -- DEVANAGARI LETTER GHA
360    (byte)183,NO_CHAR, //0919 -- DEVANAGARI LETTER NGA
361    (byte)184,NO_CHAR, //091a -- DEVANAGARI LETTER CA
362    (byte)185,NO_CHAR, //091b -- DEVANAGARI LETTER CHA
363    (byte)186,NO_CHAR, //091c -- DEVANAGARI LETTER JA
364    (byte)187,NO_CHAR, //091d -- DEVANAGARI LETTER JHA
365    (byte)188,NO_CHAR, //091e -- DEVANAGARI LETTER NYA
366    (byte)189,NO_CHAR, //091f -- DEVANAGARI LETTER TTA
367    (byte)190,NO_CHAR, //0920 -- DEVANAGARI LETTER TTHA
368    (byte)191,NO_CHAR, //0921 -- DEVANAGARI LETTER DDA
369    (byte)192,NO_CHAR, //0922 -- DEVANAGARI LETTER DDHA
370    (byte)193,NO_CHAR, //0923 -- DEVANAGARI LETTER NNA
371    (byte)194,NO_CHAR, //0924 -- DEVANAGARI LETTER TA
372    (byte)195,NO_CHAR, //0925 -- DEVANAGARI LETTER THA
373    (byte)196,NO_CHAR, //0926 -- DEVANAGARI LETTER DA
374    (byte)197,NO_CHAR, //0927 -- DEVANAGARI LETTER DHA
375    (byte)198,NO_CHAR, //0928 -- DEVANAGARI LETTER NA
376    (byte)199,NO_CHAR, //0929 -- DEVANAGARI LETTER NNNA <=> 0928 + 093C
377    (byte)200,NO_CHAR, //092a -- DEVANAGARI LETTER PA
378    (byte)201,NO_CHAR, //092b -- DEVANAGARI LETTER PHA
379    (byte)202,NO_CHAR, //092c -- DEVANAGARI LETTER BA
380    (byte)203,NO_CHAR, //092d -- DEVANAGARI LETTER BHA
381    (byte)204,NO_CHAR, //092e -- DEVANAGARI LETTER MA
382    (byte)205,NO_CHAR, //092f -- DEVANAGARI LETTER YA
383    (byte)207,NO_CHAR, //0930 -- DEVANAGARI LETTER RA
384    (byte)208,NO_CHAR, //0931 -- DEVANAGARI LETTER RRA <=> 0930 + 093C
385    (byte)209,NO_CHAR, //0932 -- DEVANAGARI LETTER LA
386    (byte)210,NO_CHAR, //0933 -- DEVANAGARI LETTER LLA
387    (byte)211,NO_CHAR, //0934 -- DEVANAGARI LETTER LLLA <=> 0933 + 093C
388    (byte)212,NO_CHAR, //0935 -- DEVANAGARI LETTER VA
389    (byte)213,NO_CHAR, //0936 -- DEVANAGARI LETTER SHA
390    (byte)214,NO_CHAR, //0937 -- DEVANAGARI LETTER SSA
391    (byte)215,NO_CHAR, //0938 -- DEVANAGARI LETTER SA
392    (byte)216,NO_CHAR, //0939 -- DEVANAGARI LETTER HA
393    NO_CHAR,NO_CHAR, //093a <reserved>
394    NO_CHAR,NO_CHAR, //093b <reserved>
395    (byte)233,NO_CHAR, //093c -- DEVANAGARI SIGN NUKTA
396    (byte)234,(byte)233, //093d -- DEVANAGARI SIGN AVAGRAHA
397    (byte)218,NO_CHAR, //093e -- DEVANAGARI VOWEL SIGN AA
398    (byte)219,NO_CHAR, //093f -- DEVANAGARI VOWEL SIGN I
399    (byte)220,NO_CHAR, //0940 -- DEVANAGARI VOWEL SIGN II
400    (byte)221,NO_CHAR, //0941 -- DEVANAGARI VOWEL SIGN U
401    (byte)222,NO_CHAR, //0942 -- DEVANAGARI VOWEL SIGN UU
402    (byte)223,NO_CHAR, //0943 -- DEVANAGARI VOWEL SIGN VOCALIC R
403    (byte)223,(byte)233, //0944 -- DEVANAGARI VOWEL SIGN VOCALIC RR
404    (byte)227,NO_CHAR, //0945 -- DEVANAGARI VOWEL SIGN CANDRA E
405    (byte)224,NO_CHAR, //0946 -- DEVANAGARI VOWEL SIGN SHORT E
406    (byte)225,NO_CHAR, //0947 -- DEVANAGARI VOWEL SIGN E
407    (byte)226,NO_CHAR, //0948 -- DEVANAGARI VOWEL SIGN AI
408    (byte)231,NO_CHAR, //0949 -- DEVANAGARI VOWEL SIGN CANDRA O
409    (byte)228,NO_CHAR, //094a -- DEVANAGARI VOWEL SIGN SHORT O
410    (byte)229,NO_CHAR, //094b -- DEVANAGARI VOWEL SIGN O
411    (byte)230,NO_CHAR, //094c -- DEVANAGARI VOWEL SIGN AU
412    (byte)232,NO_CHAR, //094d -- DEVANAGARI SIGN VIRAMA ( halant )
413    NO_CHAR,NO_CHAR, //094e <reserved>
414    NO_CHAR,NO_CHAR, //094f <reserved>
415    (byte)161,(byte)233, //0950 -- DEVANAGARI OM
416    (byte)240,(byte)181, //0951 -- DEVANAGARI STRESS SIGN UDATTA
417    (byte)240,(byte)184, //0952 -- DEVANAGARI STRESS SIGN ANUDATTA
418    (byte)254,NO_CHAR, //0953 -- DEVANAGARI GRAVE ACCENT || MISSING
419    (byte)254,NO_CHAR, //0954 -- DEVANAGARI ACUTE ACCENT || MISSING
420    NO_CHAR,NO_CHAR, //0955 <reserved>
421    NO_CHAR,NO_CHAR, //0956 <reserved>
422    NO_CHAR,NO_CHAR, //0957 <reserved>
423    (byte)179,(byte)233, //0958 -- DEVANAGARI LETTER QA <=> 0915 + 093C
424    (byte)180,(byte)233, //0959 -- DEVANAGARI LETTER KHHA <=> 0916 + 093C
425    (byte)181,(byte)233, //095a -- DEVANAGARI LETTER GHHA <=> 0917 + 093C
426    (byte)186,(byte)233, //095b -- DEVANAGARI LETTER ZA <=> 091C + 093C
427    (byte)191,(byte)233, //095c -- DEVANAGARI LETTER DDDHA <=> 0921 + 093C
428    (byte)192,(byte)233, //095d -- DEVANAGARI LETTER RHA <=> 0922 + 093C
429    (byte)201,(byte)233, //095e -- DEVANAGARI LETTER FA <=> 092B + 093C
430    (byte)206,NO_CHAR, //095f -- DEVANAGARI LETTER YYA <=> 092F + 093C
431    (byte)170,(byte)233, //0960 -- DEVANAGARI LETTER VOCALIC RR
432    (byte)167,(byte)233, //0961 -- DEVANAGARI LETTER VOCALIC LL
433    (byte)219,(byte)233, //0962 -- DEVANAGARI VOWEL SIGN VOCALIC L
434    (byte)220,(byte)233, //0963 -- DEVANAGARI VOWEL SIGN VOCALIC LL
435    (byte)234,NO_CHAR, //0964 -- DEVANAGARI DANDA ( phrase separator )
436    (byte)234,(byte)234, //0965 -- DEVANAGARI DOUBLE DANDA
437    (byte)241,NO_CHAR, //0966 -- DEVANAGARI DIGIT ZERO
438    (byte)242,NO_CHAR, //0967 -- DEVANAGARI DIGIT ONE
439    (byte)243,NO_CHAR, //0968 -- DEVANAGARI DIGIT TWO
440    (byte)244,NO_CHAR, //0969 -- DEVANAGARI DIGIT THREE
441    (byte)245,NO_CHAR, //096a -- DEVANAGARI DIGIT FOUR
442    (byte)246,NO_CHAR, //096b -- DEVANAGARI DIGIT FIVE
443    (byte)247,NO_CHAR, //096c -- DEVANAGARI DIGIT SIX
444    (byte)248,NO_CHAR, //096d -- DEVANAGARI DIGIT SEVEN
445    (byte)249,NO_CHAR, //096e -- DEVANAGARI DIGIT EIGHT
446    (byte)250,NO_CHAR, //096f -- DEVANAGARI DIGIT NINE
447    (byte)240,(byte)191,  //0970 -- DEVANAGARI ABBREVIATION SIGN
448    NO_CHAR,NO_CHAR, //0971 -- reserved
449    NO_CHAR,NO_CHAR, //0972 -- reserved
450    NO_CHAR,NO_CHAR, //0973 -- reserved
451    NO_CHAR,NO_CHAR, //0974 -- reserved
452    NO_CHAR,NO_CHAR, //0975 -- reserved
453    NO_CHAR,NO_CHAR, //0976 -- reserved
454    NO_CHAR,NO_CHAR, //0977 -- reserved
455    NO_CHAR,NO_CHAR, //0978 -- reserved
456    NO_CHAR,NO_CHAR, //0979 -- reserved
457    NO_CHAR,NO_CHAR, //097a -- reserved
458    NO_CHAR,NO_CHAR, //097b -- reserved
459    NO_CHAR,NO_CHAR, //097c -- reserved
460    NO_CHAR,NO_CHAR, //097d -- reserved
461    NO_CHAR,NO_CHAR, //097e -- reserved
462    NO_CHAR,NO_CHAR  //097f -- reserved
463    }; //end of table definition
464
465    private static class Decoder extends CharsetDecoder {
466
467        private static final char ZWNJ_CHAR = '\u200c';
468        private static final char ZWJ_CHAR = '\u200d';
469        private static final char INVALID_CHAR = '\uffff';
470
471        private char contextChar = INVALID_CHAR;
472        private boolean needFlushing = false;
473
474
475        private Decoder(Charset cs) {
476            super(cs, 1.0f, 1.0f);
477        }
478
479        protected CoderResult implFlush(CharBuffer out) {
480            if(needFlushing) {
481                if (out.remaining() < 1) {
482                    return CoderResult.OVERFLOW;
483                } else {
484                    out.put(contextChar);
485                }
486            }
487            contextChar = INVALID_CHAR;
488            needFlushing = false;
489            return CoderResult.UNDERFLOW;
490        }
491
492        /*Rules:
493         * 1)ATR,EXT,following character to be replaced with '\ufffd'
494         * 2)Halant + Halant => '\u094d' (Virama) + '\u200c'(ZWNJ)
495         * 3)Halant + Nukta => '\u094d' (Virama) + '\u200d'(ZWJ)
496        */
497        private CoderResult decodeArrayLoop(ByteBuffer src,
498                                             CharBuffer dst)
499        {
500            byte[] sa = src.array();
501            int sp = src.arrayOffset() + src.position();
502            int sl = src.arrayOffset() + src.limit();
503            assert (sp <= sl);
504            sp = (sp <= sl ? sp : sl);
505
506            char[] da = dst.array();
507            int dp = dst.arrayOffset() + dst.position();
508            int dl = dst.arrayOffset() + dst.limit();
509            assert (dp <= dl);
510            dp = (dp <= dl ? dp : dl);
511
512            try {
513                while (sp < sl) {
514                    int index = sa[sp];
515                    index = ( index < 0 )? ( index + 255 ):index;
516                    char currentChar = directMapTable[index];
517
518                    // if the contextChar is either ATR || EXT
519                    // set the output to '\ufffd'
520                    if(contextChar == '\ufffd') {
521                        if (dl - dp < 1)
522                            return CoderResult.OVERFLOW;
523                        da[dp++] = '\ufffd';
524                        contextChar = INVALID_CHAR;
525                        needFlushing = false;
526                        sp++;
527                        continue;
528                    }
529
530                    switch(currentChar) {
531                    case '\u0901':
532                    case '\u0907':
533                    case '\u0908':
534                    case '\u090b':
535                    case '\u093f':
536                    case '\u0940':
537                    case '\u0943':
538                    case '\u0964':
539                        if(needFlushing) {
540                            if (dl - dp < 1)
541                                return CoderResult.OVERFLOW;
542                            da[dp++] = contextChar;
543                            contextChar = currentChar;
544                            sp++;
545                            continue;
546                        }
547                        contextChar = currentChar;
548                        needFlushing = true;
549                        sp++;
550                        continue;
551                    case NUKTA_CHAR:
552                        if (dl - dp < 1)
553                                return CoderResult.OVERFLOW;
554                        switch(contextChar) {
555                        case '\u0901':
556                            da[dp++] = '\u0950';
557                            break;
558                        case '\u0907':
559                            da[dp++] = '\u090c';
560                            break;
561                        case '\u0908':
562                            da[dp++] = '\u0961';
563                            break;
564                        case '\u090b':
565                            da[dp++] = '\u0960';
566                            break;
567                        case '\u093f':
568                            da[dp++] = '\u0962';
569                            break;
570                        case '\u0940':
571                            da[dp++] = '\u0963';
572                            break;
573                        case '\u0943':
574                            da[dp++] = '\u0944';
575                            break;
576                        case '\u0964':
577                            da[dp++] = '\u093d';
578                            break;
579                        case HALANT_CHAR:
580                            if(needFlushing) {
581                                da[dp++] = contextChar;
582                                contextChar = currentChar;
583                                sp++;
584                                continue;
585                            }
586                            da[dp++] = ZWJ_CHAR;
587                            break;
588                        default:
589                            if(needFlushing) {
590                                da[dp++] = contextChar;
591                                contextChar = currentChar;
592                                sp++;
593                                continue;
594                            }
595                            da[dp++] = NUKTA_CHAR;
596                        }
597                        break;
598                    case HALANT_CHAR:
599                        if (dl - dp < 1)
600                            return CoderResult.OVERFLOW;
601                        if(needFlushing) {
602                            da[dp++] = contextChar;
603                            contextChar = currentChar;
604                            sp++;
605                            continue;
606                        }
607                        if(contextChar == HALANT_CHAR) {
608                            da[dp++] = ZWNJ_CHAR;
609                            break;
610                        }
611                        da[dp++] = HALANT_CHAR;
612                        break;
613                    case INVALID_CHAR:
614                        if(needFlushing) {
615                            if (dl - dp < 1)
616                                return CoderResult.OVERFLOW;
617                            da[dp++] = contextChar;
618                            contextChar = currentChar;
619                            sp++;
620                            continue;
621                        }
622                        return CoderResult.unmappableForLength(1);
623                    default:
624                        if (dl - dp < 1)
625                            return CoderResult.OVERFLOW;
626                        if(needFlushing) {
627                            da[dp++] = contextChar;
628                            contextChar = currentChar;
629                            sp++;
630                            continue;
631                        }
632                        da[dp++] = currentChar;
633                        break;
634                    }//end switch
635
636                contextChar = currentChar;
637                needFlushing = false;
638                sp++;
639            }
640            return CoderResult.UNDERFLOW;
641           } finally {
642                src.position(sp - src.arrayOffset());
643                dst.position(dp - dst.arrayOffset());
644           }
645        }
646
647        private CoderResult decodeBufferLoop(ByteBuffer src,
648                                             CharBuffer dst)
649        {
650            int mark = src.position();
651
652            try {
653                while (src.hasRemaining()) {
654                    int index = src.get();
655                    index = ( index < 0 )? ( index + 255 ):index;
656                    char currentChar = directMapTable[index];
657
658                    // if the contextChar is either ATR || EXT
659                    // set the output to '\ufffd'
660                    if(contextChar == '\ufffd') {
661                        if (dst.remaining() < 1)
662                            return CoderResult.OVERFLOW;
663                        dst.put('\ufffd');
664                        contextChar = INVALID_CHAR;
665                        needFlushing = false;
666                        mark++;
667                        continue;
668                    }
669
670                    switch(currentChar) {
671                    case '\u0901':
672                    case '\u0907':
673                    case '\u0908':
674                    case '\u090b':
675                    case '\u093f':
676                    case '\u0940':
677                    case '\u0943':
678                    case '\u0964':
679                        if(needFlushing) {
680                            if (dst.remaining() < 1)
681                                return CoderResult.OVERFLOW;
682                            dst.put(contextChar);
683                            contextChar = currentChar;
684                            mark++;
685                            continue;
686                        }
687                        contextChar = currentChar;
688                        needFlushing = true;
689                        mark++;
690                        continue;
691                    case NUKTA_CHAR:
692                        if (dst.remaining() < 1)
693                            return CoderResult.OVERFLOW;
694                        switch(contextChar) {
695                        case '\u0901':
696                            dst.put('\u0950');
697                            break;
698                        case '\u0907':
699                            dst.put('\u090c');
700                            break;
701                        case '\u0908':
702                            dst.put('\u0961');
703                            break;
704                        case '\u090b':
705                            dst.put('\u0960');
706                            break;
707                        case '\u093f':
708                            dst.put('\u0962');
709                            break;
710                        case '\u0940':
711                            dst.put('\u0963');
712                            break;
713                        case '\u0943':
714                            dst.put('\u0944');
715                            break;
716                        case '\u0964':
717                            dst.put('\u093d');
718                            break;
719                        case HALANT_CHAR:
720                            if(needFlushing) {
721                                dst.put(contextChar);
722                                contextChar = currentChar;
723                                mark++;
724                                continue;
725                            }
726                            dst.put(ZWJ_CHAR);
727                            break;
728                        default:
729                            if(needFlushing) {
730                                dst.put(contextChar);
731                                contextChar = currentChar;
732                                mark++;
733                                continue;
734                            }
735                            dst.put(NUKTA_CHAR);
736                        }
737                        break;
738                    case HALANT_CHAR:
739                        if (dst.remaining() < 1)
740                            return CoderResult.OVERFLOW;
741                        if(needFlushing) {
742                            dst.put(contextChar);
743                            contextChar = currentChar;
744                            mark++;
745                            continue;
746                        }
747                        if(contextChar == HALANT_CHAR) {
748                            dst.put(ZWNJ_CHAR);
749                            break;
750                        }
751                        dst.put(HALANT_CHAR);
752                        break;
753                    case INVALID_CHAR:
754                        if(needFlushing) {
755                            if (dst.remaining() < 1)
756                                return CoderResult.OVERFLOW;
757                            dst.put(contextChar);
758                            contextChar = currentChar;
759                            mark++;
760                            continue;
761                        }
762                        return CoderResult.unmappableForLength(1);
763                    default:
764                        if (dst.remaining() < 1)
765                            return CoderResult.OVERFLOW;
766                        if(needFlushing) {
767                            dst.put(contextChar);
768                            contextChar = currentChar;
769                            mark++;
770                            continue;
771                        }
772                        dst.put(currentChar);
773                        break;
774                    }//end switch
775                contextChar = currentChar;
776                needFlushing = false;
777                mark++;
778                }
779            return CoderResult.UNDERFLOW;
780            } finally {
781                src.position(mark);
782           }
783        }
784
785        protected CoderResult decodeLoop(ByteBuffer src,
786                                         CharBuffer dst)
787        {
788            if (src.hasArray() && dst.hasArray())
789                return decodeArrayLoop(src, dst);
790            else
791                return decodeBufferLoop(src, dst);
792        }
793    }
794
795    private static class Encoder extends CharsetEncoder {
796
797        private static final byte NO_CHAR = (byte)255;
798
799        //private static CharToByteISCII91 c2b = new CharToByteISCII91();
800        //private static final byte[] directMapTable = c2b.getISCIIEncoderMap();
801
802        private final Surrogate.Parser sgp = new Surrogate.Parser();
803
804        private Encoder(Charset cs) {
805            super(cs, 2.0f, 2.0f);
806        }
807
808        public boolean canEncode(char ch) {
809            //check for Devanagari range,ZWJ,ZWNJ and ASCII range.
810            return ((ch >= '\u0900' && ch <= '\u097f' &&
811                     encoderMappingTable[2*(ch-'\u0900')] != NO_CHAR) ||
812                    (ch == '\u200d') ||
813                    (ch == '\u200c') ||
814                    (ch <= '\u007f'));
815        }
816
817
818        private CoderResult encodeArrayLoop(CharBuffer src,
819                                             ByteBuffer dst)
820        {
821            char[] sa = src.array();
822            int sp = src.arrayOffset() + src.position();
823            int sl = src.arrayOffset() + src.limit();
824            assert (sp <= sl);
825            sp = (sp <= sl ? sp : sl);
826            byte[] da = dst.array();
827            int dp = dst.arrayOffset() + dst.position();
828            int dl = dst.arrayOffset() + dst.limit();
829            assert (dp <= dl);
830            dp = (dp <= dl ? dp : dl);
831
832            int outputSize = 0;
833
834            try {
835                char inputChar;
836                while (sp < sl) {
837                    int index = Integer.MIN_VALUE;
838                    inputChar = sa[sp];
839
840                    if (inputChar >= 0x0000 && inputChar <= 0x007f) {
841                        if (dl - dp < 1)
842                            return CoderResult.OVERFLOW;
843                        da[dp++] = (byte) inputChar;
844                        sp++;
845                        continue;
846                    }
847
848                    // if inputChar == ZWJ replace it with halant
849                    // if inputChar == ZWNJ replace it with Nukta
850
851                    if (inputChar == 0x200c) {
852                        inputChar = HALANT_CHAR;
853                    }
854                    else if (inputChar == 0x200d) {
855                        inputChar = NUKTA_CHAR;
856                    }
857
858                    if (inputChar >= 0x0900 && inputChar <= 0x097f) {
859                        index = ((int)(inputChar) - 0x0900)*2;
860                    }
861
862                    if (Character.isSurrogate(inputChar)) {
863                        if (sgp.parse(inputChar, sa, sp, sl) < 0)
864                            return sgp.error();
865                        return sgp.unmappableResult();
866                    }
867
868                    if (index == Integer.MIN_VALUE ||
869                        encoderMappingTable[index] == NO_CHAR) {
870                        return CoderResult.unmappableForLength(1);
871                    } else {
872                        if(encoderMappingTable[index + 1] == NO_CHAR) {
873                            if(dl - dp < 1)
874                                return CoderResult.OVERFLOW;
875                            da[dp++] = encoderMappingTable[index];
876                        } else {
877                            if(dl - dp < 2)
878                                return CoderResult.OVERFLOW;
879                            da[dp++] = encoderMappingTable[index];
880                            da[dp++] = encoderMappingTable[index + 1];
881                        }
882                        sp++;
883                    }
884                }
885                return CoderResult.UNDERFLOW;
886            } finally {
887                src.position(sp - src.arrayOffset());
888                dst.position(dp - dst.arrayOffset());
889            }
890        }
891
892        private CoderResult encodeBufferLoop(CharBuffer src,
893                                             ByteBuffer dst)
894        {
895            int mark = src.position();
896
897            try {
898                char inputChar;
899                while (src.hasRemaining()) {
900                    int index = Integer.MIN_VALUE;
901                    inputChar = src.get();
902
903                    if (inputChar >= 0x0000 && inputChar <= 0x007f) {
904                        if (dst.remaining() < 1)
905                            return CoderResult.OVERFLOW;
906                        dst.put((byte) inputChar);
907                        mark++;
908                        continue;
909                    }
910
911                    // if inputChar == ZWJ replace it with halant
912                    // if inputChar == ZWNJ replace it with Nukta
913
914                    if (inputChar == 0x200c) {
915                        inputChar = HALANT_CHAR;
916                    }
917                    else if (inputChar == 0x200d) {
918                        inputChar = NUKTA_CHAR;
919                    }
920
921                    if (inputChar >= 0x0900 && inputChar <= 0x097f) {
922                        index = ((int)(inputChar) - 0x0900)*2;
923                    }
924
925                    if (Character.isSurrogate(inputChar)) {
926                        if (sgp.parse(inputChar, src) < 0)
927                            return sgp.error();
928                        return sgp.unmappableResult();
929                    }
930
931                    if (index == Integer.MIN_VALUE ||
932                        encoderMappingTable[index] == NO_CHAR) {
933                        return CoderResult.unmappableForLength(1);
934                    } else {
935                        if(encoderMappingTable[index + 1] == NO_CHAR) {
936                            if(dst.remaining() < 1)
937                                return CoderResult.OVERFLOW;
938                            dst.put(encoderMappingTable[index]);
939                        } else {
940                            if(dst.remaining() < 2)
941                                return CoderResult.OVERFLOW;
942                            dst.put(encoderMappingTable[index]);
943                            dst.put(encoderMappingTable[index + 1]);
944                        }
945                    }
946                    mark++;
947                }
948                return CoderResult.UNDERFLOW;
949            } finally {
950                src.position(mark);
951            }
952        }
953
954        protected CoderResult encodeLoop(CharBuffer src,
955                                         ByteBuffer dst)
956        {
957            if (src.hasArray() && dst.hasArray())
958                return encodeArrayLoop(src, dst);
959            else
960                return encodeBufferLoop(src, dst);
961        }
962    }
963}
964