Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/awt/motif/X11CNS11643.java
32288 views
/*1* Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt.motif;2627import java.nio.CharBuffer;28import java.nio.ByteBuffer;29import java.nio.charset.*;30import sun.nio.cs.ext.EUC_TW;3132public abstract class X11CNS11643 extends Charset {33private final int plane;34public X11CNS11643 (int plane, String name) {35super(name, null);36switch (plane) {37case 1:38this.plane = 0; // CS139break;40case 2:41case 3:42this.plane = plane;43break;44default:45throw new IllegalArgumentException46("Only planes 1, 2, and 3 supported");47}48}4950public CharsetEncoder newEncoder() {51return new Encoder(this, plane);52}5354public CharsetDecoder newDecoder() {55return new Decoder(this, plane);56}5758public boolean contains(Charset cs) {59return cs instanceof X11CNS11643;60}6162private class Encoder extends EUC_TW.Encoder {63private int plane;64public Encoder(Charset cs, int plane) {65super(cs);66this.plane = plane;67}6869private byte[] bb = new byte[4];70public boolean canEncode(char c) {71if (c <= 0x7F) {72return false;73}74int nb = toEUC(c, bb);75if (nb == -1)76return false;77int p = 0;78if (nb == 4)79p = (bb[1] & 0xff) - 0xa0;80return (p == plane);81}8283public boolean isLegalReplacement(byte[] repl) {84return true;85}8687protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {88char[] sa = src.array();89int sp = src.arrayOffset() + src.position();90int sl = src.arrayOffset() + src.limit();91byte[] da = dst.array();92int dp = dst.arrayOffset() + dst.position();93int dl = dst.arrayOffset() + dst.limit();9495try {96while (sp < sl) {97char c = sa[sp];98if ( c > '\u007f'&& c < '\uFFFE') {99int nb = toEUC(c, bb);100if (nb != -1) {101int p = 0;102if (nb == 4)103p = (bb[1] & 0xff) - 0xa0;104if (p == plane) {105if (dl - dp < 2)106return CoderResult.OVERFLOW;107if (nb == 2) {108da[dp++] = (byte)(bb[0] & 0x7f);109da[dp++] = (byte)(bb[1] & 0x7f);110} else {111da[dp++] = (byte)(bb[2] & 0x7f);112da[dp++] = (byte)(bb[3] & 0x7f);113}114sp++;115continue;116}117}118}119return CoderResult.unmappableForLength(1);120}121return CoderResult.UNDERFLOW;122} finally {123src.position(sp - src.arrayOffset());124dst.position(dp - dst.arrayOffset());125}126}127}128129private class Decoder extends EUC_TW.Decoder {130int plane;131private String table;132protected Decoder(Charset cs, int plane) {133super(cs);134if (plane == 0)135this.plane = plane;136else if (plane == 2 || plane == 3)137this.plane = plane - 1;138else139throw new IllegalArgumentException140("Only planes 1, 2, and 3 supported");141}142143//we only work on array backed buffer.144protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {145byte[] sa = src.array();146int sp = src.arrayOffset() + src.position();147int sl = src.arrayOffset() + src.limit();148149char[] da = dst.array();150int dp = dst.arrayOffset() + dst.position();151int dl = dst.arrayOffset() + dst.limit();152153try {154while (sp < sl) {155if ( sl - sp < 2) {156return CoderResult.UNDERFLOW;157}158int b1 = (sa[sp] & 0xff) | 0x80;159int b2 = (sa[sp + 1] & 0xff) | 0x80;160char[] cc = toUnicode(b1, b2, plane);161// plane3 has non-bmp characters(added), x11cnsp3162// however does not support them163if (cc == null || cc.length == 2)164return CoderResult.unmappableForLength(2);165if (dl - dp < 1)166return CoderResult.OVERFLOW;167da[dp++] = cc[0];168sp +=2;169}170return CoderResult.UNDERFLOW;171} finally {172src.position(sp - src.arrayOffset());173dst.position(dp - dst.arrayOffset());174}175}176}177}178179180