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