1 /*
   2  * Copyright (c) 2004, 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 
  26 package $PACKAGE$;
  27 
  28 import java.nio.charset.Charset;
  29 import java.nio.charset.CharsetDecoder;
  30 import java.nio.charset.CharsetEncoder;
  31 import sun.nio.cs.DoubleByte;
  32 import sun.nio.cs.HistoricallyNamedCharset;
  33 import java.util.Arrays;
  34 import static sun.nio.cs.CharsetMapping.*;
  35 
  36 public class Big5_Solaris extends Charset implements HistoricallyNamedCharset
  37 {
  38     public Big5_Solaris() {
  39         super("x-Big5-Solaris", $ALIASES$);
  40     }
  41 
  42     public String historicalName() {
  43         return "Big5_Solaris";
  44     }
  45 
  46     public boolean contains(Charset cs) {
  47         return ((cs.name().equals("US-ASCII"))
  48                 || (cs instanceof Big5)
  49                 || (cs instanceof Big5_Solaris));
  50     }
  51 
  52     public CharsetDecoder newDecoder() {
  53         initb2c();
  54         return new  DoubleByte.Decoder(this, b2c, b2cSB, 0x40, 0xfe);
  55     }
  56 
  57     public CharsetEncoder newEncoder() {
  58         initc2b();
  59         return new DoubleByte.Encoder(this, c2b, c2bIndex);
  60     }
  61 
  62     static char[][] b2c;
  63     static char[] b2cSB;
  64     private static volatile boolean b2cInitialized = false;
  65 
  66     static void initb2c() {
  67         if (b2cInitialized)
  68             return;
  69         synchronized (Big5_Solaris.class) {
  70             if (b2cInitialized)
  71                 return;
  72             Big5.initb2c();
  73             b2c = Big5.b2c.clone();
  74             // Big5 Solaris implementation has 7 additional mappings
  75             int[] sol = new int[] {
  76                 0xF9D6, 0x7881,
  77                 0xF9D7, 0x92B9,
  78                 0xF9D8, 0x88CF,
  79                 0xF9D9, 0x58BB,
  80                 0xF9DA, 0x6052,
  81                 0xF9DB, 0x7CA7,
  82                 0xF9DC, 0x5AFA };
  83             if (b2c[0xf9] == DoubleByte.B2C_UNMAPPABLE) {
  84                 b2c[0xf9] = new char[0xfe - 0x40 + 1];
  85                 Arrays.fill(b2c[0xf9], UNMAPPABLE_DECODING);
  86             }
  87 
  88             for (int i = 0; i < sol.length;) {
  89                 b2c[0xf9][sol[i++] & 0xff - 0x40] = (char)sol[i++];
  90             }
  91             b2cSB = Big5.b2cSB;
  92             b2cInitialized = true;
  93         }
  94     }
  95 
  96     static char[] c2b;
  97     static char[] c2bIndex;
  98     private static volatile boolean c2bInitialized = false;
  99 
 100     static void initc2b() {
 101         if (c2bInitialized)
 102             return;
 103         synchronized (Big5_Solaris.class) {
 104             if (c2bInitialized)
 105                 return;
 106             Big5.initc2b();
 107             c2b = Big5.c2b.clone();
 108             c2bIndex = Big5.c2bIndex.clone();
 109             int[] sol = new int[] {
 110                 0x7881, 0xF9D6,
 111                 0x92B9, 0xF9D7,
 112                 0x88CF, 0xF9D8,
 113                 0x58BB, 0xF9D9,
 114                 0x6052, 0xF9DA,
 115                 0x7CA7, 0xF9DB,
 116                 0x5AFA, 0xF9DC };
 117 
 118             for (int i = 0; i < sol.length;) {
 119                 int c = sol[i++];
 120                 // no need to check c2bIndex[c >>8], we know it points
 121                 // to the appropriate place.
 122                 c2b[c2bIndex[c >> 8] + (c & 0xff)] = (char)sol[i++];
 123             }
 124             c2bInitialized = true;
 125         }
 126     }
 127 }