Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/cs/ext/ISCII91.java
38919 views
/*1* Copyright (c) 2002, 2011, 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();502assert (sp <= sl);503sp = (sp <= sl ? sp : sl);504505char[] da = dst.array();506int dp = dst.arrayOffset() + dst.position();507int dl = dst.arrayOffset() + dst.limit();508assert (dp <= dl);509dp = (dp <= dl ? dp : dl);510511try {512while (sp < sl) {513int index = sa[sp];514index = ( index < 0 )? ( index + 255 ):index;515char currentChar = directMapTable[index];516517// if the contextChar is either ATR || EXT518// set the output to '\ufffd'519if(contextChar == '\ufffd') {520if (dl - dp < 1)521return CoderResult.OVERFLOW;522da[dp++] = '\ufffd';523contextChar = INVALID_CHAR;524needFlushing = false;525sp++;526continue;527}528529switch(currentChar) {530case '\u0901':531case '\u0907':532case '\u0908':533case '\u090b':534case '\u093f':535case '\u0940':536case '\u0943':537case '\u0964':538if(needFlushing) {539if (dl - dp < 1)540return CoderResult.OVERFLOW;541da[dp++] = contextChar;542contextChar = currentChar;543sp++;544continue;545}546contextChar = currentChar;547needFlushing = true;548sp++;549continue;550case NUKTA_CHAR:551if (dl - dp < 1)552return CoderResult.OVERFLOW;553switch(contextChar) {554case '\u0901':555da[dp++] = '\u0950';556break;557case '\u0907':558da[dp++] = '\u090c';559break;560case '\u0908':561da[dp++] = '\u0961';562break;563case '\u090b':564da[dp++] = '\u0960';565break;566case '\u093f':567da[dp++] = '\u0962';568break;569case '\u0940':570da[dp++] = '\u0963';571break;572case '\u0943':573da[dp++] = '\u0944';574break;575case '\u0964':576da[dp++] = '\u093d';577break;578case HALANT_CHAR:579if(needFlushing) {580da[dp++] = contextChar;581contextChar = currentChar;582sp++;583continue;584}585da[dp++] = ZWJ_CHAR;586break;587default:588if(needFlushing) {589da[dp++] = contextChar;590contextChar = currentChar;591sp++;592continue;593}594da[dp++] = NUKTA_CHAR;595}596break;597case HALANT_CHAR:598if (dl - dp < 1)599return CoderResult.OVERFLOW;600if(needFlushing) {601da[dp++] = contextChar;602contextChar = currentChar;603sp++;604continue;605}606if(contextChar == HALANT_CHAR) {607da[dp++] = ZWNJ_CHAR;608break;609}610da[dp++] = HALANT_CHAR;611break;612case INVALID_CHAR:613if(needFlushing) {614if (dl - dp < 1)615return CoderResult.OVERFLOW;616da[dp++] = contextChar;617contextChar = currentChar;618sp++;619continue;620}621return CoderResult.unmappableForLength(1);622default:623if (dl - dp < 1)624return CoderResult.OVERFLOW;625if(needFlushing) {626da[dp++] = contextChar;627contextChar = currentChar;628sp++;629continue;630}631da[dp++] = currentChar;632break;633}//end switch634635contextChar = currentChar;636needFlushing = false;637sp++;638}639return CoderResult.UNDERFLOW;640} finally {641src.position(sp - src.arrayOffset());642dst.position(dp - dst.arrayOffset());643}644}645646private CoderResult decodeBufferLoop(ByteBuffer src,647CharBuffer dst)648{649int mark = src.position();650651try {652while (src.hasRemaining()) {653int index = src.get();654index = ( index < 0 )? ( index + 255 ):index;655char currentChar = directMapTable[index];656657// if the contextChar is either ATR || EXT658// set the output to '\ufffd'659if(contextChar == '\ufffd') {660if (dst.remaining() < 1)661return CoderResult.OVERFLOW;662dst.put('\ufffd');663contextChar = INVALID_CHAR;664needFlushing = false;665mark++;666continue;667}668669switch(currentChar) {670case '\u0901':671case '\u0907':672case '\u0908':673case '\u090b':674case '\u093f':675case '\u0940':676case '\u0943':677case '\u0964':678if(needFlushing) {679if (dst.remaining() < 1)680return CoderResult.OVERFLOW;681dst.put(contextChar);682contextChar = currentChar;683mark++;684continue;685}686contextChar = currentChar;687needFlushing = true;688mark++;689continue;690case NUKTA_CHAR:691if (dst.remaining() < 1)692return CoderResult.OVERFLOW;693switch(contextChar) {694case '\u0901':695dst.put('\u0950');696break;697case '\u0907':698dst.put('\u090c');699break;700case '\u0908':701dst.put('\u0961');702break;703case '\u090b':704dst.put('\u0960');705break;706case '\u093f':707dst.put('\u0962');708break;709case '\u0940':710dst.put('\u0963');711break;712case '\u0943':713dst.put('\u0944');714break;715case '\u0964':716dst.put('\u093d');717break;718case HALANT_CHAR:719if(needFlushing) {720dst.put(contextChar);721contextChar = currentChar;722mark++;723continue;724}725dst.put(ZWJ_CHAR);726break;727default:728if(needFlushing) {729dst.put(contextChar);730contextChar = currentChar;731mark++;732continue;733}734dst.put(NUKTA_CHAR);735}736break;737case HALANT_CHAR:738if (dst.remaining() < 1)739return CoderResult.OVERFLOW;740if(needFlushing) {741dst.put(contextChar);742contextChar = currentChar;743mark++;744continue;745}746if(contextChar == HALANT_CHAR) {747dst.put(ZWNJ_CHAR);748break;749}750dst.put(HALANT_CHAR);751break;752case INVALID_CHAR:753if(needFlushing) {754if (dst.remaining() < 1)755return CoderResult.OVERFLOW;756dst.put(contextChar);757contextChar = currentChar;758mark++;759continue;760}761return CoderResult.unmappableForLength(1);762default:763if (dst.remaining() < 1)764return CoderResult.OVERFLOW;765if(needFlushing) {766dst.put(contextChar);767contextChar = currentChar;768mark++;769continue;770}771dst.put(currentChar);772break;773}//end switch774contextChar = currentChar;775needFlushing = false;776mark++;777}778return CoderResult.UNDERFLOW;779} finally {780src.position(mark);781}782}783784protected CoderResult decodeLoop(ByteBuffer src,785CharBuffer dst)786{787if (src.hasArray() && dst.hasArray())788return decodeArrayLoop(src, dst);789else790return decodeBufferLoop(src, dst);791}792}793794private static class Encoder extends CharsetEncoder {795796private static final byte NO_CHAR = (byte)255;797798//private static CharToByteISCII91 c2b = new CharToByteISCII91();799//private static final byte[] directMapTable = c2b.getISCIIEncoderMap();800801private final Surrogate.Parser sgp = new Surrogate.Parser();802803private Encoder(Charset cs) {804super(cs, 2.0f, 2.0f);805}806807public boolean canEncode(char ch) {808//check for Devanagari range,ZWJ,ZWNJ and ASCII range.809return ((ch >= '\u0900' && ch <= '\u097f' &&810encoderMappingTable[2*(ch-'\u0900')] != NO_CHAR) ||811(ch == '\u200d') ||812(ch == '\u200c') ||813(ch <= '\u007f'));814}815816817private CoderResult encodeArrayLoop(CharBuffer src,818ByteBuffer dst)819{820char[] sa = src.array();821int sp = src.arrayOffset() + src.position();822int sl = src.arrayOffset() + src.limit();823assert (sp <= sl);824sp = (sp <= sl ? sp : sl);825byte[] da = dst.array();826int dp = dst.arrayOffset() + dst.position();827int dl = dst.arrayOffset() + dst.limit();828assert (dp <= dl);829dp = (dp <= dl ? dp : dl);830831int outputSize = 0;832833try {834char inputChar;835while (sp < sl) {836int index = Integer.MIN_VALUE;837inputChar = sa[sp];838839if (inputChar >= 0x0000 && inputChar <= 0x007f) {840if (dl - dp < 1)841return CoderResult.OVERFLOW;842da[dp++] = (byte) inputChar;843sp++;844continue;845}846847// if inputChar == ZWJ replace it with halant848// if inputChar == ZWNJ replace it with Nukta849850if (inputChar == 0x200c) {851inputChar = HALANT_CHAR;852}853else if (inputChar == 0x200d) {854inputChar = NUKTA_CHAR;855}856857if (inputChar >= 0x0900 && inputChar <= 0x097f) {858index = ((int)(inputChar) - 0x0900)*2;859}860861if (Character.isSurrogate(inputChar)) {862if (sgp.parse(inputChar, sa, sp, sl) < 0)863return sgp.error();864return sgp.unmappableResult();865}866867if (index == Integer.MIN_VALUE ||868encoderMappingTable[index] == NO_CHAR) {869return CoderResult.unmappableForLength(1);870} else {871if(encoderMappingTable[index + 1] == NO_CHAR) {872if(dl - dp < 1)873return CoderResult.OVERFLOW;874da[dp++] = encoderMappingTable[index];875} else {876if(dl - dp < 2)877return CoderResult.OVERFLOW;878da[dp++] = encoderMappingTable[index];879da[dp++] = encoderMappingTable[index + 1];880}881sp++;882}883}884return CoderResult.UNDERFLOW;885} finally {886src.position(sp - src.arrayOffset());887dst.position(dp - dst.arrayOffset());888}889}890891private CoderResult encodeBufferLoop(CharBuffer src,892ByteBuffer dst)893{894int mark = src.position();895896try {897char inputChar;898while (src.hasRemaining()) {899int index = Integer.MIN_VALUE;900inputChar = src.get();901902if (inputChar >= 0x0000 && inputChar <= 0x007f) {903if (dst.remaining() < 1)904return CoderResult.OVERFLOW;905dst.put((byte) inputChar);906mark++;907continue;908}909910// if inputChar == ZWJ replace it with halant911// if inputChar == ZWNJ replace it with Nukta912913if (inputChar == 0x200c) {914inputChar = HALANT_CHAR;915}916else if (inputChar == 0x200d) {917inputChar = NUKTA_CHAR;918}919920if (inputChar >= 0x0900 && inputChar <= 0x097f) {921index = ((int)(inputChar) - 0x0900)*2;922}923924if (Character.isSurrogate(inputChar)) {925if (sgp.parse(inputChar, src) < 0)926return sgp.error();927return sgp.unmappableResult();928}929930if (index == Integer.MIN_VALUE ||931encoderMappingTable[index] == NO_CHAR) {932return CoderResult.unmappableForLength(1);933} else {934if(encoderMappingTable[index + 1] == NO_CHAR) {935if(dst.remaining() < 1)936return CoderResult.OVERFLOW;937dst.put(encoderMappingTable[index]);938} else {939if(dst.remaining() < 2)940return CoderResult.OVERFLOW;941dst.put(encoderMappingTable[index]);942dst.put(encoderMappingTable[index + 1]);943}944}945mark++;946}947return CoderResult.UNDERFLOW;948} finally {949src.position(mark);950}951}952953protected CoderResult encodeLoop(CharBuffer src,954ByteBuffer dst)955{956if (src.hasArray() && dst.hasArray())957return encodeArrayLoop(src, dst);958else959return encodeBufferLoop(src, dst);960}961}962}963964965