Path: blob/master/src/jdk.charsets/share/classes/sun/nio/cs/ext/ISCII91.java
41161 views
/*1* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*/272829package sun.nio.cs.ext;3031import java.nio.charset.Charset;32import java.nio.CharBuffer;33import java.nio.ByteBuffer;34import java.nio.charset.CharsetDecoder;35import java.nio.charset.CharsetEncoder;36import java.nio.charset.CoderResult;37import sun.nio.cs.Surrogate;38import sun.nio.cs.HistoricallyNamedCharset;3940public class ISCII91 extends Charset implements HistoricallyNamedCharset41{42private static final char NUKTA_CHAR = '\u093c';43private static final char HALANT_CHAR = '\u094d';44private static final byte NO_CHAR = (byte)255;4546public ISCII91() {47super("x-ISCII91", ExtendedCharsets.aliasesFor("x-ISCII91"));48}4950public String historicalName() {51return "ISCII91";52}5354public boolean contains(Charset cs) {55return ((cs.name().equals("US-ASCII"))56|| (cs instanceof ISCII91));57}5859public CharsetDecoder newDecoder() {60return new Decoder(this);61}6263public CharsetEncoder newEncoder() {64return new Encoder(this);65}6667private static final char[] directMapTable = {68'\u0000', // ascii character69'\u0001', // ascii character70'\u0002', // ascii character71'\u0003', // ascii character72'\u0004', // ascii character73'\u0005', // ascii character74'\u0006', // ascii character75'\u0007', // ascii character76'\u0008', // ascii character77'\u0009', // ascii character78'\012', // ascii character79'\u000b', // ascii character80'\u000c', // ascii character81'\015', // ascii character82'\u000e', // ascii character83'\u000f', // ascii character84'\u0010', // ascii character85'\u0011', // ascii character86'\u0012', // ascii character87'\u0013', // ascii character88'\u0014', // ascii character89'\u0015', // ascii character90'\u0016', // ascii character91'\u0017', // ascii character92'\u0018', // ascii character93'\u0019', // ascii character94'\u001a', // ascii character95'\u001b', // ascii character96'\u001c', // ascii character97'\u001d', // ascii character98'\u001e', // ascii character99'\u001f', // ascii character100'\u0020', // ascii character101'\u0021', // ascii character102'\u0022', // ascii character103'\u0023', // ascii character104'\u0024', // ascii character105'\u0025', // ascii character106'\u0026', // ascii character107(char)0x0027, // '\u0027' control -- ascii character108'\u0028', // ascii character109'\u0029', // ascii character110'\u002a', // ascii character111'\u002b', // ascii character112'\u002c', // ascii character113'\u002d', // ascii character114'\u002e', // ascii character115'\u002f', // ascii character116'\u0030', // ascii character117'\u0031', // ascii character118'\u0032', // ascii character119'\u0033', // ascii character120'\u0034', // ascii character121'\u0035', // ascii character122'\u0036', // ascii character123'\u0037', // ascii character124'\u0038', // ascii character125'\u0039', // ascii character126'\u003a', // ascii character127'\u003b', // ascii character128'\u003c', // ascii character129'\u003d', // ascii character130'\u003e', // ascii character131'\u003f', // ascii character132'\u0040', // ascii character133'\u0041', // ascii character134'\u0042', // ascii character135'\u0043', // ascii character136'\u0044', // ascii character137'\u0045', // ascii character138'\u0046', // ascii character139'\u0047', // ascii character140'\u0048', // ascii character141'\u0049', // ascii character142'\u004a', // ascii character143'\u004b', // ascii character144'\u004c', // ascii character145'\u004d', // ascii character146'\u004e', // ascii character147'\u004f', // ascii character148'\u0050', // ascii character149'\u0051', // ascii character150'\u0052', // ascii character151'\u0053', // ascii character152'\u0054', // ascii character153'\u0055', // ascii character154'\u0056', // ascii character155'\u0057', // ascii character156'\u0058', // ascii character157'\u0059', // ascii character158'\u005a', // ascii character159'\u005b', // ascii character160'\\',// '\u005c' -- ascii character161'\u005d', // ascii character162'\u005e', // ascii character163'\u005f', // ascii character164'\u0060', // ascii character165'\u0061', // ascii character166'\u0062', // ascii character167'\u0063', // ascii character168'\u0064', // ascii character169'\u0065', // ascii character170'\u0066', // ascii character171'\u0067', // ascii character172'\u0068', // ascii character173'\u0069', // ascii character174'\u006a', // ascii character175'\u006b', // ascii character176'\u006c', // ascii character177'\u006d', // ascii character178'\u006e', // ascii character179'\u006f', // ascii character180'\u0070', // ascii character181'\u0071', // ascii character182'\u0072', // ascii character183'\u0073', // ascii character184'\u0074', // ascii character185'\u0075', // ascii character186'\u0076', // ascii character187'\u0077', // ascii character188'\u0078', // ascii character189'\u0079', // ascii character190'\u007a', // ascii character191'\u007b', // ascii character192'\u007c', // ascii character193'\u007d', // ascii character194'\u007e', // ascii character195'\u007f', // ascii character196'\uffff', // unknown character197'\uffff', // unknown character198'\uffff', // unknown character199'\uffff', // unknown character200'\uffff', // unknown character201'\uffff', // unknown character202'\uffff', // unknown character203'\uffff', // unknown character204'\uffff', // unknown character205'\uffff', // unknown character206'\uffff', // unknown character207'\uffff', // unknown character208'\uffff', // unknown character209'\uffff', // unknown character210'\uffff', // unknown character211'\uffff', // unknown character212'\uffff', // unknown character213'\uffff', // unknown character214'\uffff', // unknown character215'\uffff', // unknown character216'\uffff', // unknown character217'\uffff', // unknown character218'\uffff', // unknown character219'\uffff', // unknown character220'\uffff', // unknown character221'\uffff', // unknown character222'\uffff', // unknown character223'\uffff', // unknown character224'\uffff', // unknown character225'\uffff', // unknown character226'\uffff', // unknown character227'\uffff', // unknown character228'\u0901', // a1 -- Vowel-modifier CHANDRABINDU229'\u0902', // a2 -- Vowel-modifier ANUSWAR230'\u0903', // a3 -- Vowel-modifier VISARG231232'\u0905', // a4 -- Vowel A233'\u0906', // a5 -- Vowel AA234'\u0907', // a6 -- Vowel I235'\u0908', // a7 -- Vowel II236'\u0909', // a8 -- Vowel U237'\u090a', // a9 -- Vowel UU238'\u090b', // aa -- Vowel RI239'\u090e', // ab -- Vowel E ( Southern Scripts )240'\u090f', // ac -- Vowel EY241'\u0910', // ad -- Vowel AI242'\u090d', // ae -- Vowel AYE ( Devanagari Script )243'\u0912', // af -- Vowel O ( Southern Scripts )244'\u0913', // b0 -- Vowel OW245'\u0914', // b1 -- Vowel AU246'\u0911', // b2 -- Vowel AWE ( Devanagari Script )247'\u0915', // b3 -- Consonant KA248'\u0916', // b4 -- Consonant KHA249'\u0917', // b5 -- Consonant GA250'\u0918', // b6 -- Consonant GHA251'\u0919', // b7 -- Consonant NGA252'\u091a', // b8 -- Consonant CHA253'\u091b', // b9 -- Consonant CHHA254'\u091c', // ba -- Consonant JA255'\u091d', // bb -- Consonant JHA256'\u091e', // bc -- Consonant JNA257'\u091f', // bd -- Consonant Hard TA258'\u0920', // be -- Consonant Hard THA259'\u0921', // bf -- Consonant Hard DA260'\u0922', // c0 -- Consonant Hard DHA261'\u0923', // c1 -- Consonant Hard NA262'\u0924', // c2 -- Consonant Soft TA263'\u0925', // c3 -- Consonant Soft THA264'\u0926', // c4 -- Consonant Soft DA265'\u0927', // c5 -- Consonant Soft DHA266'\u0928', // c6 -- Consonant Soft NA267'\u0929', // c7 -- Consonant NA ( Tamil )268'\u092a', // c8 -- Consonant PA269'\u092b', // c9 -- Consonant PHA270'\u092c', // ca -- Consonant BA271'\u092d', // cb -- Consonant BHA272'\u092e', // cc -- Consonant MA273'\u092f', // cd -- Consonant YA274'\u095f', // ce -- Consonant JYA ( Bengali, Assamese & Oriya )275'\u0930', // cf -- Consonant RA276'\u0931', // d0 -- Consonant Hard RA ( Southern Scripts )277'\u0932', // d1 -- Consonant LA278'\u0933', // d2 -- Consonant Hard LA279'\u0934', // d3 -- Consonant ZHA ( Tamil & Malayalam )280'\u0935', // d4 -- Consonant VA281'\u0936', // d5 -- Consonant SHA282'\u0937', // d6 -- Consonant Hard SHA283'\u0938', // d7 -- Consonant SA284'\u0939', // d8 -- Consonant HA285286'\u200d', // d9 -- Consonant INVISIBLE287'\u093e', // da -- Vowel Sign AA288289'\u093f', // db -- Vowel Sign I290'\u0940', // dc -- Vowel Sign II291'\u0941', // dd -- Vowel Sign U292'\u0942', // de -- Vowel Sign UU293'\u0943', // df -- Vowel Sign RI294'\u0946', // e0 -- Vowel Sign E ( Southern Scripts )295'\u0947', // e1 -- Vowel Sign EY296'\u0948', // e2 -- Vowel Sign AI297'\u0945', // e3 -- Vowel Sign AYE ( Devanagari Script )298'\u094a', // e4 -- Vowel Sign O ( Southern Scripts )299'\u094b', // e5 -- Vowel Sign OW300'\u094c', // e6 -- Vowel Sign AU301'\u0949', // e7 -- Vowel Sign AWE ( Devanagari Script )302303'\u094d', // e8 -- Vowel Omission Sign ( Halant )304'\u093c', // e9 -- Diacritic Sign ( Nukta )305'\u0964', // ea -- Full Stop ( Viram, Northern Scripts )306307'\uffff', // eb -- This position shall not be used308'\uffff', // ec -- This position shall not be used309'\uffff', // ed -- This position shall not be used310'\uffff', // ee -- This position shall not be used311312'\ufffd', // ef -- Attribute Code ( ATR )313'\ufffd', // f0 -- Extension Code ( EXT )314315'\u0966', // f1 -- Digit 0316'\u0967', // f2 -- Digit 1317'\u0968', // f3 -- Digit 2318'\u0969', // f4 -- Digit 3319'\u096a', // f5 -- Digit 4320'\u096b', // f6 -- Digit 5321'\u096c', // f7 -- Digit 6322'\u096d', // f8 -- Digit 7323'\u096e', // f9 -- Digit 8324'\u096f', // fa -- Digit 9325326'\uffff', // fb -- This position shall not be used327'\uffff', // fc -- This position shall not be used328'\uffff', // fd -- This position shall not be used329'\uffff', // fe -- This position shall not be used330'\uffff' // ff -- This position shall not be used331}; //end of table definition332333private static final byte[] encoderMappingTable = {334NO_CHAR,NO_CHAR, //0900 <reserved>335(byte)161,NO_CHAR, //0901 -- DEVANAGARI SIGN CANDRABINDU = anunasika336(byte)162,NO_CHAR, //0902 -- DEVANAGARI SIGN ANUSVARA = bindu337(byte)163,NO_CHAR, //0903 -- DEVANAGARI SIGN VISARGA338NO_CHAR,NO_CHAR, //0904 <reserved>339(byte)164,NO_CHAR, //0905 -- DEVANAGARI LETTER A340(byte)165,NO_CHAR, //0906 -- DEVANAGARI LETTER AA341(byte)166,NO_CHAR, //0907 -- DEVANAGARI LETTER I342(byte)167,NO_CHAR, //0908 -- DEVANAGARI LETTER II343(byte)168,NO_CHAR, //0909 -- DEVANAGARI LETTER U344(byte)169,NO_CHAR, //090a -- DEVANAGARI LETTER UU345(byte)170,NO_CHAR, //090b -- DEVANAGARI LETTER VOCALIC R346(byte)166,(byte)233, //090c -- DEVANAGARI LETTER VOVALIC L347(byte)174,NO_CHAR, //090d -- DEVANAGARI LETTER CANDRA E348(byte)171,NO_CHAR, //090e -- DEVANAGARI LETTER SHORT E349(byte)172,NO_CHAR, //090f -- DEVANAGARI LETTER E350(byte)173,NO_CHAR, //0910 -- DEVANAGARI LETTER AI351(byte)178,NO_CHAR, //0911 -- DEVANAGARI LETTER CANDRA O352(byte)175,NO_CHAR, //0912 -- DEVANAGARI LETTER SHORT O353(byte)176,NO_CHAR, //0913 -- DEVANAGARI LETTER O354(byte)177,NO_CHAR, //0914 -- DEVANAGARI LETTER AU355(byte)179,NO_CHAR, //0915 -- DEVANAGARI LETTER KA356(byte)180,NO_CHAR, //0916 -- DEVANAGARI LETTER KHA357(byte)181,NO_CHAR, //0917 -- DEVANAGARI LETTER GA358(byte)182,NO_CHAR, //0918 -- DEVANAGARI LETTER GHA359(byte)183,NO_CHAR, //0919 -- DEVANAGARI LETTER NGA360(byte)184,NO_CHAR, //091a -- DEVANAGARI LETTER CA361(byte)185,NO_CHAR, //091b -- DEVANAGARI LETTER CHA362(byte)186,NO_CHAR, //091c -- DEVANAGARI LETTER JA363(byte)187,NO_CHAR, //091d -- DEVANAGARI LETTER JHA364(byte)188,NO_CHAR, //091e -- DEVANAGARI LETTER NYA365(byte)189,NO_CHAR, //091f -- DEVANAGARI LETTER TTA366(byte)190,NO_CHAR, //0920 -- DEVANAGARI LETTER TTHA367(byte)191,NO_CHAR, //0921 -- DEVANAGARI LETTER DDA368(byte)192,NO_CHAR, //0922 -- DEVANAGARI LETTER DDHA369(byte)193,NO_CHAR, //0923 -- DEVANAGARI LETTER NNA370(byte)194,NO_CHAR, //0924 -- DEVANAGARI LETTER TA371(byte)195,NO_CHAR, //0925 -- DEVANAGARI LETTER THA372(byte)196,NO_CHAR, //0926 -- DEVANAGARI LETTER DA373(byte)197,NO_CHAR, //0927 -- DEVANAGARI LETTER DHA374(byte)198,NO_CHAR, //0928 -- DEVANAGARI LETTER NA375(byte)199,NO_CHAR, //0929 -- DEVANAGARI LETTER NNNA <=> 0928 + 093C376(byte)200,NO_CHAR, //092a -- DEVANAGARI LETTER PA377(byte)201,NO_CHAR, //092b -- DEVANAGARI LETTER PHA378(byte)202,NO_CHAR, //092c -- DEVANAGARI LETTER BA379(byte)203,NO_CHAR, //092d -- DEVANAGARI LETTER BHA380(byte)204,NO_CHAR, //092e -- DEVANAGARI LETTER MA381(byte)205,NO_CHAR, //092f -- DEVANAGARI LETTER YA382(byte)207,NO_CHAR, //0930 -- DEVANAGARI LETTER RA383(byte)208,NO_CHAR, //0931 -- DEVANAGARI LETTER RRA <=> 0930 + 093C384(byte)209,NO_CHAR, //0932 -- DEVANAGARI LETTER LA385(byte)210,NO_CHAR, //0933 -- DEVANAGARI LETTER LLA386(byte)211,NO_CHAR, //0934 -- DEVANAGARI LETTER LLLA <=> 0933 + 093C387(byte)212,NO_CHAR, //0935 -- DEVANAGARI LETTER VA388(byte)213,NO_CHAR, //0936 -- DEVANAGARI LETTER SHA389(byte)214,NO_CHAR, //0937 -- DEVANAGARI LETTER SSA390(byte)215,NO_CHAR, //0938 -- DEVANAGARI LETTER SA391(byte)216,NO_CHAR, //0939 -- DEVANAGARI LETTER HA392NO_CHAR,NO_CHAR, //093a <reserved>393NO_CHAR,NO_CHAR, //093b <reserved>394(byte)233,NO_CHAR, //093c -- DEVANAGARI SIGN NUKTA395(byte)234,(byte)233, //093d -- DEVANAGARI SIGN AVAGRAHA396(byte)218,NO_CHAR, //093e -- DEVANAGARI VOWEL SIGN AA397(byte)219,NO_CHAR, //093f -- DEVANAGARI VOWEL SIGN I398(byte)220,NO_CHAR, //0940 -- DEVANAGARI VOWEL SIGN II399(byte)221,NO_CHAR, //0941 -- DEVANAGARI VOWEL SIGN U400(byte)222,NO_CHAR, //0942 -- DEVANAGARI VOWEL SIGN UU401(byte)223,NO_CHAR, //0943 -- DEVANAGARI VOWEL SIGN VOCALIC R402(byte)223,(byte)233, //0944 -- DEVANAGARI VOWEL SIGN VOCALIC RR403(byte)227,NO_CHAR, //0945 -- DEVANAGARI VOWEL SIGN CANDRA E404(byte)224,NO_CHAR, //0946 -- DEVANAGARI VOWEL SIGN SHORT E405(byte)225,NO_CHAR, //0947 -- DEVANAGARI VOWEL SIGN E406(byte)226,NO_CHAR, //0948 -- DEVANAGARI VOWEL SIGN AI407(byte)231,NO_CHAR, //0949 -- DEVANAGARI VOWEL SIGN CANDRA O408(byte)228,NO_CHAR, //094a -- DEVANAGARI VOWEL SIGN SHORT O409(byte)229,NO_CHAR, //094b -- DEVANAGARI VOWEL SIGN O410(byte)230,NO_CHAR, //094c -- DEVANAGARI VOWEL SIGN AU411(byte)232,NO_CHAR, //094d -- DEVANAGARI SIGN VIRAMA ( halant )412NO_CHAR,NO_CHAR, //094e <reserved>413NO_CHAR,NO_CHAR, //094f <reserved>414(byte)161,(byte)233, //0950 -- DEVANAGARI OM415(byte)240,(byte)181, //0951 -- DEVANAGARI STRESS SIGN UDATTA416(byte)240,(byte)184, //0952 -- DEVANAGARI STRESS SIGN ANUDATTA417(byte)254,NO_CHAR, //0953 -- DEVANAGARI GRAVE ACCENT || MISSING418(byte)254,NO_CHAR, //0954 -- DEVANAGARI ACUTE ACCENT || MISSING419NO_CHAR,NO_CHAR, //0955 <reserved>420NO_CHAR,NO_CHAR, //0956 <reserved>421NO_CHAR,NO_CHAR, //0957 <reserved>422(byte)179,(byte)233, //0958 -- DEVANAGARI LETTER QA <=> 0915 + 093C423(byte)180,(byte)233, //0959 -- DEVANAGARI LETTER KHHA <=> 0916 + 093C424(byte)181,(byte)233, //095a -- DEVANAGARI LETTER GHHA <=> 0917 + 093C425(byte)186,(byte)233, //095b -- DEVANAGARI LETTER ZA <=> 091C + 093C426(byte)191,(byte)233, //095c -- DEVANAGARI LETTER DDDHA <=> 0921 + 093C427(byte)192,(byte)233, //095d -- DEVANAGARI LETTER RHA <=> 0922 + 093C428(byte)201,(byte)233, //095e -- DEVANAGARI LETTER FA <=> 092B + 093C429(byte)206,NO_CHAR, //095f -- DEVANAGARI LETTER YYA <=> 092F + 093C430(byte)170,(byte)233, //0960 -- DEVANAGARI LETTER VOCALIC RR431(byte)167,(byte)233, //0961 -- DEVANAGARI LETTER VOCALIC LL432(byte)219,(byte)233, //0962 -- DEVANAGARI VOWEL SIGN VOCALIC L433(byte)220,(byte)233, //0963 -- DEVANAGARI VOWEL SIGN VOCALIC LL434(byte)234,NO_CHAR, //0964 -- DEVANAGARI DANDA ( phrase separator )435(byte)234,(byte)234, //0965 -- DEVANAGARI DOUBLE DANDA436(byte)241,NO_CHAR, //0966 -- DEVANAGARI DIGIT ZERO437(byte)242,NO_CHAR, //0967 -- DEVANAGARI DIGIT ONE438(byte)243,NO_CHAR, //0968 -- DEVANAGARI DIGIT TWO439(byte)244,NO_CHAR, //0969 -- DEVANAGARI DIGIT THREE440(byte)245,NO_CHAR, //096a -- DEVANAGARI DIGIT FOUR441(byte)246,NO_CHAR, //096b -- DEVANAGARI DIGIT FIVE442(byte)247,NO_CHAR, //096c -- DEVANAGARI DIGIT SIX443(byte)248,NO_CHAR, //096d -- DEVANAGARI DIGIT SEVEN444(byte)249,NO_CHAR, //096e -- DEVANAGARI DIGIT EIGHT445(byte)250,NO_CHAR, //096f -- DEVANAGARI DIGIT NINE446(byte)240,(byte)191, //0970 -- DEVANAGARI ABBREVIATION SIGN447NO_CHAR,NO_CHAR, //0971 -- reserved448NO_CHAR,NO_CHAR, //0972 -- reserved449NO_CHAR,NO_CHAR, //0973 -- reserved450NO_CHAR,NO_CHAR, //0974 -- reserved451NO_CHAR,NO_CHAR, //0975 -- reserved452NO_CHAR,NO_CHAR, //0976 -- reserved453NO_CHAR,NO_CHAR, //0977 -- reserved454NO_CHAR,NO_CHAR, //0978 -- reserved455NO_CHAR,NO_CHAR, //0979 -- reserved456NO_CHAR,NO_CHAR, //097a -- reserved457NO_CHAR,NO_CHAR, //097b -- reserved458NO_CHAR,NO_CHAR, //097c -- reserved459NO_CHAR,NO_CHAR, //097d -- reserved460NO_CHAR,NO_CHAR, //097e -- reserved461NO_CHAR,NO_CHAR //097f -- reserved462}; //end of table definition463464private static class Decoder extends CharsetDecoder {465466private static final char ZWNJ_CHAR = '\u200c';467private static final char ZWJ_CHAR = '\u200d';468private static final char INVALID_CHAR = '\uffff';469470private char contextChar = INVALID_CHAR;471private boolean needFlushing = false;472473474private Decoder(Charset cs) {475super(cs, 1.0f, 1.0f);476}477478protected CoderResult implFlush(CharBuffer out) {479if(needFlushing) {480if (out.remaining() < 1) {481return CoderResult.OVERFLOW;482} else {483out.put(contextChar);484}485}486contextChar = INVALID_CHAR;487needFlushing = false;488return CoderResult.UNDERFLOW;489}490491/* Rules:492* 1) ATR,EXT,following character to be replaced with '\ufffd'493* 2) Halant + Halant => '\u094d' (Virama) + '\u200c'(ZWNJ)494* 3) Halant + Nukta => '\u094d' (Virama) + '\u200d'(ZWJ)495*/496private CoderResult decodeArrayLoop(ByteBuffer src,497CharBuffer dst)498{499byte[] sa = src.array();500int sp = src.arrayOffset() + src.position();501int sl = src.arrayOffset() + src.limit();502503char[] da = dst.array();504int dp = dst.arrayOffset() + dst.position();505int dl = dst.arrayOffset() + dst.limit();506507try {508while (sp < sl) {509int index = sa[sp];510index = ( index < 0 )? ( index + 255 ):index;511char currentChar = directMapTable[index];512513// if the contextChar is either ATR || EXT514// set the output to '\ufffd'515if(contextChar == '\ufffd') {516if (dl - dp < 1)517return CoderResult.OVERFLOW;518da[dp++] = '\ufffd';519contextChar = INVALID_CHAR;520needFlushing = false;521sp++;522continue;523}524525switch(currentChar) {526case '\u0901':527case '\u0907':528case '\u0908':529case '\u090b':530case '\u093f':531case '\u0940':532case '\u0943':533case '\u0964':534if(needFlushing) {535if (dl - dp < 1)536return CoderResult.OVERFLOW;537da[dp++] = contextChar;538contextChar = currentChar;539sp++;540continue;541}542contextChar = currentChar;543needFlushing = true;544sp++;545continue;546case NUKTA_CHAR:547if (dl - dp < 1)548return CoderResult.OVERFLOW;549switch(contextChar) {550case '\u0901':551da[dp++] = '\u0950';552break;553case '\u0907':554da[dp++] = '\u090c';555break;556case '\u0908':557da[dp++] = '\u0961';558break;559case '\u090b':560da[dp++] = '\u0960';561break;562case '\u093f':563da[dp++] = '\u0962';564break;565case '\u0940':566da[dp++] = '\u0963';567break;568case '\u0943':569da[dp++] = '\u0944';570break;571case '\u0964':572da[dp++] = '\u093d';573break;574case HALANT_CHAR:575if(needFlushing) {576da[dp++] = contextChar;577contextChar = currentChar;578sp++;579continue;580}581da[dp++] = ZWJ_CHAR;582break;583default:584if(needFlushing) {585da[dp++] = contextChar;586contextChar = currentChar;587sp++;588continue;589}590da[dp++] = NUKTA_CHAR;591}592break;593case HALANT_CHAR:594if (dl - dp < 1)595return CoderResult.OVERFLOW;596if(needFlushing) {597da[dp++] = contextChar;598contextChar = currentChar;599sp++;600continue;601}602if(contextChar == HALANT_CHAR) {603da[dp++] = ZWNJ_CHAR;604break;605}606da[dp++] = HALANT_CHAR;607break;608case INVALID_CHAR:609if(needFlushing) {610if (dl - dp < 1)611return CoderResult.OVERFLOW;612da[dp++] = contextChar;613contextChar = currentChar;614sp++;615continue;616}617return CoderResult.unmappableForLength(1);618default:619if (dl - dp < 1)620return CoderResult.OVERFLOW;621if(needFlushing) {622da[dp++] = contextChar;623contextChar = currentChar;624sp++;625continue;626}627da[dp++] = currentChar;628break;629}//end switch630631contextChar = currentChar;632needFlushing = false;633sp++;634}635return CoderResult.UNDERFLOW;636} finally {637src.position(sp - src.arrayOffset());638dst.position(dp - dst.arrayOffset());639}640}641642private CoderResult decodeBufferLoop(ByteBuffer src,643CharBuffer dst)644{645int mark = src.position();646647try {648while (src.hasRemaining()) {649int index = src.get();650index = ( index < 0 )? ( index + 255 ):index;651char currentChar = directMapTable[index];652653// if the contextChar is either ATR || EXT654// set the output to '\ufffd'655if(contextChar == '\ufffd') {656if (dst.remaining() < 1)657return CoderResult.OVERFLOW;658dst.put('\ufffd');659contextChar = INVALID_CHAR;660needFlushing = false;661mark++;662continue;663}664665switch(currentChar) {666case '\u0901':667case '\u0907':668case '\u0908':669case '\u090b':670case '\u093f':671case '\u0940':672case '\u0943':673case '\u0964':674if(needFlushing) {675if (dst.remaining() < 1)676return CoderResult.OVERFLOW;677dst.put(contextChar);678contextChar = currentChar;679mark++;680continue;681}682contextChar = currentChar;683needFlushing = true;684mark++;685continue;686case NUKTA_CHAR:687if (dst.remaining() < 1)688return CoderResult.OVERFLOW;689switch(contextChar) {690case '\u0901':691dst.put('\u0950');692break;693case '\u0907':694dst.put('\u090c');695break;696case '\u0908':697dst.put('\u0961');698break;699case '\u090b':700dst.put('\u0960');701break;702case '\u093f':703dst.put('\u0962');704break;705case '\u0940':706dst.put('\u0963');707break;708case '\u0943':709dst.put('\u0944');710break;711case '\u0964':712dst.put('\u093d');713break;714case HALANT_CHAR:715if(needFlushing) {716dst.put(contextChar);717contextChar = currentChar;718mark++;719continue;720}721dst.put(ZWJ_CHAR);722break;723default:724if(needFlushing) {725dst.put(contextChar);726contextChar = currentChar;727mark++;728continue;729}730dst.put(NUKTA_CHAR);731}732break;733case HALANT_CHAR:734if (dst.remaining() < 1)735return CoderResult.OVERFLOW;736if(needFlushing) {737dst.put(contextChar);738contextChar = currentChar;739mark++;740continue;741}742if(contextChar == HALANT_CHAR) {743dst.put(ZWNJ_CHAR);744break;745}746dst.put(HALANT_CHAR);747break;748case INVALID_CHAR:749if(needFlushing) {750if (dst.remaining() < 1)751return CoderResult.OVERFLOW;752dst.put(contextChar);753contextChar = currentChar;754mark++;755continue;756}757return CoderResult.unmappableForLength(1);758default:759if (dst.remaining() < 1)760return CoderResult.OVERFLOW;761if(needFlushing) {762dst.put(contextChar);763contextChar = currentChar;764mark++;765continue;766}767dst.put(currentChar);768break;769}//end switch770contextChar = currentChar;771needFlushing = false;772mark++;773}774return CoderResult.UNDERFLOW;775} finally {776src.position(mark);777}778}779780protected CoderResult decodeLoop(ByteBuffer src,781CharBuffer dst)782{783if (src.hasArray() && dst.hasArray())784return decodeArrayLoop(src, dst);785else786return decodeBufferLoop(src, dst);787}788}789790private static class Encoder extends CharsetEncoder {791792private static final byte NO_CHAR = (byte)255;793794//private static CharToByteISCII91 c2b = new CharToByteISCII91();795//private static final byte[] directMapTable = c2b.getISCIIEncoderMap();796797private final Surrogate.Parser sgp = new Surrogate.Parser();798799private Encoder(Charset cs) {800super(cs, 2.0f, 2.0f);801}802803public boolean canEncode(char ch) {804//check for Devanagari range,ZWJ,ZWNJ and ASCII range.805return ((ch >= '\u0900' && ch <= '\u097f' &&806encoderMappingTable[2*(ch-'\u0900')] != NO_CHAR) ||807(ch == '\u200d') ||808(ch == '\u200c') ||809(ch <= '\u007f'));810}811812813private CoderResult encodeArrayLoop(CharBuffer src,814ByteBuffer dst)815{816char[] sa = src.array();817int sp = src.arrayOffset() + src.position();818int sl = src.arrayOffset() + src.limit();819820byte[] da = dst.array();821int dp = dst.arrayOffset() + dst.position();822int dl = dst.arrayOffset() + dst.limit();823824try {825char inputChar;826while (sp < sl) {827int index = Integer.MIN_VALUE;828inputChar = sa[sp];829830if (inputChar >= 0x0000 && inputChar <= 0x007f) {831if (dl - dp < 1)832return CoderResult.OVERFLOW;833da[dp++] = (byte) inputChar;834sp++;835continue;836}837838// if inputChar == ZWJ replace it with halant839// if inputChar == ZWNJ replace it with Nukta840841if (inputChar == 0x200c) {842inputChar = HALANT_CHAR;843}844else if (inputChar == 0x200d) {845inputChar = NUKTA_CHAR;846}847848if (inputChar >= 0x0900 && inputChar <= 0x097f) {849index = ((int)(inputChar) - 0x0900)*2;850}851852if (Character.isSurrogate(inputChar)) {853if (sgp.parse(inputChar, sa, sp, sl) < 0)854return sgp.error();855return sgp.unmappableResult();856}857858if (index == Integer.MIN_VALUE ||859encoderMappingTable[index] == NO_CHAR) {860return CoderResult.unmappableForLength(1);861} else {862if(encoderMappingTable[index + 1] == NO_CHAR) {863if(dl - dp < 1)864return CoderResult.OVERFLOW;865da[dp++] = encoderMappingTable[index];866} else {867if(dl - dp < 2)868return CoderResult.OVERFLOW;869da[dp++] = encoderMappingTable[index];870da[dp++] = encoderMappingTable[index + 1];871}872sp++;873}874}875return CoderResult.UNDERFLOW;876} finally {877src.position(sp - src.arrayOffset());878dst.position(dp - dst.arrayOffset());879}880}881882private CoderResult encodeBufferLoop(CharBuffer src,883ByteBuffer dst)884{885int mark = src.position();886887try {888char inputChar;889while (src.hasRemaining()) {890int index = Integer.MIN_VALUE;891inputChar = src.get();892893if (inputChar >= 0x0000 && inputChar <= 0x007f) {894if (dst.remaining() < 1)895return CoderResult.OVERFLOW;896dst.put((byte) inputChar);897mark++;898continue;899}900901// if inputChar == ZWJ replace it with halant902// if inputChar == ZWNJ replace it with Nukta903904if (inputChar == 0x200c) {905inputChar = HALANT_CHAR;906}907else if (inputChar == 0x200d) {908inputChar = NUKTA_CHAR;909}910911if (inputChar >= 0x0900 && inputChar <= 0x097f) {912index = ((int)(inputChar) - 0x0900)*2;913}914915if (Character.isSurrogate(inputChar)) {916if (sgp.parse(inputChar, src) < 0)917return sgp.error();918return sgp.unmappableResult();919}920921if (index == Integer.MIN_VALUE ||922encoderMappingTable[index] == NO_CHAR) {923return CoderResult.unmappableForLength(1);924} else {925if(encoderMappingTable[index + 1] == NO_CHAR) {926if(dst.remaining() < 1)927return CoderResult.OVERFLOW;928dst.put(encoderMappingTable[index]);929} else {930if(dst.remaining() < 2)931return CoderResult.OVERFLOW;932dst.put(encoderMappingTable[index]);933dst.put(encoderMappingTable[index + 1]);934}935}936mark++;937}938return CoderResult.UNDERFLOW;939} finally {940src.position(mark);941}942}943944protected CoderResult encodeLoop(CharBuffer src,945ByteBuffer dst)946{947if (src.hasArray() && dst.hasArray())948return encodeArrayLoop(src, dst);949else950return encodeBufferLoop(src, dst);951}952}953}954955956