|
1 /* |
|
2 * Copyright 2006 Sun Microsystems, Inc. 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package com.sun.codemodel.internal.util; |
|
27 |
|
28 import java.nio.ByteBuffer; |
|
29 import java.nio.CharBuffer; |
|
30 import java.nio.charset.Charset; |
|
31 import java.nio.charset.CharsetEncoder; |
|
32 import java.nio.charset.CoderResult; |
|
33 |
|
34 import sun.nio.cs.Surrogate; |
|
35 |
|
36 |
|
37 abstract class SingleByteEncoder |
|
38 extends CharsetEncoder |
|
39 { |
|
40 |
|
41 private final short index1[]; |
|
42 private final String index2; |
|
43 private final int mask1; |
|
44 private final int mask2; |
|
45 private final int shift; |
|
46 |
|
47 private final Surrogate.Parser sgp = new Surrogate.Parser(); |
|
48 |
|
49 protected SingleByteEncoder(Charset cs, |
|
50 short[] index1, String index2, |
|
51 int mask1, int mask2, int shift) |
|
52 { |
|
53 super(cs, 1.0f, 1.0f); |
|
54 this.index1 = index1; |
|
55 this.index2 = index2; |
|
56 this.mask1 = mask1; |
|
57 this.mask2 = mask2; |
|
58 this.shift = shift; |
|
59 } |
|
60 |
|
61 public boolean canEncode(char c) { |
|
62 char testEncode; |
|
63 testEncode = index2.charAt(index1[(c & mask1) >> shift] |
|
64 + (c & mask2)); |
|
65 if (testEncode == '\u0000') |
|
66 return false; |
|
67 else |
|
68 return true; |
|
69 } |
|
70 |
|
71 private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { |
|
72 char[] sa = src.array(); |
|
73 int sp = src.arrayOffset() + src.position(); |
|
74 int sl = src.arrayOffset() + src.limit(); |
|
75 sp = (sp <= sl ? sp : sl); |
|
76 byte[] da = dst.array(); |
|
77 int dp = dst.arrayOffset() + dst.position(); |
|
78 int dl = dst.arrayOffset() + dst.limit(); |
|
79 dp = (dp <= dl ? dp : dl); |
|
80 |
|
81 try { |
|
82 while (sp < sl) { |
|
83 char c = sa[sp]; |
|
84 if (Surrogate.is(c)) { |
|
85 if (sgp.parse(c, sa, sp, sl) < 0) |
|
86 return sgp.error(); |
|
87 return sgp.unmappableResult(); |
|
88 } |
|
89 if (c >= '\uFFFE') |
|
90 return CoderResult.unmappableForLength(1); |
|
91 if (dl - dp < 1) |
|
92 return CoderResult.OVERFLOW; |
|
93 |
|
94 char e = index2.charAt(index1[(c & mask1) >> shift] |
|
95 + (c & mask2)); |
|
96 |
|
97 // If output byte is zero because input char is zero |
|
98 // then character is mappable, o.w. fail |
|
99 if (e == '\u0000' && c != '\u0000') |
|
100 return CoderResult.unmappableForLength(1); |
|
101 |
|
102 sp++; |
|
103 da[dp++] = (byte)e; |
|
104 } |
|
105 return CoderResult.UNDERFLOW; |
|
106 } finally { |
|
107 src.position(sp - src.arrayOffset()); |
|
108 dst.position(dp - dst.arrayOffset()); |
|
109 } |
|
110 } |
|
111 |
|
112 private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) { |
|
113 int mark = src.position(); |
|
114 try { |
|
115 while (src.hasRemaining()) { |
|
116 char c = src.get(); |
|
117 if (Surrogate.is(c)) { |
|
118 if (sgp.parse(c, src) < 0) |
|
119 return sgp.error(); |
|
120 return sgp.unmappableResult(); |
|
121 } |
|
122 if (c >= '\uFFFE') |
|
123 return CoderResult.unmappableForLength(1); |
|
124 if (!dst.hasRemaining()) |
|
125 return CoderResult.OVERFLOW; |
|
126 |
|
127 char e = index2.charAt(index1[(c & mask1) >> shift] |
|
128 + (c & mask2)); |
|
129 |
|
130 // If output byte is zero because input char is zero |
|
131 // then character is mappable, o.w. fail |
|
132 if (e == '\u0000' && c != '\u0000') |
|
133 return CoderResult.unmappableForLength(1); |
|
134 |
|
135 mark++; |
|
136 dst.put((byte)e); |
|
137 } |
|
138 return CoderResult.UNDERFLOW; |
|
139 } finally { |
|
140 src.position(mark); |
|
141 } |
|
142 } |
|
143 |
|
144 protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { |
|
145 if (true && src.hasArray() && dst.hasArray()) |
|
146 return encodeArrayLoop(src, dst); |
|
147 else |
|
148 return encodeBufferLoop(src, dst); |
|
149 } |
|
150 |
|
151 public byte encode(char inputChar) { |
|
152 return (byte)index2.charAt(index1[(inputChar & mask1) >> shift] + |
|
153 (inputChar & mask2)); |
|
154 } |
|
155 } |