1 /*
   2  * Copyright (c) 1996, 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.awt.motif;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.nio.CharBuffer;
  30 import java.nio.charset.*;
  31 import sun.nio.cs.*;
  32 import sun.nio.cs.ext.JIS_X_0201;
  33 import static sun.nio.cs.CharsetMapping.*;
  34 
  35 public class X11JIS0201 extends Charset {
  36 
  37     private static Charset jis0201 = new JIS_X_0201();
  38     private static SingleByte.Encoder enc =
  39         (SingleByte.Encoder)jis0201.newEncoder();
  40 
  41     public X11JIS0201 () {
  42         super("X11JIS0201", null);
  43     }
  44 
  45     public CharsetEncoder newEncoder() {
  46         return new Encoder(this);
  47     }
  48 
  49     public CharsetDecoder newDecoder() {
  50         return jis0201.newDecoder();
  51     }
  52 
  53     public boolean contains(Charset cs) {
  54         return cs instanceof X11JIS0201;
  55     }
  56 
  57     private class Encoder extends CharsetEncoder {
  58 
  59         public Encoder(Charset cs) {
  60             super(cs, 1.0f, 1.0f);
  61         }
  62 
  63         public boolean canEncode(char c){
  64             if ((c >= 0xff61 && c <= 0xff9f)
  65                 || c == 0x203e
  66                 || c == 0xa5) {
  67                 return true;
  68             }
  69             return false;
  70         }
  71 
  72         private Surrogate.Parser sgp;
  73         protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
  74             char[] sa = src.array();
  75             int sp = src.arrayOffset() + src.position();
  76             int sl = src.arrayOffset() + src.limit();
  77 
  78             byte[] da = dst.array();
  79             int dp = dst.arrayOffset() + dst.position();
  80             int dl = dst.arrayOffset() + dst.limit();
  81             CoderResult cr = CoderResult.UNDERFLOW;
  82             if ((dl - dp) < (sl - sp)) {
  83                 sl = sp + (dl - dp);
  84                 cr = CoderResult.OVERFLOW;
  85             }
  86             try {
  87                 while (sp < sl) {
  88                     char c = sa[sp];
  89                     int b = enc.encode(c);
  90                     if (b == UNMAPPABLE_ENCODING) {
  91                         if (Character.isSurrogate(c)) {
  92                             if (sgp == null)
  93                                 sgp = new Surrogate.Parser();
  94                             if (sgp.parse(c, sa, sp, sl) >= 0)
  95                                 return CoderResult.unmappableForLength(2);
  96                         }
  97                         return CoderResult.unmappableForLength(1);
  98                     }
  99                     da[dp++] = (byte)b;
 100                     sp++;
 101                 }
 102                 return cr;
 103             } finally {
 104                 src.position(sp - src.arrayOffset());
 105                 dst.position(dp - dst.arrayOffset());
 106             }
 107         }
 108     }
 109 }