1 /*
   2  * Copyright (c) 1996, 2005, 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.awt.motif;
  27 
  28 import java.nio.CharBuffer;
  29 import java.nio.ByteBuffer;
  30 import java.nio.charset.*;
  31 import sun.nio.cs.*;
  32 import sun.nio.cs.ext.*;
  33 import static sun.nio.cs.CharsetMapping.*;
  34 
  35 public class X11GB2312 extends Charset {
  36     public X11GB2312 () {
  37         super("X11GB2312", null);
  38     }
  39     public CharsetEncoder newEncoder() {
  40         return new Encoder(this);
  41     }
  42     public CharsetDecoder newDecoder() {
  43         return new Decoder(this);
  44     }
  45 
  46     public boolean contains(Charset cs) {
  47         return cs instanceof X11GB2312;
  48     }
  49 
  50     private class Encoder extends CharsetEncoder {
  51         private DoubleByte.Encoder enc = (DoubleByte.Encoder)new EUC_CN().newEncoder();
  52 
  53         public Encoder(Charset cs) {
  54             super(cs, 2.0f, 2.0f);
  55         }
  56 
  57         public boolean canEncode(char c) {
  58             if (c <= 0x7F) {
  59                 return false;
  60             }
  61             return enc.canEncode(c);
  62         }
  63 
  64         protected int encodeDouble(char c) {
  65             return enc.encodeChar(c);
  66         }
  67 
  68         protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
  69             char[] sa = src.array();
  70             int sp = src.arrayOffset() + src.position();
  71             int sl = src.arrayOffset() + src.limit();
  72 
  73             byte[] da = dst.array();
  74             int dp = dst.arrayOffset() + dst.position();
  75             int dl = dst.arrayOffset() + dst.limit();
  76 
  77             try {
  78                 while (sp < sl) {
  79                     char c = sa[sp];
  80                     if (c <= '\u007f')
  81                         return CoderResult.unmappableForLength(1);
  82                     int ncode = encodeDouble(c);
  83                     if (ncode != 0 && c != '\u0000' ) {
  84                         da[dp++] = (byte) ((ncode  >> 8) & 0x7f);
  85                         da[dp++] = (byte) (ncode & 0x7f);
  86                         sp++;
  87                         continue;
  88                     }
  89                     return CoderResult.unmappableForLength(1);
  90                 }
  91                 return CoderResult.UNDERFLOW;
  92             } finally {
  93                 src.position(sp - src.arrayOffset());
  94                 dst.position(dp - dst.arrayOffset());
  95             }
  96         }
  97         public boolean isLegalReplacement(byte[] repl) {
  98             return true;
  99         }
 100     }
 101 
 102     private class Decoder extends  CharsetDecoder {
 103         private DoubleByte.Decoder dec = (DoubleByte.Decoder)new EUC_CN().newDecoder();
 104 
 105         public Decoder(Charset cs) {
 106             super(cs, 0.5f, 1.0f);
 107         }
 108 
 109         protected char decodeDouble(int b1, int b2) {
 110             return dec.decodeDouble(b1, b2);
 111         }
 112 
 113         protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
 114             byte[] sa = src.array();
 115             int sp = src.arrayOffset() + src.position();
 116             int sl = src.arrayOffset() + src.limit();
 117             assert (sp <= sl);
 118             sp = (sp <= sl ? sp : sl);
 119             char[] da = dst.array();
 120             int dp = dst.arrayOffset() + dst.position();
 121             int dl = dst.arrayOffset() + dst.limit();
 122             assert (dp <= dl);
 123             dp = (dp <= dl ? dp : dl);
 124 
 125             try {
 126                 while (sp < sl) {
 127                     if ( sl - sp < 2) {
 128                         return CoderResult.UNDERFLOW;
 129                     }
 130                     int b1 = sa[sp] & 0xFF | 0x80;
 131                     int b2 = sa[sp + 1] & 0xFF | 0x80;
 132                     char c = decodeDouble(b1, b2);
 133                     if (c == UNMAPPABLE_DECODING) {
 134                         return CoderResult.unmappableForLength(2);
 135                     }
 136                     if (dl - dp < 1)
 137                         return CoderResult.OVERFLOW;
 138                     da[dp++] = c;
 139                     sp +=2;
 140                 }
 141                 return CoderResult.UNDERFLOW;
 142             } finally {
 143                 src.position(sp - src.arrayOffset());
 144                 dst.position(dp - dst.arrayOffset());
 145             }
 146 
 147         }
 148     }
 149 
 150 }