blob: d713331c2a65d0a44e265bf23d70b2d284084fbb [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.awt.motif;
27
28import java.nio.CharBuffer;
29import java.nio.ByteBuffer;
30import java.nio.charset.*;
31import sun.nio.cs.ext.EUC_KR;
32
33public class X11KSC5601 extends Charset {
34 public X11KSC5601 () {
35 super("X11KSC5601", null);
36 }
37 public CharsetEncoder newEncoder() {
38 return new Encoder(this);
39 }
40 public CharsetDecoder newDecoder() {
41 return new Decoder(this);
42 }
43
44 public boolean contains(Charset cs) {
45 return cs instanceof X11KSC5601;
46 }
47
48 private class Encoder extends EUC_KR.Encoder {
49 public Encoder(Charset cs) {
50 super(cs);
51 }
52
53 public boolean canEncode(char c) {
54 if (c <= 0x7F) {
55 return false;
56 }
57 return super.canEncode(c);
58 }
59
60 protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
61 char[] sa = src.array();
62 int sp = src.arrayOffset() + src.position();
63 int sl = src.arrayOffset() + src.limit();
64 byte[] da = dst.array();
65 int dp = dst.arrayOffset() + dst.position();
66 int dl = dst.arrayOffset() + dst.limit();
67
68 try {
69 while (sp < sl) {
70 char c = sa[sp];
71 if (c <= '\u007f')
72 return CoderResult.unmappableForLength(1);
73 int ncode = encodeDouble(c);
74 if (ncode != 0 && c != '\u0000' ) {
75 da[dp++] = (byte) ((ncode >> 8) & 0x7f);
76 da[dp++] = (byte) (ncode & 0x7f);
77 sp++;
78 continue;
79 }
80 return CoderResult.unmappableForLength(1);
81 }
82 return CoderResult.UNDERFLOW;
83 } finally {
84 src.position(sp - src.arrayOffset());
85 dst.position(dp - dst.arrayOffset());
86 }
87 }
88 public boolean isLegalReplacement(byte[] repl) {
89 return true;
90 }
91 }
92
93 private class Decoder extends EUC_KR.Decoder {
94 public Decoder(Charset cs) {
95 super(cs);
96 }
97
98 protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
99 byte[] sa = src.array();
100 int sp = src.arrayOffset() + src.position();
101 int sl = src.arrayOffset() + src.limit();
102 assert (sp <= sl);
103 sp = (sp <= sl ? sp : sl);
104 char[] da = dst.array();
105 int dp = dst.arrayOffset() + dst.position();
106 int dl = dst.arrayOffset() + dst.limit();
107 assert (dp <= dl);
108 dp = (dp <= dl ? dp : dl);
109
110
111 try {
112 while (sp < sl) {
113 if ( sl - sp < 2) {
114 return CoderResult.UNDERFLOW;
115 }
116 int b1 = sa[sp] & 0xFF | 0x80;
117 int b2 = sa[sp + 1] & 0xFF | 0x80;
118 char c = decodeDouble(b1, b2);
119 if (c == replacement().charAt(0)) {
120 return CoderResult.unmappableForLength(2);
121 }
122 if (dl - dp < 1)
123 return CoderResult.OVERFLOW;
124 da[dp++] = c;
125 sp +=2;
126 }
127 return CoderResult.UNDERFLOW;
128 } finally {
129 src.position(sp - src.arrayOffset());
130 dst.position(dp - dst.arrayOffset());
131 }
132
133 }
134 }
135}