KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > components > encoding > UTF8Encoder


1 /*
2
3  * Copyright 2001-2004 The Apache Software Foundation.
4
5  *
6
7  * Licensed under the Apache License, Version 2.0 (the "License");
8
9  * you may not use this file except in compliance with the License.
10
11  * You may obtain a copy of the License at
12
13  *
14
15  * http://www.apache.org/licenses/LICENSE-2.0
16
17  *
18
19  * Unless required by applicable law or agreed to in writing, software
20
21  * distributed under the License is distributed on an "AS IS" BASIS,
22
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
25  * See the License for the specific language governing permissions and
26
27  * limitations under the License.
28
29  */

30
31 package org.jboss.axis.components.encoding;
32
33
34 /**
35  * UTF-8 Encoder.
36  *
37  * @author <a HREF="mailto:jens@void.fm">Jens Schumann</a>
38  * @see <a HREF="http://encoding.org">encoding.org</a>
39  * @see <a HREF="http://czyborra.com/utf/#UTF-8">UTF 8 explained</a>
40  */

41
42 class UTF8Encoder extends AbstractXMLEncoder
43 {
44
45    public String JavaDoc getEncoding()
46    {
47
48       return XMLEncoderFactory.ENCODING_UTF_8;
49
50    }
51
52
53    public boolean needsEncoding(char c)
54    {
55
56       return c > 0x7F;
57
58    }
59
60
61    public void appendEncoded(EncodedByteArray out, char c)
62    {
63
64       if (c < 0x80)
65       {
66
67          out.append(c);
68
69       }
70       else if (c < 0x800)
71       {
72
73          out.append((0xC0 | c >> 6));
74
75          out.append((0x80 | c & 0x3F));
76
77       }
78       else if (c < 0x10000)
79       {
80
81          out.append((0xE0 | c >> 12));
82
83          out.append((0x80 | c >> 6 & 0x3F));
84
85          out.append((0x80 | c & 0x3F));
86
87       }
88       else if (c < 0x200000)
89       {
90
91          out.append((0xF0 | c >> 18));
92
93          out.append((0x80 | c >> 12 & 0x3F));
94
95          out.append((0x80 | c >> 6 & 0x3F));
96
97          out.append((0x80 | c & 0x3F));
98
99       }
100
101    }
102
103 }
104
105
Popular Tags