jdk/src/share/classes/sun/nio/cs/ext/PCK.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5785 5dfabe612d10
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5785
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
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
package sun.nio.cs.ext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.charset.CharsetDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.CoderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.nio.cs.HistoricallyNamedCharset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class PCK
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    extends Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    implements HistoricallyNamedCharset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public PCK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        super("x-PCK", ExtendedCharsets.aliasesFor("x-PCK"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public String historicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return "PCK";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public boolean contains(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return ((cs.name().equals("US-ASCII"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                || (cs instanceof JIS_X_0201)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                || (cs instanceof PCK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public CharsetDecoder newDecoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return new Decoder(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public CharsetEncoder newEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // Need to force the replacement byte to 0x3f
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // because JIS_X_0208_Encoder defines its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // alternative 2 byte substitution to permit it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // to exist as a self-standing Encoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        byte[] replacementBytes = { (byte)0x3f };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return new Encoder(this).replaceWith(replacementBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static class Decoder extends SJIS.Decoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        JIS_X_0208_Solaris_Decoder jis0208;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        private static final char REPLACE_CHAR='\uFFFD';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        private Decoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            super(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            jis0208 = new JIS_X_0208_Solaris_Decoder(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        protected char decodeDouble(int c1, int c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            char outChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if ((outChar = super.decodeDouble(c1, c2)) != '\uFFFD')  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                // Map JIS X 0208:1983 0x213D <--> U+2015
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                return ((outChar != '\u2014')? outChar: '\u2015');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                int adjust = c2 < 0x9F ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                int rowOffset = c1 < 0xA0 ? 0x70 : 0xB0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                int cellOffset = (adjust == 1) ? (c2 > 0x7F ? 0x20 : 0x1F) : 0x7E;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                int b1 = ((c1 - rowOffset) << 1) - adjust;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                int b2 = c2 - cellOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                char outChar2 = jis0208.decodeDouble(b1, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                return outChar2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static class Encoder extends SJIS.Encoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        private JIS_X_0201.Encoder jis0201;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
5785
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   105
        private static final short[] j0208Index1 =
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   106
            JIS_X_0208_Solaris_Encoder.getIndex1();
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   107
        private static final String[] j0208Index2 =
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   108
            JIS_X_0208_Solaris_Encoder.getIndex2();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        private Encoder(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            super(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            jis0201 = new JIS_X_0201.Encoder(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        protected int encodeDouble(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // PCK uses JIS_X_0208:1983 rather than JIS_X_0208:1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                case '\u2015':
5785
5dfabe612d10 6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents: 5506
diff changeset
   122
                    return 0x815C;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                case '\u2014':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if ((result = super.encodeDouble(ch)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                int offset = j0208Index1[ch >> 8] << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                int pos = j0208Index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                if (pos != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                int c1 = (pos >> 8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                int c2 = pos & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                int rowOffset = c1 < 0x5F ? 0x70 : 0xB0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                int cellOffset = (c1 % 2 == 1) ? (c2 > 0x5F ? 0x20 : 0x1F) : 0x7E;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                result = ((((c1 + 1 ) >> 1) + rowOffset) << 8) | (c2 + cellOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}