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