1 /*
2  * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.awt;
27 
28 import java.nio.ByteBuffer;
29 import java.nio.CharBuffer;
30 import java.nio.charset.*;
31 
32 public class Symbol extends Charset {
Symbol()33     public Symbol () {
34         super("Symbol", null);
35     }
newEncoder()36     public CharsetEncoder newEncoder() {
37         return new Encoder(this);
38     }
39 
40     /* Seems like supporting a decoder is required, but we aren't going
41      * to be publically exposing this class, so no need to waste work
42      */
newDecoder()43     public CharsetDecoder newDecoder() {
44         throw new Error("Decoder is not implemented for Symbol Charset");
45     }
46 
contains(Charset cs)47     public boolean contains(Charset cs) {
48         return cs instanceof Symbol;
49     }
50 
51     private static class Encoder extends CharsetEncoder {
Encoder(Charset cs)52         public Encoder(Charset cs) {
53             super(cs, 1.0f, 1.0f);
54         }
55 
canEncode(char c)56         public boolean canEncode(char c) {
57             if (c >= 0x2200 && c <= 0x22ef) {
58                 if (table_math[c - 0x2200] != 0x00) {
59                     return true;
60                 }
61             } else if (c >= 0x0391 && c <= 0x03d6) {
62                 if (table_greek[c - 0x0391] != 0x00) {
63                     return true;
64                 }
65             }
66             return false;
67         }
68 
encodeLoop(CharBuffer src, ByteBuffer dst)69         protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
70             char[] sa = src.array();
71             int sp = src.arrayOffset() + src.position();
72             int sl = src.arrayOffset() + src.limit();
73             assert (sp <= sl);
74             sp = (sp <= sl ? sp : sl);
75             byte[] da = dst.array();
76             int dp = dst.arrayOffset() + dst.position();
77             int dl = dst.arrayOffset() + dst.limit();
78             assert (dp <= dl);
79             dp = (dp <= dl ? dp : dl);
80 
81             try {
82                 while (sp < sl) {
83                     char c = sa[sp];
84                     if (dl - dp < 1)
85                         return CoderResult.OVERFLOW;
86                     if (!canEncode(c))
87                         return CoderResult.unmappableForLength(1);
88                     sp++;
89                     if (c >= 0x2200 && c <= 0x22ef){
90                         da[dp++] = table_math[c - 0x2200];
91                     } else if (c >= 0x0391 && c <= 0x03d6) {
92                         da[dp++]= table_greek[c - 0x0391];
93                     }
94                 }
95                 return CoderResult.UNDERFLOW;
96             } finally {
97                 src.position(sp - src.arrayOffset());
98                 dst.position(dp - dst.arrayOffset());
99             }
100         }
101 
102         private static byte[] table_math = {
103             (byte)0042, (byte)0000, (byte)0144, (byte)0044,
104             (byte)0000, (byte)0306, (byte)0104, (byte)0321,    // 00
105             (byte)0316, (byte)0317, (byte)0000, (byte)0000,
106             (byte)0000, (byte)0047, (byte)0000, (byte)0120,
107             (byte)0000, (byte)0345, (byte)0055, (byte)0000,
108             (byte)0000, (byte)0244, (byte)0000, (byte)0052,    // 10
109             (byte)0260, (byte)0267, (byte)0326, (byte)0000,
110             (byte)0000, (byte)0265, (byte)0245, (byte)0000,
111             (byte)0000, (byte)0000, (byte)0000, (byte)0275,
112             (byte)0000, (byte)0000, (byte)0000, (byte)0331,    // 20
113             (byte)0332, (byte)0307, (byte)0310, (byte)0362,
114             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
115             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
116             (byte)0134, (byte)0000, (byte)0000, (byte)0000,    // 30
117             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
118             (byte)0176, (byte)0000, (byte)0000, (byte)0000,
119             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
120             (byte)0000, (byte)0100, (byte)0000, (byte)0000,    // 40
121             (byte)0273, (byte)0000, (byte)0000, (byte)0000,
122             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
123             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
124             (byte)0000, (byte)0000, (byte)0000, (byte)0000,    // 50
125             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
126             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
127             (byte)0271, (byte)0272, (byte)0000, (byte)0000,
128             (byte)0243, (byte)0263, (byte)0000, (byte)0000,    // 60
129             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
130             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
131             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
132             (byte)0000, (byte)0000, (byte)0000, (byte)0000,    // 70
133             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
134             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
135             (byte)0000, (byte)0000, (byte)0314, (byte)0311,
136             (byte)0313, (byte)0000, (byte)0315, (byte)0312,    // 80
137             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
138             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
139             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
140             (byte)0000, (byte)0305, (byte)0000, (byte)0304,    // 90
141             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
142             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
143             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
144             (byte)0000, (byte)0136, (byte)0000, (byte)0000,    // a0
145             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
146             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
147             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
148             (byte)0000, (byte)0000, (byte)0000, (byte)0000,    // b0
149             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
150             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
151             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
152             (byte)0340, (byte)0327, (byte)0000, (byte)0000,    // c0
153             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
154             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
155             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
156             (byte)0000, (byte)0000, (byte)0000, (byte)0000,    // d0
157             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
158             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
159             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
160             (byte)0000, (byte)0000, (byte)0000, (byte)0000,    // e0
161             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
162             (byte)0000, (byte)0000, (byte)0000, (byte)0274,
163         };
164 
165         private static byte[] table_greek = {
166             (byte)0101, (byte)0102, (byte)0107,
167             (byte)0104, (byte)0105, (byte)0132, (byte)0110,    // 90
168             (byte)0121, (byte)0111, (byte)0113, (byte)0114,
169             (byte)0115, (byte)0116, (byte)0130, (byte)0117,
170             (byte)0120, (byte)0122, (byte)0000, (byte)0123,
171             (byte)0124, (byte)0125, (byte)0106, (byte)0103,    // a0
172             (byte)0131, (byte)0127, (byte)0000, (byte)0000,
173             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
174             (byte)0000, (byte)0141, (byte)0142, (byte)0147,
175             (byte)0144, (byte)0145, (byte)0172, (byte)0150,    // b0
176             (byte)0161, (byte)0151, (byte)0153, (byte)0154,
177             (byte)0155, (byte)0156, (byte)0170, (byte)0157,
178             (byte)0160, (byte)0162, (byte)0126, (byte)0163,
179             (byte)0164, (byte)0165, (byte)0146, (byte)0143,    // c0
180             (byte)0171, (byte)0167, (byte)0000, (byte)0000,
181             (byte)0000, (byte)0000, (byte)0000, (byte)0000,
182             (byte)0000, (byte)0112, (byte)0241, (byte)0000,
183             (byte)0000, (byte)0152, (byte)0166,                // d0
184         };
185 
186         /* The default implementation creates a decoder and we don't have one */
isLegalReplacement(byte[] repl)187         public boolean isLegalReplacement(byte[] repl) {
188             return true;
189         }
190     }
191 }
192