1 /* |
|
2 * Copyright (c) 1997, Oracle and/or its affiliates. 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 package sun.io; |
|
26 |
|
27 /** |
|
28 * @author Malcolm Ayres |
|
29 */ |
|
30 public abstract class ByteToCharEUC extends ByteToCharConverter |
|
31 { |
|
32 private final int G0 = 0; |
|
33 private final int G1 = 1; |
|
34 private final int SS2 = 0x8E; |
|
35 private final int SS3 = 0x8F; |
|
36 |
|
37 private int firstByte, state; |
|
38 |
|
39 protected String mappingTableG1; |
|
40 protected String byteToCharTable; |
|
41 |
|
42 |
|
43 public ByteToCharEUC() { |
|
44 super(); |
|
45 state = G0; |
|
46 } |
|
47 |
|
48 /** |
|
49 * flush out any residual data and reset the buffer state |
|
50 */ |
|
51 public int flush(char[] output, int outStart, int outEnd) |
|
52 throws MalformedInputException |
|
53 { |
|
54 if (state != G0) { |
|
55 reset(); |
|
56 badInputLength = 0; |
|
57 throw new MalformedInputException(); |
|
58 } |
|
59 |
|
60 reset(); |
|
61 return 0; |
|
62 } |
|
63 |
|
64 /** |
|
65 * Resets the converter. |
|
66 */ |
|
67 public void reset() { |
|
68 state = G0; |
|
69 charOff = byteOff = 0; |
|
70 } |
|
71 |
|
72 /** |
|
73 * Character conversion |
|
74 */ |
|
75 public int convert(byte[] input, int inOff, int inEnd, |
|
76 char[] output, int outOff, int outEnd) |
|
77 throws UnknownCharacterException, MalformedInputException, |
|
78 ConversionBufferFullException |
|
79 { |
|
80 |
|
81 int byte1; |
|
82 char outputChar = '\uFFFD'; |
|
83 |
|
84 byteOff = inOff; |
|
85 charOff = outOff; |
|
86 |
|
87 while (byteOff < inEnd) { |
|
88 |
|
89 byte1 = input[byteOff]; |
|
90 if (byte1 < 0) |
|
91 byte1 += 256; |
|
92 |
|
93 switch (state) { |
|
94 case G0: |
|
95 if (byte1 == SS2 || // no general support |
|
96 byte1 == SS3 ) { // for g2 or g3 |
|
97 badInputLength = 1; |
|
98 throw new MalformedInputException(); |
|
99 } |
|
100 |
|
101 if ( byte1 <= 0x9f ) // < 0x9f has its own table |
|
102 outputChar = byteToCharTable.charAt(byte1); |
|
103 else |
|
104 if (byte1 < 0xa1 || byte1 > 0xfe) { // byte within range? |
|
105 badInputLength = 1; |
|
106 throw new MalformedInputException(); |
|
107 } else { // G1 set first byte |
|
108 firstByte = byte1; |
|
109 state = G1; |
|
110 } |
|
111 break; |
|
112 |
|
113 case G1: |
|
114 |
|
115 state = G0; |
|
116 if ( byte1 < 0xa1 || byte1 > 0xfe) { // valid G1 set second byte |
|
117 badInputLength = 1; |
|
118 throw new MalformedInputException(); |
|
119 } |
|
120 |
|
121 outputChar = mappingTableG1.charAt(((firstByte - 0xa1) * 94) + byte1 - 0xa1); |
|
122 break; |
|
123 |
|
124 } |
|
125 |
|
126 if (state == G0) { |
|
127 if (outputChar == '\uFFFD') { |
|
128 if (subMode) |
|
129 outputChar = subChars[0]; |
|
130 else { |
|
131 badInputLength = 1; |
|
132 throw new UnknownCharacterException(); |
|
133 } |
|
134 } |
|
135 |
|
136 if (charOff >= outEnd) |
|
137 throw new ConversionBufferFullException(); |
|
138 |
|
139 output[charOff++] = outputChar; |
|
140 } |
|
141 |
|
142 byteOff++; |
|
143 |
|
144 } |
|
145 |
|
146 return charOff - outOff; |
|
147 |
|
148 } |
|
149 |
|
150 } |
|