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_KR;
  30 
  31 public class X11KSC5601_OLD extends Charset {
  32     public X11KSC5601_OLD () {
  33         super("X11KSC5601-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 X11KSC5601_OLD;
  44     }
  45 
  46     private class Encoder extends EUC_KR_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             byte[] da = dst.array();
  63             int dp = dst.arrayOffset() + dst.position();
  64             int dl = dst.arrayOffset() + dst.limit();
  65 
  66             try {
  67                 while (sp < sl) {
  68                     char c = sa[sp];
  69                     if (c <= '\u007f')
  70                         return CoderResult.unmappableForLength(1);
  71                     int ncode = encodeDouble(c);
  72                     if (ncode != 0 && c != '\u0000' ) {
  73                         da[dp++] = (byte) ((ncode  >> 8) & 0x7f);
  74                         da[dp++] = (byte) (ncode & 0x7f);
  75                         sp++;
  76                         continue;
  77                     }
  78                     return CoderResult.unmappableForLength(1);
  79                 }
  80                 return CoderResult.UNDERFLOW;
  81             } finally {
  82                 src.position(sp - src.arrayOffset());
  83                 dst.position(dp - dst.arrayOffset());
  84             }
  85         }
  86         public boolean isLegalReplacement(byte[] repl) {
  87             return true;
  88         }
  89     }
  90 
  91     private class Decoder extends EUC_KR_OLD.Decoder {
  92         public Decoder(Charset cs) {
  93             super(cs);
  94         }
  95 
  96         protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
  97             byte[] sa = src.array();
  98             int sp = src.arrayOffset() + src.position();
  99             int sl = src.arrayOffset() + src.limit();
 100             assert (sp <= sl);
 101             sp = (sp <= sl ? sp : sl);
 102             char[] da = dst.array();
 103             int dp = dst.arrayOffset() + dst.position();
 104             int dl = dst.arrayOffset() + dst.limit();
 105             assert (dp <= dl);
 106             dp = (dp <= dl ? dp : dl);
 107 
 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 }