001: /*
002: * Copyright 2001-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.motif;
027:
028: import java.nio.CharBuffer;
029: import java.nio.ByteBuffer;
030: import java.nio.charset.*;
031: import sun.nio.cs.ext.EUC_TW;
032:
033: public abstract class X11CNS11643 extends Charset {
034: private final int plane;
035:
036: public X11CNS11643(int plane, String name) {
037: super (name, null);
038: switch (plane) {
039: case 1:
040: this .plane = 0; // CS1
041: break;
042: case 2:
043: case 3:
044: this .plane = plane;
045: break;
046: default:
047: throw new IllegalArgumentException(
048: "Only planes 1, 2, and 3 supported");
049: }
050: }
051:
052: public CharsetEncoder newEncoder() {
053: return new Encoder(this , plane);
054: }
055:
056: public CharsetDecoder newDecoder() {
057: return new Decoder(this , plane);
058: }
059:
060: public boolean contains(Charset cs) {
061: return cs instanceof X11CNS11643;
062: }
063:
064: private class Encoder extends EUC_TW.Encoder {
065: private int plane;
066:
067: public Encoder(Charset cs, int plane) {
068: super (cs);
069: this .plane = plane;
070: }
071:
072: public boolean canEncode(char c) {
073: if (c <= 0x7F) {
074: return false;
075: }
076: int p = getNative(c) >> 16;
077: if (p == 1 && plane == 0 || p == 2 && plane == 2 || p == 3
078: && plane == 3)
079: return true;
080: return false;
081: }
082:
083: public boolean isLegalReplacement(byte[] repl) {
084: return true;
085: }
086:
087: protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
088: char[] sa = src.array();
089: int sp = src.arrayOffset() + src.position();
090: int sl = src.arrayOffset() + src.limit();
091: byte[] da = dst.array();
092: int dp = dst.arrayOffset() + dst.position();
093: int dl = dst.arrayOffset() + dst.limit();
094:
095: try {
096: while (sp < sl) {
097: char c = sa[sp];
098: if (c >= '\uFFFE' || c <= '\u007f')
099: return CoderResult.unmappableForLength(1);
100: int cns = getNative(c);
101: int p = cns >> 16;
102: if (p == 1 && plane == 0 || p == 2 && plane == 2
103: || p == 3 && plane == 3) {
104: if (dl - dp < 2)
105: return CoderResult.OVERFLOW;
106: da[dp++] = (byte) ((cns >> 8) & 0x7f);
107: da[dp++] = (byte) (cns & 0x7f);
108: sp++;
109: continue;
110: }
111: return CoderResult.unmappableForLength(1);
112: }
113: return CoderResult.UNDERFLOW;
114: } finally {
115: src.position(sp - src.arrayOffset());
116: dst.position(dp - dst.arrayOffset());
117: }
118: }
119: }
120:
121: private class Decoder extends EUC_TW.Decoder {
122: private String table;
123:
124: protected Decoder(Charset cs, int plane) {
125: super (cs);
126: switch (plane) {
127: case 0:
128: table = unicodeCNS1;
129: break;
130: case 2:
131: table = unicodeCNS2;
132: break;
133: case 3:
134: table = unicodeCNS3;
135: break;
136: default:
137: throw new IllegalArgumentException(
138: "Only planes 1, 2, and 3 supported");
139: }
140: }
141:
142: //we only work on array backed buffer.
143: protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
144: byte[] sa = src.array();
145: int sp = src.arrayOffset() + src.position();
146: int sl = src.arrayOffset() + src.limit();
147: assert (sp <= sl);
148: sp = (sp <= sl ? sp : sl);
149:
150: char[] da = dst.array();
151: int dp = dst.arrayOffset() + dst.position();
152: int dl = dst.arrayOffset() + dst.limit();
153: assert (dp <= dl);
154: dp = (dp <= dl ? dp : dl);
155:
156: try {
157: while (sp < sl) {
158: if (sl - sp < 2) {
159: return CoderResult.UNDERFLOW;
160: }
161: byte b1 = sa[sp];
162: byte b2 = sa[sp + 1];
163: char c = convToUnicode((byte) (b1 | 0x80),
164: (byte) (b2 | 0x80), table);
165: if (c == replacement().charAt(0)
166: //to keep the compatibility with b2cX11CNS11643
167: /*|| c == '\u0000'*/) {
168: return CoderResult.unmappableForLength(2);
169: }
170: if (dl - dp < 1)
171: return CoderResult.OVERFLOW;
172: da[dp++] = c;
173: sp += 2;
174: }
175: return CoderResult.UNDERFLOW;
176: } finally {
177: src.position(sp - src.arrayOffset());
178: dst.position(dp - dst.arrayOffset());
179: }
180: }
181: }
182: }
|