Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/awt/motif/X11GB2312.java
32288 views
/*1* Copyright (c) 1996, 2005, 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.*;31import static sun.nio.cs.CharsetMapping.*;3233public class X11GB2312 extends Charset {34public X11GB2312 () {35super("X11GB2312", null);36}37public CharsetEncoder newEncoder() {38return new Encoder(this);39}40public CharsetDecoder newDecoder() {41return new Decoder(this);42}4344public boolean contains(Charset cs) {45return cs instanceof X11GB2312;46}4748private class Encoder extends CharsetEncoder {49private DoubleByte.Encoder enc = (DoubleByte.Encoder)new EUC_CN().newEncoder();5051public Encoder(Charset cs) {52super(cs, 2.0f, 2.0f);53}5455public boolean canEncode(char c) {56if (c <= 0x7F) {57return false;58}59return enc.canEncode(c);60}6162protected int encodeDouble(char c) {63return enc.encodeChar(c);64}6566protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {67char[] sa = src.array();68int sp = src.arrayOffset() + src.position();69int sl = src.arrayOffset() + src.limit();7071byte[] da = dst.array();72int dp = dst.arrayOffset() + dst.position();73int dl = dst.arrayOffset() + dst.limit();7475try {76while (sp < sl) {77char c = sa[sp];78if (c <= '\u007f')79return CoderResult.unmappableForLength(1);80int ncode = encodeDouble(c);81if (ncode != 0 && c != '\u0000' ) {82da[dp++] = (byte) ((ncode >> 8) & 0x7f);83da[dp++] = (byte) (ncode & 0x7f);84sp++;85continue;86}87return CoderResult.unmappableForLength(1);88}89return CoderResult.UNDERFLOW;90} finally {91src.position(sp - src.arrayOffset());92dst.position(dp - dst.arrayOffset());93}94}95public boolean isLegalReplacement(byte[] repl) {96return true;97}98}99100private class Decoder extends CharsetDecoder {101private DoubleByte.Decoder dec = (DoubleByte.Decoder)new EUC_CN().newDecoder();102103public Decoder(Charset cs) {104super(cs, 0.5f, 1.0f);105}106107protected char decodeDouble(int b1, int b2) {108return dec.decodeDouble(b1, b2);109}110111protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {112byte[] sa = src.array();113int sp = src.arrayOffset() + src.position();114int sl = src.arrayOffset() + src.limit();115assert (sp <= sl);116sp = (sp <= sl ? sp : sl);117char[] da = dst.array();118int dp = dst.arrayOffset() + dst.position();119int dl = dst.arrayOffset() + dst.limit();120assert (dp <= dl);121dp = (dp <= dl ? dp : dl);122123try {124while (sp < sl) {125if ( sl - sp < 2) {126return CoderResult.UNDERFLOW;127}128int b1 = sa[sp] & 0xFF | 0x80;129int b2 = sa[sp + 1] & 0xFF | 0x80;130char c = decodeDouble(b1, b2);131if (c == UNMAPPABLE_DECODING) {132return CoderResult.unmappableForLength(2);133}134if (dl - dp < 1)135return CoderResult.OVERFLOW;136da[dp++] = c;137sp +=2;138}139return CoderResult.UNDERFLOW;140} finally {141src.position(sp - src.arrayOffset());142dst.position(dp - dst.arrayOffset());143}144145}146}147148}149150151