jdk/src/share/classes/sun/io/ByteToCharSingleByte.java
changeset 10372 2f6d68f22eae
parent 10321 64f7ee2f31dd
parent 10371 7da2112e4236
child 10373 d4c5e59b82f8
equal deleted inserted replaced
10321:64f7ee2f31dd 10372:2f6d68f22eae
     1 /*
       
     2  * Copyright (c) 1996, 2002, 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 package sun.io;
       
    27 
       
    28 /**
       
    29  * A table driven conversion from byte to char for single byte  character sets.
       
    30  * The needed data tables will reside in a character set specific subclass.
       
    31  *
       
    32  * @author Lloyd Honomichl
       
    33  * @author Asmus Freytag
       
    34  */
       
    35 public abstract class ByteToCharSingleByte extends ByteToCharConverter {
       
    36 
       
    37     /**
       
    38      * Mapping table. Values supplied by subclass
       
    39      */
       
    40     protected String byteToCharTable;
       
    41 
       
    42     public String getByteToCharTable() {
       
    43         return byteToCharTable;
       
    44     }
       
    45 
       
    46     public int flush(char[] output, int outStart, int outEnd) {
       
    47         byteOff = charOff = 0;
       
    48         return 0;
       
    49     }
       
    50 
       
    51     /**
       
    52      * Converts bytes to characters according to the selected character
       
    53      * encoding.
       
    54      * Maintains internal state, so that conversions that result in
       
    55      * exceptions can be restarted by calling convert again, with
       
    56      * appropriately modified parameters.
       
    57      * Call reset before converting input that is not a continuation of
       
    58      * the previous call.
       
    59      * @return the number of characters written to output.
       
    60      * @param input byte array containing text in character set
       
    61      * @param inStart offset in input array
       
    62      * @param inEnd offset of last byte to be converted
       
    63      * @param output character array to receive conversion result
       
    64      * @param outStart starting offset
       
    65      * @param outEnd offset of last character to be written to
       
    66      * @throw MalformedInputException for any sequence of bytes that is
       
    67      * illegal for the input character set, including any partial multi-byte
       
    68      * sequence which occurs at the end of an input buffer.
       
    69      * @throw UnsupportedCharacterException for any sequence of bytes that
       
    70      * contain a character not supported in the current conversion.
       
    71      * @throw BufferFullException whenever the output buffer is full
       
    72      * before the input is exhausted.
       
    73      * @see #reset
       
    74      */
       
    75     public int convert(byte[] input, int inOff, int inEnd,
       
    76                        char[] output, int outOff, int outEnd)
       
    77         throws UnknownCharacterException,
       
    78                MalformedInputException,
       
    79                ConversionBufferFullException
       
    80     {
       
    81         char    outputChar;
       
    82         int     byteIndex;
       
    83 
       
    84         charOff = outOff;
       
    85         byteOff = inOff;
       
    86 
       
    87         // Loop until we hit the end of the input
       
    88         while(byteOff < inEnd) {
       
    89 
       
    90             byteIndex = input[byteOff];
       
    91 
       
    92             /* old source
       
    93              *outputChar = byteToCharTable[input[byteOff] + 128];
       
    94              */
       
    95             // Lookup the output character
       
    96             outputChar = getUnicode(byteIndex);
       
    97 
       
    98             // Is the output unmappable?
       
    99             if (outputChar == '\uFFFD') {
       
   100                 if (subMode) {
       
   101                     outputChar = subChars[0];
       
   102                 } else {
       
   103                     badInputLength = 1;
       
   104                     throw new UnknownCharacterException();
       
   105                 }
       
   106             }
       
   107 
       
   108             // If we don't have room for the output, throw an exception
       
   109             if (charOff >= outEnd)
       
   110                 throw new ConversionBufferFullException();
       
   111 
       
   112             // Put the character in the output buffer
       
   113             output[charOff]= outputChar;
       
   114             charOff++;
       
   115             byteOff++;
       
   116         }
       
   117 
       
   118         // Return the length written to the output buffer
       
   119         return charOff-outOff;
       
   120     }
       
   121 
       
   122     protected char getUnicode(int byteIndex) {
       
   123         int n = byteIndex + 128;
       
   124         if (n >= byteToCharTable.length() || n < 0)
       
   125             return '\uFFFD';
       
   126         return byteToCharTable.charAt(n);
       
   127     }
       
   128 
       
   129     /**
       
   130      *  Resets the converter.
       
   131      *  Call this method to reset the converter to its initial state
       
   132      */
       
   133     public void reset() {
       
   134         byteOff = charOff = 0;
       
   135     }
       
   136 
       
   137 }