1 /* 2 * Copyright (c) 2001, 2008, 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 34 public abstract class X11CNS11643 extends Charset { 35 private final int plane; 36 public X11CNS11643 (int plane, String name) { 37 super(name, null); 38 switch (plane) { 39 case 1: 40 this.plane = 0; // CS1 41 break; 42 case 2: 43 case 3: 44 this.plane = plane; 45 break; 46 default: 47 throw new IllegalArgumentException 48 ("Only planes 1, 2, and 3 supported"); 49 } 50 } 51 52 public CharsetEncoder newEncoder() { 53 return new Encoder(this, plane); 54 } 55 56 public CharsetDecoder newDecoder() { 57 return new Decoder(this, plane); 58 } 59 60 public boolean contains(Charset cs) { 61 return cs instanceof X11CNS11643; 62 } 63 64 private class Encoder extends EUC_TW.Encoder { 65 private int plane; 66 public Encoder(Charset cs, int plane) { 67 super(cs); 68 this.plane = plane; 69 } 70 71 private byte[] bb = new byte[4]; 72 public boolean canEncode(char c) { 73 if (c <= 0x7F) { 74 return false; 75 } 76 int nb = toEUC(c, bb); 77 if (nb == -1) 78 return false; 79 int p = 0; 80 if (nb == 4) 81 p = (bb[1] & 0xff) - 0xa0; 82 return (p == plane); 83 } 84 85 public boolean isLegalReplacement(byte[] repl) { 86 return true; 87 } 88 89 protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { 90 char[] sa = src.array(); 91 int sp = src.arrayOffset() + src.position(); 92 int sl = src.arrayOffset() + src.limit(); 93 byte[] da = dst.array(); 94 int dp = dst.arrayOffset() + dst.position(); 95 int dl = dst.arrayOffset() + dst.limit(); 96 97 try { 98 while (sp < sl) { 99 char c = sa[sp]; 100 if ( c > '\u007f'&& c < '\uFFFE') { 101 int nb = toEUC(c, bb); 102 if (nb != -1) { 103 int p = 0; 104 if (nb == 4) 105 p = (bb[1] & 0xff) - 0xa0; 106 if (p == plane) { 107 if (dl - dp < 2) 108 return CoderResult.OVERFLOW; 109 if (nb == 2) { 110 da[dp++] = (byte)(bb[0] & 0x7f); 111 da[dp++] = (byte)(bb[1] & 0x7f); 112 } else { 113 da[dp++] = (byte)(bb[2] & 0x7f); 114 da[dp++] = (byte)(bb[3] & 0x7f); 115 } 116 sp++; 117 continue; 118 } 119 } 120 } 121 return CoderResult.unmappableForLength(1); 122 } 123 return CoderResult.UNDERFLOW; 124 } finally { 125 src.position(sp - src.arrayOffset()); 126 dst.position(dp - dst.arrayOffset()); 127 } 128 } 129 } 130 131 private class Decoder extends EUC_TW.Decoder { 132 int plane; 133 private String table; 134 protected Decoder(Charset cs, int plane) { 135 super(cs); 136 if (plane == 0) 137 this.plane = plane; 138 else if (plane == 2 || plane == 3) 139 this.plane = plane - 1; 140 else 141 throw new IllegalArgumentException 142 ("Only planes 1, 2, and 3 supported"); 143 } 144 145 //we only work on array backed buffer. 146 protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) { 147 byte[] sa = src.array(); 148 int sp = src.arrayOffset() + src.position(); 149 int sl = src.arrayOffset() + src.limit(); 150 151 char[] da = dst.array(); 152 int dp = dst.arrayOffset() + dst.position(); 153 int dl = dst.arrayOffset() + dst.limit(); 154 155 try { 156 while (sp < sl) { 157 if ( sl - sp < 2) { 158 return CoderResult.UNDERFLOW; 159 } 160 int b1 = (sa[sp] & 0xff) | 0x80; 161 int b2 = (sa[sp + 1] & 0xff) | 0x80; 162 char[] cc = toUnicode(b1, b2, plane); 163 // plane3 has non-bmp characters(added), x11cnsp3 164 // however does not support them 165 if (cc == null || cc.length == 2) 166 return CoderResult.unmappableForLength(2); 167 if (dl - dp < 1) 168 return CoderResult.OVERFLOW; 169 da[dp++] = cc[0]; 170 sp +=2; 171 } 172 return CoderResult.UNDERFLOW; 173 } finally { 174 src.position(sp - src.arrayOffset()); 175 dst.position(dp - dst.arrayOffset()); 176 } 177 } 178 } 179 } --- EOF ---