001: /*
002: * Copyright 1996-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_CN;
032:
033: public class X11GB2312 extends Charset {
034: public X11GB2312() {
035: super ("X11GB2312", null);
036: }
037:
038: public CharsetEncoder newEncoder() {
039: return new Encoder(this );
040: }
041:
042: public CharsetDecoder newDecoder() {
043: return new Decoder(this );
044: }
045:
046: public boolean contains(Charset cs) {
047: return cs instanceof X11GB2312;
048: }
049:
050: private class Encoder extends EUC_CN.Encoder {
051: public Encoder(Charset cs) {
052: super (cs);
053: }
054:
055: public boolean canEncode(char c) {
056: if (c <= 0x7F) {
057: return false;
058: }
059: return super .canEncode(c);
060: }
061:
062: protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
063: char[] sa = src.array();
064: int sp = src.arrayOffset() + src.position();
065: int sl = src.arrayOffset() + src.limit();
066:
067: byte[] da = dst.array();
068: int dp = dst.arrayOffset() + dst.position();
069: int dl = dst.arrayOffset() + dst.limit();
070:
071: try {
072: while (sp < sl) {
073: char c = sa[sp];
074: if (c <= '\u007f')
075: return CoderResult.unmappableForLength(1);
076: int ncode = encodeDouble(c);
077: if (ncode != 0 && c != '\u0000') {
078: da[dp++] = (byte) ((ncode >> 8) & 0x7f);
079: da[dp++] = (byte) (ncode & 0x7f);
080: sp++;
081: continue;
082: }
083: return CoderResult.unmappableForLength(1);
084: }
085: return CoderResult.UNDERFLOW;
086: } finally {
087: src.position(sp - src.arrayOffset());
088: dst.position(dp - dst.arrayOffset());
089: }
090: }
091:
092: public boolean isLegalReplacement(byte[] repl) {
093: return true;
094: }
095: }
096:
097: private class Decoder extends EUC_CN.Decoder {
098: public Decoder(Charset cs) {
099: super (cs);
100: }
101:
102: protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
103: byte[] sa = src.array();
104: int sp = src.arrayOffset() + src.position();
105: int sl = src.arrayOffset() + src.limit();
106: assert (sp <= sl);
107: sp = (sp <= sl ? sp : sl);
108: char[] da = dst.array();
109: int dp = dst.arrayOffset() + dst.position();
110: int dl = dst.arrayOffset() + dst.limit();
111: assert (dp <= dl);
112: dp = (dp <= dl ? dp : dl);
113:
114: try {
115: while (sp < sl) {
116: if (sl - sp < 2) {
117: return CoderResult.UNDERFLOW;
118: }
119: int b1 = sa[sp] & 0xFF | 0x80;
120: int b2 = sa[sp + 1] & 0xFF | 0x80;
121: char c = decodeDouble(b1, b2);
122: if (c == replacement().charAt(0)) {
123: return CoderResult.unmappableForLength(2);
124: }
125: if (dl - dp < 1)
126: return CoderResult.OVERFLOW;
127: da[dp++] = c;
128: sp += 2;
129: }
130: return CoderResult.UNDERFLOW;
131: } finally {
132: src.position(sp - src.arrayOffset());
133: dst.position(dp - dst.arrayOffset());
134: }
135:
136: }
137: }
138:
139: }
|