J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2004 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 | package sun.nio.cs.ext; |
| 30 | |
| 31 | import java.nio.ByteBuffer; |
| 32 | import java.nio.CharBuffer; |
| 33 | import java.nio.charset.Charset; |
| 34 | import java.nio.charset.CharsetDecoder; |
| 35 | import java.nio.charset.CharsetEncoder; |
| 36 | import java.nio.charset.CoderResult; |
| 37 | import sun.nio.cs.HistoricallyNamedCharset; |
| 38 | import sun.nio.cs.Surrogate; |
| 39 | |
| 40 | public class EUC_JP_Open |
| 41 | extends Charset |
| 42 | implements HistoricallyNamedCharset |
| 43 | { |
| 44 | public EUC_JP_Open() { |
| 45 | super("x-eucJP-Open", ExtendedCharsets.aliasesFor("x-eucJP-Open")); |
| 46 | } |
| 47 | |
| 48 | public String historicalName() { |
| 49 | return "EUC_JP_Solaris"; |
| 50 | } |
| 51 | |
| 52 | public boolean contains(Charset cs) { |
| 53 | return ((cs.name().equals("US-ASCII")) |
| 54 | || (cs instanceof JIS_X_0201) |
| 55 | || (cs instanceof EUC_JP)); |
| 56 | } |
| 57 | |
| 58 | public CharsetDecoder newDecoder() { |
| 59 | return new Decoder(this); |
| 60 | } |
| 61 | |
| 62 | public CharsetEncoder newEncoder() { |
| 63 | |
| 64 | // Need to force the replacement byte to 0x3f |
| 65 | // because JIS_X_0208_Encoder defines its own |
| 66 | // alternative 2 byte substitution to permit it |
| 67 | // to exist as a self-standing Encoder |
| 68 | |
| 69 | byte[] replacementBytes = { (byte)0x3f }; |
| 70 | return new Encoder(this).replaceWith(replacementBytes); |
| 71 | } |
| 72 | |
| 73 | private static class Decoder extends EUC_JP.Decoder { |
| 74 | JIS_X_0201.Decoder decoderJ0201; |
| 75 | JIS_X_0212_Solaris_Decoder decodeMappingJ0212; |
| 76 | JIS_X_0208_Solaris_Decoder decodeMappingJ0208; |
| 77 | |
| 78 | short[] j0208Index1; |
| 79 | String[] j0208Index2; |
| 80 | |
| 81 | protected final char REPLACE_CHAR='\uFFFD'; |
| 82 | |
| 83 | private Decoder(Charset cs) { |
| 84 | super(cs); |
| 85 | decoderJ0201 = new JIS_X_0201.Decoder(cs); |
| 86 | decodeMappingJ0212 = new JIS_X_0212_Solaris_Decoder(cs); |
| 87 | decodeMappingJ0208 = new JIS_X_0208_Solaris_Decoder(cs); |
| 88 | decodeMappingJ0208.start = 0xa1; |
| 89 | decodeMappingJ0208.end = 0xfe; |
| 90 | j0208Index1 = decodeMappingJ0208.getIndex1(); |
| 91 | j0208Index2 = decodeMappingJ0208.getIndex2(); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | protected char decode0212(int byte1, int byte2) { |
| 96 | return decodeMappingJ0212.decodeDouble(byte1, byte2); |
| 97 | |
| 98 | } |
| 99 | |
| 100 | protected char decodeDouble(int byte1, int byte2) { |
| 101 | if (byte1 == 0x8e) { |
| 102 | return decoderJ0201.decode(byte2 - 256); |
| 103 | } |
| 104 | |
| 105 | if (((byte1 < 0) |
| 106 | || (byte1 > decodeMappingJ0208.getIndex1().length)) |
| 107 | || ((byte2 < decodeMappingJ0208.start) |
| 108 | || (byte2 > decodeMappingJ0208.end))) |
| 109 | return REPLACE_CHAR; |
| 110 | |
| 111 | char result = super.decodeDouble(byte1, byte2); |
| 112 | if (result != '\uFFFD') { |
| 113 | return result; |
| 114 | } else { |
| 115 | int n = (j0208Index1[byte1 - 0x80] & 0xf) * |
| 116 | (decodeMappingJ0208.end - decodeMappingJ0208.start + 1) |
| 117 | + (byte2 - decodeMappingJ0208.start); |
| 118 | return j0208Index2[j0208Index1[byte1 - 0x80] >> 4].charAt(n); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | private static class Encoder extends EUC_JP.Encoder { |
| 125 | |
| 126 | JIS_X_0201.Encoder encoderJ0201; |
| 127 | JIS_X_0212_Solaris_Encoder encoderJ0212; |
| 128 | JIS_X_0208_Solaris_Encoder encoderJ0208; |
| 129 | |
| 130 | short[] j0208Index1; |
| 131 | String[] j0208Index2; |
| 132 | |
| 133 | private final Surrogate.Parser sgp = new Surrogate.Parser(); |
| 134 | |
| 135 | private Encoder(Charset cs) { |
| 136 | super(cs); |
| 137 | encoderJ0201 = new JIS_X_0201.Encoder(cs); |
| 138 | encoderJ0212 = new JIS_X_0212_Solaris_Encoder(cs); |
| 139 | encoderJ0208 = new JIS_X_0208_Solaris_Encoder(cs); |
| 140 | j0208Index1 = encoderJ0208.getIndex1(); |
| 141 | j0208Index2 = encoderJ0208.getIndex2(); |
| 142 | } |
| 143 | |
| 144 | protected int encodeSingle(char inputChar, byte[] outputByte) { |
| 145 | byte b; |
| 146 | |
| 147 | if (inputChar == 0) { |
| 148 | outputByte[0] = (byte)0; |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | if ((b = encoderJ0201.encode(inputChar)) == 0) |
| 153 | return 0; |
| 154 | |
| 155 | if (b > 0 && b < 128) { |
| 156 | outputByte[0] = b; |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | outputByte[0] = (byte)0x8e; |
| 161 | outputByte[1] = b; |
| 162 | return 2; |
| 163 | } |
| 164 | |
| 165 | protected int encodeDouble(char ch) { |
| 166 | int r = super.encodeDouble(ch); |
| 167 | if (r != 0) { |
| 168 | return r; |
| 169 | } |
| 170 | else { |
| 171 | int offset = j0208Index1[((ch & 0xff00) >> 8 )] << 8; |
| 172 | r = j0208Index2[offset >> 12].charAt((offset & 0xfff) + |
| 173 | (ch & 0xFF)); |
| 174 | if (r > 0x7500) |
| 175 | return 0x8F8080 + encoderJ0212.encodeDouble(ch); |
| 176 | } |
| 177 | return (r==0 ? 0: r + 0x8080); |
| 178 | } |
| 179 | } |
| 180 | } |