src/solaris/classes/sun/awt/motif/X11CNS11643.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright 2001-2005 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 2001-2008 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Sun designates this

@@ -64,20 +64,23 @@
         private int plane;
         public Encoder(Charset cs, int plane) {
             super(cs);
             this.plane = plane;
         }
+
+        private byte[] bb = new byte[4];
         public boolean canEncode(char c) {
             if (c <= 0x7F) {
                 return false;
             }
-            int p = getNative(c) >> 16;
-            if (p == 1 && plane == 0 ||
-                p == 2 && plane == 2 ||
-                p == 3 && plane == 3)
-                return true;
+            int nb = toEUC(c, bb);
+            if (nb == -1)
             return false;
+            int p = 0;
+            if (nb == 4)
+                p = (bb[1] & 0xff) - 0xa0;
+            return (p == plane);
         }
 
         public boolean isLegalReplacement(byte[] repl) {
             return true;
         }

@@ -91,24 +94,31 @@
             int dl = dst.arrayOffset() + dst.limit();
 
             try {
                 while (sp < sl) {
                     char c = sa[sp];
-                    if (c >= '\uFFFE' || c <= '\u007f')
-                        return CoderResult.unmappableForLength(1);
-                    int cns = getNative(c);
-                    int p = cns >> 16;
-                    if (p == 1 && plane == 0 ||
-                        p == 2 && plane == 2 ||
-                        p == 3 && plane == 3) {
+                    if ( c > '\u007f'&& c < '\uFFFE') {
+                        int nb = toEUC(c, bb);
+                        if (nb != -1) {
+                            int p = 0;
+                            if (nb == 4)
+                                p = (bb[1] & 0xff) - 0xa0;
+                            if (p == plane) {
                         if (dl - dp < 2)
                             return CoderResult.OVERFLOW;
-                        da[dp++] = (byte) ((cns  >> 8) & 0x7f);
-                        da[dp++] = (byte) (cns & 0x7f);
+                                if (nb == 2) {
+                                    da[dp++] = (byte)(bb[0] & 0x7f);
+                                    da[dp++] = (byte)(bb[1] & 0x7f);
+                                } else {
+                                    da[dp++] = (byte)(bb[2] & 0x7f);
+                                    da[dp++] = (byte)(bb[3] & 0x7f);
+                                }
                         sp++;
                         continue;
                     }
+                        }
+                    }
                     return CoderResult.unmappableForLength(1);
                 }
                 return CoderResult.UNDERFLOW;
             } finally {
                 src.position(sp - src.arrayOffset());

@@ -116,61 +126,48 @@
             }
         }
     }
 
     private class Decoder extends EUC_TW.Decoder {
+        int plane;
         private String table;
         protected Decoder(Charset cs, int plane) {
             super(cs);
-            switch (plane) {
-            case 0:
-                table = unicodeCNS1;
-                break;
-            case 2:
-                table = unicodeCNS2;
-                break;
-            case 3:
-                table = unicodeCNS3;
-                break;
-            default:
+            if (plane == 0)
+                this.plane = plane;
+            else if (plane == 2 || plane == 3)
+                this.plane = plane - 1;
+            else
                 throw new IllegalArgumentException
                     ("Only planes 1, 2, and 3 supported");
             }
-        }
 
         //we only work on array backed buffer.
         protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
             byte[] sa = src.array();
             int sp = src.arrayOffset() + src.position();
             int sl = src.arrayOffset() + src.limit();
-            assert (sp <= sl);
-            sp = (sp <= sl ? sp : sl);
 
             char[] da = dst.array();
             int dp = dst.arrayOffset() + dst.position();
             int dl = dst.arrayOffset() + dst.limit();
-            assert (dp <= dl);
-            dp = (dp <= dl ? dp : dl);
 
             try {
                 while (sp < sl) {
                     if ( sl - sp < 2) {
                         return CoderResult.UNDERFLOW;
                     }
-                    byte b1 = sa[sp];
-                    byte b2 = sa[sp + 1];
-                    char c = convToUnicode((byte)(b1 | 0x80),
-                                           (byte)(b2 | 0x80),
-                                           table);
-                    if (c == replacement().charAt(0)
-                        //to keep the compatibility with b2cX11CNS11643
-                        /*|| c == '\u0000'*/) {
+                    int b1 = (sa[sp] & 0xff) | 0x80;
+                    int b2 = (sa[sp + 1] & 0xff) | 0x80;
+                    char[] cc = toUnicode(b1, b2, plane);
+                    // plane3 has non-bmp characters(added), x11cnsp3
+                    // however does not support them
+                    if (cc == null || cc.length == 2)
                         return CoderResult.unmappableForLength(2);
-                    }
                     if (dl - dp < 1)
                         return CoderResult.OVERFLOW;
-                    da[dp++] = c;
+                    da[dp++] = cc[0];
                     sp +=2;
                 }
                 return CoderResult.UNDERFLOW;
             } finally {
                 src.position(sp - src.arrayOffset());