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_KR;
032:
033: public class X11KSC5601 extends Charset {
034: public X11KSC5601() {
035: super ("X11KSC5601", 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 X11KSC5601;
048: }
049:
050: private class Encoder extends EUC_KR.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: byte[] da = dst.array();
067: int dp = dst.arrayOffset() + dst.position();
068: int dl = dst.arrayOffset() + dst.limit();
069:
070: try {
071: while (sp < sl) {
072: char c = sa[sp];
073: if (c <= '\u007f')
074: return CoderResult.unmappableForLength(1);
075: int ncode = encodeDouble(c);
076: if (ncode != 0 && c != '\u0000') {
077: da[dp++] = (byte) ((ncode >> 8) & 0x7f);
078: da[dp++] = (byte) (ncode & 0x7f);
079: sp++;
080: continue;
081: }
082: return CoderResult.unmappableForLength(1);
083: }
084: return CoderResult.UNDERFLOW;
085: } finally {
086: src.position(sp - src.arrayOffset());
087: dst.position(dp - dst.arrayOffset());
088: }
089: }
090:
091: public boolean isLegalReplacement(byte[] repl) {
092: return true;
093: }
094: }
095:
096: private class Decoder extends EUC_KR.Decoder {
097: public Decoder(Charset cs) {
098: super (cs);
099: }
100:
101: protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
102: byte[] sa = src.array();
103: int sp = src.arrayOffset() + src.position();
104: int sl = src.arrayOffset() + src.limit();
105: assert (sp <= sl);
106: sp = (sp <= sl ? sp : sl);
107: char[] da = dst.array();
108: int dp = dst.arrayOffset() + dst.position();
109: int dl = dst.arrayOffset() + dst.limit();
110: assert (dp <= dl);
111: dp = (dp <= dl ? dp : dl);
112:
113: try {
114: while (sp < sl) {
115: if (sl - sp < 2) {
116: return CoderResult.UNDERFLOW;
117: }
118: int b1 = sa[sp] & 0xFF | 0x80;
119: int b2 = sa[sp + 1] & 0xFF | 0x80;
120: char c = decodeDouble(b1, b2);
121: if (c == replacement().charAt(0)) {
122: return CoderResult.unmappableForLength(2);
123: }
124: if (dl - dp < 1)
125: return CoderResult.OVERFLOW;
126: da[dp++] = c;
127: sp += 2;
128: }
129: return CoderResult.UNDERFLOW;
130: } finally {
131: src.position(sp - src.arrayOffset());
132: dst.position(dp - dst.arrayOffset());
133: }
134:
135: }
136: }
137: }
|