blob: 916c0fb79981e70c2afaee4a49ed789e7ae19ae1 [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_CN;
32
33public class X11GB2312 extends Charset {
34 public X11GB2312 () {
35 super("X11GB2312", 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 X11GB2312;
46 }
47
48 private class Encoder extends EUC_CN.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
65 byte[] da = dst.array();
66 int dp = dst.arrayOffset() + dst.position();
67 int dl = dst.arrayOffset() + dst.limit();
68
69 try {
70 while (sp < sl) {
71 char c = sa[sp];
72 if (c <= '\u007f')
73 return CoderResult.unmappableForLength(1);
74 int ncode = encodeDouble(c);
75 if (ncode != 0 && c != '\u0000' ) {
76 da[dp++] = (byte) ((ncode >> 8) & 0x7f);
77 da[dp++] = (byte) (ncode & 0x7f);
78 sp++;
79 continue;
80 }
81 return CoderResult.unmappableForLength(1);
82 }
83 return CoderResult.UNDERFLOW;
84 } finally {
85 src.position(sp - src.arrayOffset());
86 dst.position(dp - dst.arrayOffset());
87 }
88 }
89 public boolean isLegalReplacement(byte[] repl) {
90 return true;
91 }
92 }
93
94 private class Decoder extends EUC_CN.Decoder {
95 public Decoder(Charset cs) {
96 super(cs);
97 }
98
99 protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
100 byte[] sa = src.array();
101 int sp = src.arrayOffset() + src.position();
102 int sl = src.arrayOffset() + src.limit();
103 assert (sp <= sl);
104 sp = (sp <= sl ? sp : sl);
105 char[] da = dst.array();
106 int dp = dst.arrayOffset() + dst.position();
107 int dl = dst.arrayOffset() + dst.limit();
108 assert (dp <= dl);
109 dp = (dp <= dl ? dp : dl);
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
136}