1238N/A/*
2362N/A * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.
1238N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1238N/A *
1238N/A * This code is free software; you can redistribute it and/or modify it
1238N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1238N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1238N/A *
1238N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1238N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1238N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1238N/A * version 2 for more details (a copy is included in the LICENSE file that
1238N/A * accompanied this code).
1238N/A *
1238N/A * You should have received a copy of the GNU General Public License version
1238N/A * 2 along with this work; if not, write to the Free Software Foundation,
1238N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1238N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1238N/A */
1238N/A
1238N/Aimport java.nio.CharBuffer;
1238N/Aimport java.nio.ByteBuffer;
1238N/Aimport java.nio.charset.*;
1238N/Aimport sun.nio.cs.ext.EUC_TW;
1238N/A
1238N/Apublic abstract class X11CNS11643 extends Charset {
1238N/A private final int plane;
1238N/A public X11CNS11643 (int plane, String name) {
1238N/A super(name, null);
1238N/A switch (plane) {
1238N/A case 1:
1238N/A this.plane = 0; // CS1
1238N/A break;
1238N/A case 2:
1238N/A case 3:
1238N/A this.plane = plane;
1238N/A break;
1238N/A default:
1238N/A throw new IllegalArgumentException
1238N/A ("Only planes 1, 2, and 3 supported");
1238N/A }
1238N/A }
1238N/A
1238N/A public CharsetEncoder newEncoder() {
1238N/A return new Encoder(this, plane);
1238N/A }
1238N/A
1238N/A public CharsetDecoder newDecoder() {
1238N/A return new Decoder(this, plane);
1238N/A }
1238N/A
1238N/A public boolean contains(Charset cs) {
1238N/A return cs instanceof X11CNS11643;
1238N/A }
1238N/A
1238N/A private class Encoder extends EUC_TW_OLD.Encoder {
1238N/A private int plane;
1238N/A public Encoder(Charset cs, int plane) {
1238N/A super(cs);
1238N/A this.plane = plane;
1238N/A }
1238N/A public boolean canEncode(char c) {
1238N/A if (c <= 0x7F) {
1238N/A return false;
1238N/A }
1238N/A int p = getNative(c) >> 16;
1238N/A if (p == 1 && plane == 0 ||
1238N/A p == 2 && plane == 2 ||
1238N/A p == 3 && plane == 3)
1238N/A return true;
1238N/A return false;
1238N/A }
1238N/A
1238N/A public boolean isLegalReplacement(byte[] repl) {
1238N/A return true;
1238N/A }
1238N/A
1238N/A protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
1238N/A char[] sa = src.array();
1238N/A int sp = src.arrayOffset() + src.position();
1238N/A int sl = src.arrayOffset() + src.limit();
1238N/A byte[] da = dst.array();
1238N/A int dp = dst.arrayOffset() + dst.position();
1238N/A int dl = dst.arrayOffset() + dst.limit();
1238N/A
1238N/A try {
1238N/A while (sp < sl) {
1238N/A char c = sa[sp];
1238N/A if (c >= '\uFFFE' || c <= '\u007f')
1238N/A return CoderResult.unmappableForLength(1);
1238N/A int cns = getNative(c);
1238N/A int p = cns >> 16;
1238N/A if (p == 1 && plane == 0 ||
1238N/A p == 2 && plane == 2 ||
1238N/A p == 3 && plane == 3) {
1238N/A if (dl - dp < 2)
1238N/A return CoderResult.OVERFLOW;
1238N/A da[dp++] = (byte) ((cns >> 8) & 0x7f);
1238N/A da[dp++] = (byte) (cns & 0x7f);
1238N/A sp++;
1238N/A continue;
1238N/A }
1238N/A return CoderResult.unmappableForLength(1);
1238N/A }
1238N/A return CoderResult.UNDERFLOW;
1238N/A } finally {
1238N/A src.position(sp - src.arrayOffset());
1238N/A dst.position(dp - dst.arrayOffset());
1238N/A }
1238N/A }
1238N/A }
1238N/A
1238N/A private class Decoder extends EUC_TW_OLD.Decoder {
1238N/A private String table;
1238N/A protected Decoder(Charset cs, int plane) {
1238N/A super(cs);
1238N/A switch (plane) {
1238N/A case 0:
1238N/A table = unicodeCNS1;
1238N/A break;
1238N/A case 2:
1238N/A table = unicodeCNS2;
1238N/A break;
1238N/A case 3:
1238N/A table = unicodeCNS3;
1238N/A break;
1238N/A default:
1238N/A throw new IllegalArgumentException
1238N/A ("Only planes 1, 2, and 3 supported");
1238N/A }
1238N/A }
1238N/A
1238N/A //we only work on array backed buffer.
1238N/A protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
1238N/A byte[] sa = src.array();
1238N/A int sp = src.arrayOffset() + src.position();
1238N/A int sl = src.arrayOffset() + src.limit();
1238N/A assert (sp <= sl);
1238N/A sp = (sp <= sl ? sp : sl);
1238N/A
1238N/A char[] da = dst.array();
1238N/A int dp = dst.arrayOffset() + dst.position();
1238N/A int dl = dst.arrayOffset() + dst.limit();
1238N/A assert (dp <= dl);
1238N/A dp = (dp <= dl ? dp : dl);
1238N/A
1238N/A try {
1238N/A while (sp < sl) {
1238N/A if ( sl - sp < 2) {
1238N/A return CoderResult.UNDERFLOW;
1238N/A }
1238N/A byte b1 = sa[sp];
1238N/A byte b2 = sa[sp + 1];
1238N/A char c = replacement().charAt(0);
1238N/A
1238N/A if (table == unicodeCNS3) {
1238N/A char[] cc = convToSurrogate((byte)(b1 | 0x80),
1238N/A (byte)(b2 | 0x80),
1238N/A table);
1238N/A if (cc != null && cc[0] == '\u0000')
1238N/A c = cc[1];
1238N/A } else {
1238N/A c = convToUnicode((byte)(b1 | 0x80),
1238N/A (byte)(b2 | 0x80),
1238N/A table);
1238N/A }
1238N/A if (c == replacement().charAt(0)
1238N/A //to keep the compatibility with b2cX11CNS11643
1238N/A /*|| c == '\u0000'*/) {
1238N/A return CoderResult.unmappableForLength(2);
1238N/A }
1238N/A if (dl - dp < 1)
1238N/A return CoderResult.OVERFLOW;
1238N/A da[dp++] = c;
1238N/A sp +=2;
1238N/A }
1238N/A return CoderResult.UNDERFLOW;
1238N/A } finally {
1238N/A src.position(sp - src.arrayOffset());
1238N/A dst.position(dp - dst.arrayOffset());
1238N/A }
1238N/A }
1238N/A }
1238N/A}