J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. |
| 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | * |
| 6 | * This code is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 only, as |
| 8 | * published by the Free Software Foundation. Sun designates this |
| 9 | * particular file as subject to the "Classpath" exception as provided |
| 10 | * by Sun in the LICENSE file that accompanied this code. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | * version 2 for more details (a copy is included in the LICENSE file that |
| 16 | * accompanied this code). |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License version |
| 19 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 23 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 24 | * have any questions. |
| 25 | */ |
| 26 | |
| 27 | /* |
| 28 | */ |
| 29 | |
| 30 | package sun.nio.cs.ext; |
| 31 | |
| 32 | import java.nio.ByteBuffer; |
| 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 | |
| 38 | // EBCDIC DBCS-only Korean |
| 39 | public class IBM834 |
| 40 | extends Charset |
| 41 | { |
| 42 | public IBM834() { |
| 43 | super("x-IBM834", ExtendedCharsets.aliasesFor("x-IBM834")); |
| 44 | } |
| 45 | |
| 46 | public boolean contains(Charset cs) { |
| 47 | return (cs instanceof IBM834); |
| 48 | } |
| 49 | |
| 50 | public CharsetDecoder newDecoder() { |
| 51 | return new Decoder(this); |
| 52 | } |
| 53 | |
| 54 | public CharsetEncoder newEncoder() { |
| 55 | return new Encoder(this); |
| 56 | } |
| 57 | |
| 58 | protected static class Decoder extends DBCS_ONLY_IBM_EBCDIC_Decoder { |
| 59 | public Decoder(Charset cs) { |
| 60 | super(cs); |
| 61 | super.mask1 = 0xFFF0; |
| 62 | super.mask2 = 0x000F; |
| 63 | super.shift = 4; |
| 64 | super.index1 = IBM933.getDecoderIndex1(); |
| 65 | super.index2 = IBM933.getDecoderIndex2(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | protected static class Encoder extends IBM933.Encoder { |
| 70 | public Encoder(Charset cs) { |
| 71 | super(cs, new byte[] {(byte)0xfe, (byte)0xfe}, false); |
| 72 | } |
| 73 | |
| 74 | protected CoderResult implFlush(ByteBuffer out) { |
| 75 | implReset(); |
| 76 | return CoderResult.UNDERFLOW; |
| 77 | } |
| 78 | |
| 79 | protected byte[] encodeHangul(char ch) { |
| 80 | byte[] bytes = super.encodeHangul(ch); |
| 81 | if (bytes.length == 0) { |
| 82 | // Cp834 has 6 additional non-roundtrip char->bytes |
| 83 | // mappings, see#6379808 |
| 84 | if (ch == '\u00b7') { |
| 85 | return new byte[] {(byte)0x41, (byte)0x43 }; |
| 86 | } else if (ch == '\u00ad') { |
| 87 | return new byte[] {(byte)0x41, (byte)0x48 }; |
| 88 | } else if (ch == '\u2015') { |
| 89 | return new byte[] {(byte)0x41, (byte)0x49 }; |
| 90 | } else if (ch == '\u223c') { |
| 91 | return new byte[] {(byte)0x42, (byte)0xa1 }; |
| 92 | } else if (ch == '\uff5e') { |
| 93 | return new byte[] {(byte)0x49, (byte)0x54 }; |
| 94 | } else if (ch == '\u2299') { |
| 95 | return new byte[] {(byte)0x49, (byte)0x6f }; |
| 96 | } |
| 97 | } else if (bytes[0] == 0) { |
| 98 | return EMPTYBA; |
| 99 | } |
| 100 | return bytes; |
| 101 | } |
| 102 | |
| 103 | public boolean canEncode(char ch) { |
| 104 | return encodeHangul(ch).length != 0; |
| 105 | } |
| 106 | |
| 107 | public boolean isLegalReplacement(byte[] repl) { |
| 108 | if (repl.length == 2 && |
| 109 | repl[0] == (byte)0xfe && repl[1] == (byte)0xfe) |
| 110 | return true; |
| 111 | return super.isLegalReplacement(repl); |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | } |