/* * Copyright (c) 1999, 2005, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.awt.motif; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.*; import static sun.awt.motif.DoubleByte.*; public class X11GBK extends Charset { private static Charset gbk = Charset.forName("GBK"); public X11GBK () { super("X11GBK", null); } public CharsetEncoder newEncoder() { return new Encoder(this); } public CharsetDecoder newDecoder() { return gbk.newDecoder(); } public boolean contains(Charset cs) { return cs instanceof X11GBK; } static class Encoder extends CharsetEncoder { static final int MAX_SINGLEBYTE = 0xff; static private CharsetEncoder enc = gbk.newEncoder(); Encoder(Charset cs) { super(cs, enc.averageBytesPerChar(), enc.maxBytesPerChar(), enc.replacement()); enc.onMalformedInput(CodingErrorAction.REPORT); enc.onUnmappableCharacter(CodingErrorAction.REPORT); } public boolean canEncode(char ch){ if (ch < 0x80) return false; return enc.canEncode(ch); } public int encodeChar(char ch) { if (ch < 0x80) { return UNMAPPABLE_ENCODING; } char[] buf = new char[1]; buf[0] = ch; ByteBuffer bb = null; CharBuffer cb = CharBuffer.wrap(buf); boolean mapped = false; try { bb = enc.encode(cb); mapped = true; } catch (CharacterCodingException cce) { } if (mapped && bb != null) { int ret = UNMAPPABLE_ENCODING; if (bb.remaining() == 1) { ret = bb.get() & 0xff; } else if (bb.remaining() > 1) { ret = ((bb.get() & 0xff) << 8) | (bb.get() & 0xff); } return ret; } else { return UNMAPPABLE_ENCODING; } } private SurrogateParser sgp; protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { int mark = src.position(); try { while (src.hasRemaining()) { char c = src.get(); int bb = encodeChar(c); if (bb == UNMAPPABLE_ENCODING) { if (Character.isSurrogate(c)) { if (sgp == null) { sgp = new SurrogateParser(); } if (sgp.parse(c, src) < 0) return sgp.error(); return sgp.unmappableResult(); } return CoderResult.unmappableForLength(1); } if (bb > MAX_SINGLEBYTE) { // DoubleByte if (dst.remaining() < 2) return CoderResult.OVERFLOW; dst.put((byte)(bb >> 8)); dst.put((byte)(bb)); } else { if (dst.remaining() < 1) return CoderResult.OVERFLOW; dst.put((byte)bb); } mark++; } return CoderResult.UNDERFLOW; } finally { src.position(mark); } } } }