1 /*
   2  * Copyright (c) 2008, 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.charset.Charset;
  29 import java.nio.charset.CharsetEncoder;
  30 import java.nio.charset.CharsetDecoder;
  31 import sun.nio.cs.DoubleByte;
  32 import sun.nio.cs.*;
  33 import static sun.nio.cs.CharsetMapping.*;
  34 
  35 public class MS932_0213 extends Charset {
  36     public MS932_0213() {
  37         super("x-MS932_0213", ExtendedCharsets.aliasesFor("MS932_0213"));
  38     }
  39 
  40     public boolean contains(Charset cs) {
  41         return ((cs.name().equals("US-ASCII"))
  42                 || (cs instanceof MS932)
  43                 || (cs instanceof MS932_0213));
  44     }
  45 
  46     public CharsetDecoder newDecoder() {
  47         return new Decoder(this);
  48     }
  49 
  50     public CharsetEncoder newEncoder() {
  51         return new Encoder(this);
  52     }
  53 
  54     protected static class Decoder extends SJIS_0213.Decoder {
  55         static DoubleByte.Decoder decMS932 =
  56             (DoubleByte.Decoder)new MS932().newDecoder();
  57         protected Decoder(Charset cs) {
  58             super(cs);
  59         }
  60 
  61         protected char decodeDouble(int b1, int b2) {
  62             char c = decMS932.decodeDouble(b1, b2);
  63             if (c == UNMAPPABLE_DECODING)
  64                 return super.decodeDouble(b1, b2);
  65             return c;
  66         }
  67     }
  68 
  69     protected static class Encoder extends SJIS_0213.Encoder {
  70         // we only use its encodeChar() method
  71         static DoubleByte.Encoder encMS932 =
  72             (DoubleByte.Encoder)new MS932().newEncoder();
  73         protected Encoder(Charset cs) {
  74             super(cs);
  75         }
  76 
  77         protected int encodeChar(char ch) {
  78             int db = encMS932.encodeChar(ch);
  79             if (db == UNMAPPABLE_ENCODING)
  80                 return super.encodeChar(ch);
  81             return db;
  82         }
  83     }
  84 }