2
|
1 |
/*
|
|
2 |
* Copyright 2000-2004 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 sun.nio.cs;
|
|
27 |
|
|
28 |
import java.nio.*;
|
|
29 |
import java.nio.charset.*;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Base class for different flavors of UTF-16 encoders
|
|
33 |
*/
|
|
34 |
public abstract class UnicodeEncoder extends CharsetEncoder {
|
|
35 |
|
|
36 |
protected static final char BYTE_ORDER_MARK = '\uFEFF';
|
|
37 |
protected static final char REVERSED_MARK = '\uFFFE';
|
|
38 |
|
|
39 |
protected static final int BIG = 0;
|
|
40 |
protected static final int LITTLE = 1;
|
|
41 |
|
|
42 |
private int byteOrder; /* Byte order in use */
|
|
43 |
private boolean usesMark; /* Write an initial BOM */
|
|
44 |
private boolean needsMark;
|
|
45 |
|
|
46 |
protected UnicodeEncoder(Charset cs, int bo, boolean m) {
|
|
47 |
super(cs, 2.0f,
|
|
48 |
// Four bytes max if you need a BOM
|
|
49 |
m ? 4.0f : 2.0f,
|
|
50 |
// Replacement depends upon byte order
|
|
51 |
((bo == BIG)
|
|
52 |
? new byte[] { (byte)0xff, (byte)0xfd }
|
|
53 |
: new byte[] { (byte)0xfd, (byte)0xff }));
|
|
54 |
usesMark = needsMark = m;
|
|
55 |
byteOrder = bo;
|
|
56 |
}
|
|
57 |
|
|
58 |
private void put(char c, ByteBuffer dst) {
|
|
59 |
if (byteOrder == BIG) {
|
|
60 |
dst.put((byte)(c >> 8));
|
|
61 |
dst.put((byte)(c & 0xff));
|
|
62 |
} else {
|
|
63 |
dst.put((byte)(c & 0xff));
|
|
64 |
dst.put((byte)(c >> 8));
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
|
68 |
private final Surrogate.Parser sgp = new Surrogate.Parser();
|
|
69 |
|
|
70 |
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
|
|
71 |
int mark = src.position();
|
|
72 |
|
|
73 |
if (needsMark) {
|
|
74 |
if (dst.remaining() < 2)
|
|
75 |
return CoderResult.OVERFLOW;
|
|
76 |
put(BYTE_ORDER_MARK, dst);
|
|
77 |
needsMark = false;
|
|
78 |
}
|
|
79 |
|
|
80 |
try {
|
|
81 |
while (src.hasRemaining()) {
|
|
82 |
char c = src.get();
|
3714
|
83 |
if (!Character.isSurrogate(c)) {
|
2
|
84 |
if (dst.remaining() < 2)
|
|
85 |
return CoderResult.OVERFLOW;
|
|
86 |
mark++;
|
|
87 |
put(c, dst);
|
|
88 |
continue;
|
|
89 |
}
|
|
90 |
int d = sgp.parse(c, src);
|
|
91 |
if (d < 0)
|
|
92 |
return sgp.error();
|
|
93 |
if (dst.remaining() < 4)
|
|
94 |
return CoderResult.OVERFLOW;
|
|
95 |
mark += 2;
|
|
96 |
put(Surrogate.high(d), dst);
|
|
97 |
put(Surrogate.low(d), dst);
|
|
98 |
}
|
|
99 |
return CoderResult.UNDERFLOW;
|
|
100 |
} finally {
|
|
101 |
src.position(mark);
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
protected void implReset() {
|
|
106 |
needsMark = usesMark;
|
|
107 |
}
|
|
108 |
|
|
109 |
public boolean canEncode(char c) {
|
3714
|
110 |
return ! Character.isSurrogate(c);
|
2
|
111 |
}
|
|
112 |
}
|