01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.lang.reflect.support;
19:
20: /**
21: * @author Serguei S. Zapreyev
22: * @version $Revision: 1.1.2.1 $
23: */
24: public final class AuxiliaryUtil {
25:
26: /**
27: * For temporary using until I has problem with Character.codePointAt(int) using.
28: */
29: /**/private static int codePointAt(char[] a, int index) {
30: /**/int ch1 = a[index]; // NullPointerException or IndexOutOfBoundsException may be arisen here
31: /**/
32: if (ch1 >= 0xD800 && ch1 <= 0xDBFF) {
33: /**/if (index++ < a.length) {
34: /**/int ch2 = a[index];
35: /**/if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
36: /**/return ((ch1 - 0xD800) << 10 | (ch2 - 0xDC00)) + 65536;
37: /**/}
38: /**/}
39: /**/}
40: /**/return ch1;
41: /**/}
42:
43: /**
44: * To transform to UTF8 representation.
45: */
46: public static String toUTF8(String ini) {
47: if (ini == null)
48: return ini;
49: StringBuffer sb = new StringBuffer();
50: int cp;
51: int dgt;
52: /**/char ca[] = ini.toCharArray();
53: for (int i = 0; i < ini.length();) {
54: /**///if((cp = ini.codePointAt(i)) <= '\u007F') {
55: /**/if ((cp = codePointAt(ca, i)) <= '\u007F') {
56: sb.append(Character.toString((char) cp));
57: i++;
58: } else if (cp <= '\u07FF') {
59: sb.append("\\0");
60: dgt = 0xC0 + ((cp & 0x7C0) >> 6);
61: sb.append(Integer.toString(dgt, 16));
62: sb.append("\\0");
63: dgt = 0x80 + (cp & 0x3F);
64: sb.append(Integer.toString(dgt, 16));
65: i++;
66: } else if (cp <= '\uFFFF') {
67: sb.append("\\0");
68: dgt = 0xE0 + ((cp & 0xF000) >> 12);
69: sb.append(Integer.toString(dgt, 16));
70: sb.append("\\0");
71: dgt = 0x80 + ((cp & 0xFC0) >> 6);
72: sb.append(Integer.toString(dgt, 16));
73: sb.append("\\0");
74: dgt = 0x80 + (cp & 0x3F);
75: sb.append(Integer.toString(dgt, 16));
76: i++;
77: } else { // > '\uFFFF'
78: sb.append("\\0ED");
79: sb.append("\\0");
80: dgt = 0xA0 + (((cp & 0xF0000) >> 16) - 1);
81: sb.append(Integer.toString(dgt, 16));
82: sb.append("\\0");
83: dgt = 0x80 + ((cp & 0xFC00) >> 10);
84: sb.append(Integer.toString(dgt, 16));
85: sb.append("\\0ED");
86: sb.append("\\0");
87: dgt = 0xB0 + ((cp & 0x3C0) >> 6);
88: sb.append(Integer.toString(dgt, 16));
89: sb.append("\\0");
90: dgt = 0x80 + (cp & 0x3F);
91: sb.append(Integer.toString(dgt, 16));
92: i += 2;
93: }
94: }
95: return sb.toString();
96: }
97: }
|