src/java.desktop/solaris/classes/sun/font/X11CNS11643.java
author chegar
Thu, 08 Mar 2018 18:44:57 +0000
branchhttp-client-branch
changeset 56266 9aeab6fea2fd
parent 47216 71c04702a3d5
permissions -rw-r--r--
http-client-branch: add some WebSocketTests to the ProblemList
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2913
diff changeset
     2
 * Copyright (c) 2001, 2008, 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: 2913
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: 2913
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: 2913
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2913
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2913
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
30456
2a753e3fc714 8035302: Eliminate dependency on jdk.charsets from 2D font code.
prr
parents: 29119
diff changeset
    26
package sun.font;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.charset.*;
29119
f680ba340556 8073893: Enable charsets build system to configure euc_tw into java.base module/sun.nio.cs
sherman
parents: 25859
diff changeset
    31
import sun.nio.cs.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public abstract class X11CNS11643 extends Charset {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private final int plane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public X11CNS11643 (int plane, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        super(name, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        switch (plane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            this.plane = 0; // CS1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            this.plane = plane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                ("Only planes 1, 2, and 3 supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public CharsetEncoder newEncoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return new Encoder(this, plane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public CharsetDecoder newDecoder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return new Decoder(this, plane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public boolean contains(Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return cs instanceof X11CNS11643;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private class Encoder extends EUC_TW.Encoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        private int plane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        public Encoder(Charset cs, int plane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            super(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            this.plane = plane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    69
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    70
        private byte[] bb = new byte[4];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        public boolean canEncode(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (c <= 0x7F) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    75
            int nb = toEUC(c, bb);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    76
            if (nb == -1)
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    77
                return false;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    78
            int p = 0;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    79
            if (nb == 4)
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    80
                p = (bb[1] & 0xff) - 0xa0;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    81
            return (p == plane);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        public boolean isLegalReplacement(byte[] repl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            char[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            byte[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    char c = sa[sp];
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
    99
                    if ( c > '\u007f'&& c < '\uFFFE') {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   100
                        int nb = toEUC(c, bb);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   101
                        if (nb != -1) {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   102
                            int p = 0;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   103
                            if (nb == 4)
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   104
                                p = (bb[1] & 0xff) - 0xa0;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   105
                            if (p == plane) {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   106
                                if (dl - dp < 2)
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   107
                                    return CoderResult.OVERFLOW;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   108
                                if (nb == 2) {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   109
                                    da[dp++] = (byte)(bb[0] & 0x7f);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   110
                                    da[dp++] = (byte)(bb[1] & 0x7f);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   111
                                } else {
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   112
                                    da[dp++] = (byte)(bb[2] & 0x7f);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   113
                                    da[dp++] = (byte)(bb[3] & 0x7f);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   114
                                }
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   115
                                sp++;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   116
                                continue;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   117
                            }
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   118
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    return CoderResult.unmappableForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private class Decoder extends EUC_TW.Decoder {
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   131
        int plane;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        private String table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        protected Decoder(Charset cs, int plane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            super(cs);
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   135
            if (plane == 0)
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   136
                this.plane = plane;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   137
            else if (plane == 2 || plane == 3)
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   138
                this.plane = plane - 1;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   139
            else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    ("Only planes 1, 2, and 3 supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        //we only work on array backed buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            byte[] sa = src.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            int sp = src.arrayOffset() + src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            int sl = src.arrayOffset() + src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            char[] da = dst.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            int dp = dst.arrayOffset() + dst.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            int dl = dst.arrayOffset() + dst.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                while (sp < sl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    if ( sl - sp < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    }
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   159
                    int b1 = (sa[sp] & 0xff) | 0x80;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   160
                    int b2 = (sa[sp + 1] & 0xff) | 0x80;
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   161
                    char[] cc = toUnicode(b1, b2, plane);
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   162
                    // plane3 has non-bmp characters(added), x11cnsp3
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   163
                    // however does not support them
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   164
                    if (cc == null || cc.length == 2)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        return CoderResult.unmappableForLength(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    if (dl - dp < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        return CoderResult.OVERFLOW;
2913
39a9cc073b84 6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents: 2
diff changeset
   168
                    da[dp++] = cc[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    sp +=2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                src.position(sp - src.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                dst.position(dp - dst.arrayOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}