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