jdk/src/share/classes/sun/nio/cs/ext/IBM834.java
author sherman
Thu, 21 May 2009 23:32:46 -0700
changeset 2921 d9d491a5a169
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6843578: Re-implement IBM doublebyte charsets 6639450: IBM949C encoder modifies state of IBM949 encoder 6569191: Cp943 io converter returns U+0000 and U+FFFD for unconvertable character 6577466: Character encoder IBM970 throws a BufferOverflowException 5065777: CharsetEncoder canEncode() methods often incorrectly return false Summary: Re-write 11 IBM doublebyte charsets. Thanks Ulf.Zibis for the codereview! Reviewed-by: martin
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;
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    37
import static sun.nio.cs.CharsetMapping.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
// EBCDIC DBCS-only Korean
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    40
public class IBM834 extends Charset
2
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() {
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    51
        IBM933.initb2c();
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    52
        return new DoubleByte.Decoder_EBCDIC_DBCSONLY(
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    53
            this, IBM933.b2c, 0x40, 0xfe);  // hardcode the b2min/max
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public CharsetEncoder newEncoder() {
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    57
        IBM933.initc2b();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return new Encoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    61
    protected static class Encoder extends DoubleByte.Encoder_EBCDIC_DBCSONLY {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public Encoder(Charset cs) {
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    63
            super(cs, new byte[] {(byte)0xfe, (byte)0xfe},
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    64
                  IBM933.c2b, IBM933.c2bIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    67
        public int encodeChar(char ch) {
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    68
            int bb = super.encodeChar(ch);
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    69
            if (bb == UNMAPPABLE_ENCODING) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                // Cp834 has 6 additional non-roundtrip char->bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                // mappings, see#6379808
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                if (ch == '\u00b7') {
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    73
                    return 0x4143;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                } else if (ch == '\u00ad') {
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    75
                    return 0x4148;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                } else if (ch == '\u2015') {
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    77
                    return 0x4149;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                } else if (ch == '\u223c') {
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    79
                    return 0x42a1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                } else if (ch == '\uff5e') {
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    81
                    return 0x4954;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                } else if (ch == '\u2299') {
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    83
                    return 0x496f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
2921
d9d491a5a169 6843578: Re-implement IBM doublebyte charsets
sherman
parents: 2
diff changeset
    86
            return bb;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public boolean isLegalReplacement(byte[] repl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (repl.length == 2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                repl[0] == (byte)0xfe && repl[1] == (byte)0xfe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return super.isLegalReplacement(repl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
}