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