jdk/src/share/classes/sun/io/CharToByteISCII91.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.nio.cs.ext.ISCII91;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Copyright (c) 1998 International Business Machines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Author : Sunanda Bera, C. Thirumalesh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Last Modified : 11,December,1998
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Purpose : Defines class CharToByteISCII91.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Revision History
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * ======== =======
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Date        By            Description
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * ----        --            -----------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * March 29, 1999 John Raley Removed MalformedInputException; modified substitution logic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Converter class. Converts between ISCII91 encoding and Unicode encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * ISCII91 is the character encoding as defined in Indian Standard document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * IS 13194:1991 ( Indian Script Code for Information Interchange ).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see sun.io.CharToByteConverter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * {jbr} I am not sure this class adheres to code converter conventions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Need to investigate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Might should recode as a subclass of CharToByteSingleByte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class CharToByteISCII91 extends CharToByteConverter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        private static final byte NO_CHAR = (byte)255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        //private final static ISCII91 nioCoder = new ISCII91();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        private final static byte[] directMapTable = ISCII91.getEncoderMappingTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        private static final char NUKTA_CHAR = '\u093c';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        private static final char HALANT_CHAR = '\u094d';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @return true for Devanagari and ASCII range and for the special characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *              Zero Width Joiner and Zero Width Non-Joiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @see sun.io.CharToByteConverter#canConvert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        public boolean canConvert(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        //check for Devanagari range,ZWJ,ZWNJ and ASCII range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return ((ch >= 0x0900 && ch <= 0x097f) || (ch == 0x200d || ch == 0x200c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                || (ch >= 0x0000 && ch <= 0x007f) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } //canConvert()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * Converts both Devanagari and ASCII range of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @see sun.io.CharToByteConverter#convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public int convert(char[] input, int inStart, int inEnd, byte[] output, int outStart, int outEnd) throws MalformedInputException, UnknownCharacterException, ConversionBufferFullException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        charOff = inStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        byteOff = outStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        for (;charOff < inEnd; charOff++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            char inputChar = input[charOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            int index = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            boolean isSurrogatePair = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            //check if input is in ASCII RANGE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (inputChar >= 0x0000 && inputChar <= 0x007f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (byteOff >= outEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                output[byteOff++] = (byte) inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            // if inputChar == ZWJ replace it with halant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // if inputChar == ZWNJ replace it with Nukta
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (inputChar == 0x200c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                inputChar = HALANT_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            else if (inputChar == 0x200d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                inputChar = NUKTA_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (inputChar >= 0x0900 && inputChar <= 0x097f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                index = ((int)(inputChar) - 0x0900)*2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // If input char is a high surrogate, ensure that the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            // char is a low surrogate.  If not, throw a MalformedInputException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            // Leave index untouched so substitution or an UnknownCharacterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // will result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            else if (inputChar >= 0xd800 && inputChar <= 0xdbff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (charOff < inEnd-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    char nextChar = input[charOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    if (nextChar >= 0xdc00 && nextChar <= 0xdfff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        charOff++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        isSurrogatePair = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                if (!isSurrogatePair) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            else if (inputChar >= 0xdc00 && inputChar <= 0xdfff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (index == Integer.MIN_VALUE || directMapTable[index] == NO_CHAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                if (subMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    if (byteOff + subBytes.length >= outEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                            throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    System.arraycopy(subBytes, 0, output, byteOff, subBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    byteOff += subBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    badInputLength = isSurrogatePair? 2 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if(byteOff >= outEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                output[byteOff++] = directMapTable[index++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                if(directMapTable[index] != NO_CHAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    if(byteOff >= outEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    output[byteOff++] = directMapTable[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } //end for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return byteOff - outStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    } //end of routine convert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
* @see sun.io.CharToByteConverter#flush
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        public int flush( byte[] output, int outStart, int outEnd )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        throws MalformedInputException, ConversionBufferFullException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        byteOff = charOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }//flush()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * @return The character encoding as a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        public String getCharacterEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return "ISCII91";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }//getCharacterEncoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * @see sun.io.CharToByteConverter#getMaxBytesPerChar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        public int getMaxBytesPerChar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }//getMaxBytesPerChar()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * @see sun.io.CharToByteConverter#reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        byteOff = charOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
} //end of class definition