blob: a473ceabdda0d09c1d655be81450e73fefabb3a6 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2006 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
26/*
27 */
28
29/**
30 * Simple EUC-like decoder used by IBM01383 and IBM970
31 * supports G1 - no support for G2 or G3
32 */
33
34package sun.nio.cs.ext;
35
36import java.nio.ByteBuffer;
37import java.nio.CharBuffer;
38import java.nio.charset.Charset;
39import java.nio.charset.CharsetDecoder;
40import java.nio.charset.CoderResult;
41
42abstract class SimpleEUCDecoder
43 extends CharsetDecoder
44{
45 private final int SS2 = 0x8E;
46 private final int SS3 = 0x8F;
47
48 protected static String mappingTableG1;
49 protected static String byteToCharTable;
50
51 protected SimpleEUCDecoder(Charset cs) {
52 super(cs, 0.5f, 1.0f);
53 }
54
55 private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
56 byte[] sa = src.array();
57 int sp = src.arrayOffset() + src.position();
58 int sl = src.arrayOffset() + src.limit();
59 assert (sp <= sl);
60 sp = (sp <= sl ? sp : sl);
61 char[] da = dst.array();
62 int dp = dst.arrayOffset() + dst.position();
63 int dl = dst.arrayOffset() + dst.limit();
64 assert (dp <= dl);
65 dp = (dp <= dl ? dp : dl);
66
67 try {
68 while (sp < sl) {
69 int byte1, byte2;
70 int inputSize = 1;
71 char outputChar = '\uFFFD';
72
73 byte1 = sa[sp] & 0xff;
74
75 if ( byte1 <= 0x9f ) { // < 0x9f has its own table (G0)
76 if (byte1 == SS2 || byte1 == SS3 ) {
77 // No support provided for G2/G3 at this time.
78 return CoderResult.malformedForLength(1);
79 }
80 outputChar = byteToCharTable.charAt(byte1);
81 } else if (byte1 < 0xa1 || byte1 > 0xfe) { // invalid range?
82 return CoderResult.malformedForLength(1);
83 } else { // (G1)
84 if (sl - sp < 2) {
85 return CoderResult.UNDERFLOW;
86 }
87 byte2 = sa[sp + 1] & 0xff;
88 inputSize++;
89 if ( byte2 < 0xa1 || byte2 > 0xfe) {
90 return CoderResult.malformedForLength(2);
91 }
92 outputChar = mappingTableG1.charAt(((byte1 - 0xa1) * 94) + byte2 - 0xa1);
93 }
94 if (outputChar == '\uFFFD') {
95 return CoderResult.unmappableForLength(inputSize);
96 }
97 if (dl - dp < 1)
98 return CoderResult.OVERFLOW;
99 da[dp++] = outputChar;
100 sp += inputSize;
101 }
102 return CoderResult.UNDERFLOW;
103 } finally {
104 src.position(sp - src.arrayOffset());
105 dst.position(dp - dst.arrayOffset());
106 }
107 }
108
109 private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
110 int mark = src.position();
111
112 try {
113 while (src.hasRemaining()) {
114 char outputChar = '\uFFFD';
115 int inputSize = 1;
116 int byte1, byte2;
117
118 byte1 = src.get() & 0xff;
119 if ( byte1 <= 0x9f ) {
120 if (byte1 == SS2 || byte1 == SS3 ) {
121 return CoderResult.malformedForLength(1);
122 }
123 outputChar = byteToCharTable.charAt(byte1);
124 } else if (byte1 < 0xa1 || byte1 > 0xfe) {
125 return CoderResult.malformedForLength(1);
126 } else {
127 if (!src.hasRemaining()) {
128 return CoderResult.UNDERFLOW;
129 }
130 byte2 = src.get() & 0xff;
131 inputSize++;
132 if ( byte2 < 0xa1 || byte2 > 0xfe) {
133 return CoderResult.malformedForLength(2);
134 }
135 outputChar = mappingTableG1.charAt(((byte1 - 0xa1) * 94) + byte2 - 0xa1);
136 }
137 if (outputChar == '\uFFFD') {
138 return CoderResult.unmappableForLength(inputSize);
139 }
140 if (!dst.hasRemaining())
141 return CoderResult.OVERFLOW;
142 dst.put(outputChar);
143 mark += inputSize;
144 }
145 return CoderResult.UNDERFLOW;
146 } finally {
147 src.position(mark);
148 }
149 }
150
151 protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
152 if (src.hasArray() && dst.hasArray())
153 return decodeArrayLoop(src, dst);
154 else
155 return decodeBufferLoop(src, dst);
156 }
157}