jdk/src/share/classes/sun/io/CharToByteSingleByte.java
changeset 1713 f8b8bfa64fc1
parent 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
1712:8664f15a755b 1713:f8b8bfa64fc1
     1 /*
     1 /*
     2  * Copyright 1996-2002 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 1996-2008 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     7  * published by the Free Software Foundation.  Sun designates this
    23  * have any questions.
    23  * have any questions.
    24  */
    24  */
    25 
    25 
    26 package sun.io;
    26 package sun.io;
    27 
    27 
       
    28 import static sun.nio.cs.CharsetMapping.*;
       
    29 
    28 /**
    30 /**
    29 * A table driven conversion from char to byte for single byte
    31 * A table driven conversion from char to byte for single byte
    30 * character sets.  Tables will reside in the class CharToByteYYYYY,
    32 * character sets.  Tables will reside in the class CharToByteYYYYY,
    31 * where YYYYY is a unique character set identifier
    33 * where YYYYY is a unique character set identifier
    32 
    34 
    33     < TBD: Tables are of the form... >
    35     < TBD: Tables are of the form... >
    34 
    36 
    35 *
    37 *
    36 * @author Lloyd Honomichl
    38 * @author Lloyd Honomichl
    37 * @author Asmus Freytag
    39 * @author Asmus Freytag
       
    40 * @version 8/28/96
    38 */
    41 */
    39 
    42 
    40 public abstract class CharToByteSingleByte extends CharToByteConverter {
    43 public abstract class CharToByteSingleByte extends CharToByteConverter {
    41 
    44 
    42     /*
    45     /*
    43      * 1st level index, provided by subclass
    46      * 1st level index, provided by subclass
    44      */
    47      */
    45     protected short index1[];
    48     protected char[] index1;
    46 
    49 
    47     /*
    50     /*
    48      * 2nd level index, provided by subclass
    51      * 2nd level index, provided by subclass
    49      */
    52      */
    50     protected String  index2;
    53     protected char[] index2;
    51 
    54 
    52     /*
    55     /*
    53      * Mask to isolate bits for 1st level index, from subclass
    56      * Mask to isolate bits for 1st level index, from subclass
    54      */
    57      */
    55     protected int   mask1;
    58     protected int   mask1;
    64      */
    67      */
    65     protected int   shift;
    68     protected int   shift;
    66 
    69 
    67     private char highHalfZoneCode;
    70     private char highHalfZoneCode;
    68 
    71 
    69     public short[] getIndex1() {
    72     public char[] getIndex1() {
    70         return index1;
    73         return index1;
    71     }
    74     }
    72 
    75 
    73     public String getIndex2() {
    76     public char[] getIndex2() {
    74         return index2;
    77         return index2;
    75     }
    78     }
    76     public int flush(byte[] output, int outStart, int outEnd)
    79     public int flush(byte[] output, int outStart, int outEnd)
    77         throws MalformedInputException
    80         throws MalformedInputException
    78     {
    81     {
   227      */
   230      */
   228     public int getMaxBytesPerChar() {
   231     public int getMaxBytesPerChar() {
   229         return 1;
   232         return 1;
   230     }
   233     }
   231 
   234 
       
   235     int encodeChar(char ch) {
       
   236         char index = index1[ch >> 8];
       
   237         if (index == UNMAPPABLE_ENCODING)
       
   238             return UNMAPPABLE_ENCODING;
       
   239         return index2[index + (ch & 0xff)];
       
   240     }
       
   241 
   232     public byte getNative(char inputChar) {
   242     public byte getNative(char inputChar) {
   233         return (byte)index2.charAt(index1[(inputChar & mask1) >> shift]
   243         int b = encodeChar(inputChar);
   234                 + (inputChar & mask2));
   244         if (b == UNMAPPABLE_ENCODING)
       
   245             return 0;
       
   246         return (byte)b;
   235     }
   247     }
   236 
   248 
   237     /**
   249     /**
   238      *  Resets the converter.
   250      *  Resets the converter.
   239      * Call this method to reset the converter to its initial state
   251      * Call this method to reset the converter to its initial state
   246     /**
   258     /**
   247      * Return whether a character is mappable or not
   259      * Return whether a character is mappable or not
   248      * @return true if a character is mappable
   260      * @return true if a character is mappable
   249      */
   261      */
   250     public boolean canConvert(char ch) {
   262     public boolean canConvert(char ch) {
   251         // Look it up in the table
   263         return encodeChar(ch) != UNMAPPABLE_ENCODING;
   252         if (index2.charAt(index1[((ch & mask1) >> shift)] + (ch & mask2)) != '\u0000')
       
   253             return true;
       
   254 
       
   255         // Nulls are always mappable
       
   256         return (ch == '\u0000');
       
   257     }
   264     }
   258 }
   265 }