jdk/src/share/classes/sun/io/CharToByteDBCS_EBCDIC.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, 2010, 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 CharToByteDBCS_EBCDIC extends CharToByteConverter
       
    31 {
       
    32     private static final int SBCS = 0;
       
    33     private static final int DBCS = 1;
       
    34 
       
    35     private static final byte SO = 0x0e;
       
    36     private static final byte SI = 0x0f;
       
    37 
       
    38     private int  currentState;
       
    39     private char highHalfZoneCode;
       
    40     private byte[] outputByte = new byte[2];
       
    41 
       
    42     private DoubleByte.Encoder enc;
       
    43 
       
    44     public CharToByteDBCS_EBCDIC(DoubleByte.Encoder enc) {
       
    45         super();
       
    46         highHalfZoneCode = 0;
       
    47         currentState = SBCS;
       
    48         this.enc = enc;
       
    49     }
       
    50 
       
    51     int encodeChar(char c) {
       
    52         return enc.encodeChar(c);
       
    53     }
       
    54 
       
    55     /**
       
    56       * flush out any residual data and reset the buffer state
       
    57       */
       
    58     public int flush(byte [] output, int outStart, int outEnd)
       
    59         throws MalformedInputException, ConversionBufferFullException
       
    60     {
       
    61         int bytesOut = 0;
       
    62 
       
    63         if (highHalfZoneCode != 0) {
       
    64             reset();
       
    65             badInputLength = 0;
       
    66             throw new MalformedInputException();
       
    67         }
       
    68 
       
    69         if (currentState == DBCS) {
       
    70           if (outStart >= outEnd)
       
    71             throw new ConversionBufferFullException();
       
    72           output[outStart] = SI;
       
    73           bytesOut++;
       
    74         }
       
    75 
       
    76         reset();
       
    77         return bytesOut;
       
    78     }
       
    79 
       
    80     /**
       
    81      * Character conversion
       
    82      */
       
    83     public int convert(char[] input, int inOff, int inEnd,
       
    84                        byte[] output, int outOff, int outEnd)
       
    85         throws UnknownCharacterException, MalformedInputException,
       
    86                ConversionBufferFullException
       
    87     {
       
    88         char    inputChar;
       
    89         int     inputSize;
       
    90 
       
    91         byteOff = outOff;
       
    92         charOff = inOff;
       
    93 
       
    94         while(charOff < inEnd) {
       
    95 
       
    96            int   index;
       
    97            int   theBytes;
       
    98            int   spaceNeeded;
       
    99 
       
   100            if (highHalfZoneCode == 0) {
       
   101               inputChar = input[charOff];
       
   102               inputSize = 1;
       
   103            } else {
       
   104               inputChar = highHalfZoneCode;
       
   105               inputSize = 0;
       
   106               highHalfZoneCode = 0;
       
   107            }
       
   108 
       
   109            // Is this a high surrogate?
       
   110            if (Character.isHighSurrogate(inputChar)) {
       
   111               // Is this the last character of the input?
       
   112               if (charOff + inputSize >= inEnd) {
       
   113                  highHalfZoneCode = inputChar;
       
   114                  charOff += inputSize;
       
   115                  break;
       
   116               }
       
   117 
       
   118               // Is there a low surrogate following?
       
   119               inputChar = input[charOff + inputSize];
       
   120               if (Character.isLowSurrogate(inputChar)) {
       
   121                  // We have a valid surrogate pair.  Too bad we don't do
       
   122                  // surrogates.  Is substitution enabled?
       
   123                  if (subMode) {
       
   124                     if (subBytes.length == 1) {
       
   125                        outputByte[0] = 0x00;
       
   126                        outputByte[1] = subBytes[0];
       
   127                     }
       
   128                     else {
       
   129                        outputByte[0] = subBytes[0];
       
   130                        outputByte[1] = subBytes[1];
       
   131                     }
       
   132                     inputSize++;
       
   133                  } else {
       
   134                     badInputLength = 2;
       
   135                     throw new UnknownCharacterException();
       
   136                  }
       
   137               } else {
       
   138                  // We have a malformed surrogate pair
       
   139                  badInputLength = 1;
       
   140                  throw new MalformedInputException();
       
   141               }
       
   142            }
       
   143            // Is this an unaccompanied low surrogate?
       
   144            else if (Character.isLowSurrogate(inputChar)) {
       
   145                badInputLength = 1;
       
   146                throw new MalformedInputException();
       
   147            } else {
       
   148 
       
   149                // We have a valid character, get the bytes for it
       
   150                theBytes = encodeChar(inputChar);
       
   151                if (theBytes == UNMAPPABLE_ENCODING) {
       
   152                    // if there was no mapping - look for substitution characters
       
   153                    if (subMode) {
       
   154                        if (subBytes.length == 1) {
       
   155                            outputByte[0] = 0x00;
       
   156                            outputByte[1] = subBytes[0];
       
   157                        } else {
       
   158                            outputByte[0] = subBytes[0];
       
   159                            outputByte[1] = subBytes[1];
       
   160                        }
       
   161                    } else {
       
   162                        badInputLength = 1;
       
   163                        throw new UnknownCharacterException();
       
   164                    }
       
   165                } else {
       
   166                    outputByte[0] = (byte)((theBytes & 0x0000ff00)>>8);
       
   167                    outputByte[1] = (byte)(theBytes & 0x000000ff);
       
   168                }
       
   169            }
       
   170 
       
   171            //Set the output buffer into the correct state
       
   172 
       
   173            if (currentState == DBCS && outputByte[0] == 0x00) {
       
   174               if (byteOff >= outEnd)
       
   175                  throw new ConversionBufferFullException();
       
   176               currentState = SBCS;
       
   177               output[byteOff++] = SI;
       
   178            } else
       
   179               if (currentState == SBCS && outputByte[0] != 0x00) {
       
   180                  if (byteOff >= outEnd) {
       
   181                     throw new ConversionBufferFullException();
       
   182                  }
       
   183                  currentState = DBCS;
       
   184                  output[byteOff++] = SO;
       
   185               }
       
   186 
       
   187            if (currentState == DBCS)
       
   188               spaceNeeded = 2;
       
   189            else
       
   190               spaceNeeded = 1;
       
   191 
       
   192            if (byteOff + spaceNeeded > outEnd) {
       
   193               throw new ConversionBufferFullException();
       
   194            }
       
   195 
       
   196            if (currentState == SBCS)
       
   197               output[byteOff++] = outputByte[1];
       
   198            else {
       
   199               output[byteOff++] = outputByte[0];
       
   200               output[byteOff++] = outputByte[1];
       
   201            }
       
   202 
       
   203            charOff += inputSize;
       
   204         }
       
   205         return byteOff - outOff;
       
   206     }
       
   207 
       
   208 
       
   209 
       
   210     /**
       
   211      * Resets converter to its initial state.
       
   212      */
       
   213     public void reset() {
       
   214        charOff = byteOff = 0;
       
   215        highHalfZoneCode = 0;
       
   216        currentState = SBCS;
       
   217     }
       
   218 
       
   219 
       
   220     /**
       
   221      * Returns the maximum number of bytes needed to convert a char.
       
   222      */
       
   223     public int getMaxBytesPerChar() {
       
   224        return 4;    //Fixed with bug 4199599 so tests would pass.
       
   225     }
       
   226 
       
   227 
       
   228     /**
       
   229      * Sets the substitution bytes to use when the converter is in
       
   230      * substitution mode.  The given bytes should represent a valid
       
   231      * character in the target character encoding.
       
   232      */
       
   233 
       
   234     public void setSubstitutionBytes( byte[] newSubBytes )
       
   235        throws IllegalArgumentException
       
   236     {
       
   237        if( newSubBytes.length > 2 || newSubBytes.length == 0) {
       
   238            throw new IllegalArgumentException();
       
   239        }
       
   240 
       
   241        subBytes = new byte[ newSubBytes.length ];
       
   242        System.arraycopy( newSubBytes, 0, subBytes, 0, newSubBytes.length );
       
   243 
       
   244     }
       
   245 
       
   246     /**
       
   247      * Returns true if the given character can be converted to the
       
   248      * target character encoding.
       
   249      */
       
   250     public boolean canConvert(char c) {
       
   251         return encodeChar(c) != UNMAPPABLE_ENCODING;
       
   252     }
       
   253 }