jdk/src/share/classes/sun/io/ByteToCharDBCS_ASCII.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) 1997, 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 package sun.io;
       
    26 
       
    27 import sun.nio.cs.ext.DoubleByte;
       
    28 import static sun.nio.cs.CharsetMapping.*;
       
    29 
       
    30 public abstract class ByteToCharDBCS_ASCII extends ByteToCharConverter
       
    31 {
       
    32     private boolean savedBytePresent;
       
    33     private int savedByte;
       
    34 
       
    35     private DoubleByte.Decoder dec;
       
    36 
       
    37     public ByteToCharDBCS_ASCII(DoubleByte.Decoder dec) {
       
    38         super();
       
    39         savedBytePresent = false;
       
    40         this.dec = dec;
       
    41     }
       
    42 
       
    43     char decodeSingle(int b) {
       
    44         return dec.decodeSingle(b);
       
    45     }
       
    46 
       
    47     char decodeDouble(int b1, int b2) {
       
    48         return dec.decodeDouble(b1, b2);
       
    49     }
       
    50 
       
    51     public int flush(char [] output, int outStart, int outEnd)
       
    52         throws MalformedInputException
       
    53     {
       
    54 
       
    55        if (savedBytePresent) {
       
    56            reset();
       
    57            badInputLength = 0;
       
    58            throw new MalformedInputException();
       
    59        }
       
    60 
       
    61        reset();
       
    62        return 0;
       
    63     }
       
    64 
       
    65     /**
       
    66      * Character conversion
       
    67      */
       
    68     public int convert(byte[] input, int inOff, int inEnd,
       
    69                        char[] output, int outOff, int outEnd)
       
    70         throws UnknownCharacterException, MalformedInputException,
       
    71                ConversionBufferFullException
       
    72     {
       
    73         int inputSize;
       
    74         char    outputChar = UNMAPPABLE_DECODING;
       
    75 
       
    76         charOff = outOff;
       
    77         byteOff = inOff;
       
    78 
       
    79         while(byteOff < inEnd)
       
    80         {
       
    81            int byte1;
       
    82 
       
    83            if (!savedBytePresent) {
       
    84               byte1 = input[byteOff] & 0xff;
       
    85               inputSize = 1;
       
    86            } else {
       
    87               byte1 = savedByte;
       
    88               savedBytePresent = false;
       
    89               inputSize = 0;
       
    90            }
       
    91 
       
    92            outputChar = decodeSingle(byte1);
       
    93            if (outputChar == UNMAPPABLE_DECODING) {
       
    94 
       
    95               if (byteOff + inputSize >= inEnd) {
       
    96                 savedByte = byte1;
       
    97                 savedBytePresent = true;
       
    98                 byteOff += inputSize;
       
    99                 break;
       
   100               }
       
   101 
       
   102               outputChar = decodeDouble(byte1, input[byteOff+inputSize] & 0xff);
       
   103               inputSize++;
       
   104            }
       
   105 
       
   106            if (outputChar == UNMAPPABLE_DECODING) {
       
   107               if (subMode)
       
   108                  outputChar = subChars[0];
       
   109               else {
       
   110                  badInputLength = inputSize;
       
   111                  throw new UnknownCharacterException();
       
   112               }
       
   113            }
       
   114 
       
   115            if (charOff >= outEnd)
       
   116               throw new ConversionBufferFullException();
       
   117 
       
   118            output[charOff++] = outputChar;
       
   119            byteOff += inputSize;
       
   120 
       
   121         }
       
   122 
       
   123         return charOff - outOff;
       
   124     }
       
   125 
       
   126     /**
       
   127      *  Resets the converter.
       
   128      */
       
   129     public void reset() {
       
   130        charOff = byteOff = 0;
       
   131        savedBytePresent = false;
       
   132     }
       
   133 }