1 /*
   2  * Copyright (c) 2003, 2015, 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.ByteBuffer;
  29 import java.nio.CharBuffer;
  30 import java.nio.charset.Charset;
  31 import java.nio.charset.CharsetDecoder;
  32 import java.nio.charset.CharsetEncoder;
  33 import java.nio.charset.CoderResult;
  34 import sun.nio.cs.DoubleByte;
  35 import sun.nio.cs.HistoricallyNamedCharset;
  36 import sun.nio.cs.*;
  37 import static sun.nio.cs.CharsetMapping.*;
  38 
  39 public class EUC_JP_Open
  40     extends Charset
  41     implements HistoricallyNamedCharset
  42 {
  43     public EUC_JP_Open() {
  44         super("x-eucJP-Open", $ALIASES$);
  45     }
  46 
  47     public String historicalName() {
  48         return "EUC_JP_Solaris";
  49     }
  50 
  51     public boolean contains(Charset cs) {
  52         return ((cs.name().equals("US-ASCII"))
  53                 || (cs instanceof JIS_X_0201)
  54                 || (cs instanceof EUC_JP));
  55     }
  56 
  57     public CharsetDecoder newDecoder() {
  58         return new Decoder(this);
  59     }
  60 
  61     public CharsetEncoder newEncoder() {
  62         return new Encoder(this);
  63     }
  64 
  65     private static class Decoder extends EUC_JP.Decoder {
  66         private static DoubleByte.Decoder DEC0208_Solaris =
  67             (DoubleByte.Decoder)new JIS_X_0208_Solaris().newDecoder();
  68         private static DoubleByte.Decoder DEC0212_Solaris =
  69             (DoubleByte.Decoder)new JIS_X_0212_Solaris().newDecoder();
  70 
  71         private Decoder(Charset cs) {
  72             // JIS_X_0208_Solaris only has the "extra" mappings, it
  73             // does not have the JIS_X_0208 entries
  74             super(cs, 0.5f, 1.0f, DEC0201, DEC0208, DEC0212_Solaris);
  75         }
  76 
  77         protected char decodeDouble(int byte1, int byte2) {
  78             char c = super.decodeDouble(byte1, byte2);
  79             if (c == UNMAPPABLE_DECODING)
  80                 return DEC0208_Solaris.decodeDouble(byte1 - 0x80, byte2 - 0x80);
  81             return c;
  82         }
  83     }
  84 
  85     private static class Encoder extends EUC_JP.Encoder {
  86         private static DoubleByte.Encoder ENC0208_Solaris =
  87             (DoubleByte.Encoder)new JIS_X_0208_Solaris().newEncoder();
  88 
  89         private static DoubleByte.Encoder ENC0212_Solaris =
  90             (DoubleByte.Encoder)new JIS_X_0212_Solaris().newEncoder();
  91 
  92         private Encoder(Charset cs) {
  93             // The EUC_JP_Open has some interesting tweak for the
  94             // encoding, so can't just pass the euc0208_solaris to
  95             // the euc_jp. Have to override the encodeDouble() as
  96             // showed below (mapping testing catches this).
  97             // super(cs, 3.0f, 3.0f, ENC0201, ENC0208_Solaris, ENC0212_Solaris);
  98             super(cs);
  99         }
 100 
 101         protected int encodeDouble(char ch) {
 102             int b = super.encodeDouble(ch);
 103             if (b != UNMAPPABLE_ENCODING)
 104                 return b;
 105             b = ENC0208_Solaris.encodeChar(ch);
 106             if (b != UNMAPPABLE_ENCODING && b > 0x7500) {
 107                 return 0x8F8080 + ENC0212_Solaris.encodeChar(ch);
 108             }
 109             return b == UNMAPPABLE_ENCODING ? b : b + 0x8080;
 110 
 111         }
 112     }
 113 }