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 /*
27 */
28
29 //package sun.nio.cs.ext;
30
31 import java.nio.ByteBuffer;
32 import java.nio.CharBuffer;
33 import java.nio.charset.Charset;
34 import java.nio.charset.CharsetEncoder;
35 import java.nio.charset.CoderResult;
36 import sun.nio.cs.Surrogate;
37
38 public abstract class DoubleByteEncoder
39 extends CharsetEncoder
40 {
41
42 private short index1[];
43 private String index2[];
44
45 private final Surrogate.Parser sgp = new Surrogate.Parser();
46
47 protected DoubleByteEncoder(Charset cs,
48 short[] index1, String[] index2)
49 {
78 this.index1 = index1;
79 this.index2 = index2;
80 }
81
82 public boolean canEncode(char c) {
83 return (encodeSingle(c) != -1 ||
84 encodeDouble(c) != 0);
85 }
86
87 private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
88 char[] sa = src.array();
89 int sp = src.arrayOffset() + src.position();
90 int sl = src.arrayOffset() + src.limit();
91 byte[] da = dst.array();
92 int dp = dst.arrayOffset() + dst.position();
93 int dl = dst.arrayOffset() + dst.limit();
94
95 try {
96 while (sp < sl) {
97 char c = sa[sp];
98 if (Surrogate.is(c)) {
99 if (sgp.parse(c, sa, sp, sl) < 0)
100 return sgp.error();
101 if (sl - sp < 2)
102 return CoderResult.UNDERFLOW;
103 char c2 = sa[sp + 1];
104
105 byte[] outputBytes = new byte[2];
106 outputBytes = encodeSurrogate(c, c2);
107
108 if (outputBytes == null) {
109 return sgp.unmappableResult();
110 }
111 else {
112 if (dl - dp < 2)
113 return CoderResult.OVERFLOW;
114 da[dp++] = outputBytes[0];
115 da[dp++] = outputBytes[1];
116 sp += 2;
117 continue;
118 }
136 da[dp++] = (byte) ((ncode & 0xff00) >> 8);
137 da[dp++] = (byte) (ncode & 0xff);
138 sp++;
139 continue;
140 }
141 return CoderResult.unmappableForLength(1);
142 }
143 return CoderResult.UNDERFLOW;
144 } finally {
145 src.position(sp - src.arrayOffset());
146 dst.position(dp - dst.arrayOffset());
147 }
148 }
149
150 private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
151 int mark = src.position();
152
153 try {
154 while (src.hasRemaining()) {
155 char c = src.get();
156 if (Surrogate.is(c)) {
157 int surr;
158 if ((surr = sgp.parse(c, src)) < 0)
159 return sgp.error();
160 char c2 = Surrogate.low(surr);
161 byte[] outputBytes = new byte[2];
162 outputBytes = encodeSurrogate(c, c2);
163
164 if (outputBytes == null) {
165 return sgp.unmappableResult();
166 } else {
167 if (dst.remaining() < 2)
168 return CoderResult.OVERFLOW;
169 mark += 2;
170 dst.put(outputBytes[0]);
171 dst.put(outputBytes[1]);
172 continue;
173 }
174 }
175 if (c >= '\uFFFE')
176 return CoderResult.unmappableForLength(1);
|
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 /*
27 */
28
29
30 import java.nio.ByteBuffer;
31 import java.nio.CharBuffer;
32 import java.nio.charset.Charset;
33 import java.nio.charset.CharsetEncoder;
34 import java.nio.charset.CoderResult;
35 import sun.nio.cs.Surrogate;
36
37 public abstract class DoubleByteEncoder
38 extends CharsetEncoder
39 {
40
41 private short index1[];
42 private String index2[];
43
44 private final Surrogate.Parser sgp = new Surrogate.Parser();
45
46 protected DoubleByteEncoder(Charset cs,
47 short[] index1, String[] index2)
48 {
77 this.index1 = index1;
78 this.index2 = index2;
79 }
80
81 public boolean canEncode(char c) {
82 return (encodeSingle(c) != -1 ||
83 encodeDouble(c) != 0);
84 }
85
86 private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
87 char[] sa = src.array();
88 int sp = src.arrayOffset() + src.position();
89 int sl = src.arrayOffset() + src.limit();
90 byte[] da = dst.array();
91 int dp = dst.arrayOffset() + dst.position();
92 int dl = dst.arrayOffset() + dst.limit();
93
94 try {
95 while (sp < sl) {
96 char c = sa[sp];
97 if (Character.isSurrogate(c)) {
98 if (sgp.parse(c, sa, sp, sl) < 0)
99 return sgp.error();
100 if (sl - sp < 2)
101 return CoderResult.UNDERFLOW;
102 char c2 = sa[sp + 1];
103
104 byte[] outputBytes = new byte[2];
105 outputBytes = encodeSurrogate(c, c2);
106
107 if (outputBytes == null) {
108 return sgp.unmappableResult();
109 }
110 else {
111 if (dl - dp < 2)
112 return CoderResult.OVERFLOW;
113 da[dp++] = outputBytes[0];
114 da[dp++] = outputBytes[1];
115 sp += 2;
116 continue;
117 }
135 da[dp++] = (byte) ((ncode & 0xff00) >> 8);
136 da[dp++] = (byte) (ncode & 0xff);
137 sp++;
138 continue;
139 }
140 return CoderResult.unmappableForLength(1);
141 }
142 return CoderResult.UNDERFLOW;
143 } finally {
144 src.position(sp - src.arrayOffset());
145 dst.position(dp - dst.arrayOffset());
146 }
147 }
148
149 private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
150 int mark = src.position();
151
152 try {
153 while (src.hasRemaining()) {
154 char c = src.get();
155 if (Character.isSurrogate(c)) {
156 int surr;
157 if ((surr = sgp.parse(c, src)) < 0)
158 return sgp.error();
159 char c2 = Surrogate.low(surr);
160 byte[] outputBytes = new byte[2];
161 outputBytes = encodeSurrogate(c, c2);
162
163 if (outputBytes == null) {
164 return sgp.unmappableResult();
165 } else {
166 if (dst.remaining() < 2)
167 return CoderResult.OVERFLOW;
168 mark += 2;
169 dst.put(outputBytes[0]);
170 dst.put(outputBytes[1]);
171 continue;
172 }
173 }
174 if (c >= '\uFFFE')
175 return CoderResult.unmappableForLength(1);
|