Source Code Cross Referenced for Convert.java in  » 6.0-JDK-Modules-com.sun » tools » com » sun » tools » javac » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules com.sun » tools » com.sun.tools.javac.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 1999-2006 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:
026:        package com.sun.tools.javac.util;
027:
028:        /** Utility class for static conversion methods between numbers
029:         *  and strings in various formats.
030:         *
031:         *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
032:         *  you write code that depends on this, you do so at your own risk.
033:         *  This code and its internal interfaces are subject to change or
034:         *  deletion without notice.</b>
035:         */
036:        @Version("@(#)Convert.java	1.29 07/05/05")
037:        public class Convert {
038:
039:            /** Convert string to integer.
040:             */
041:            public static int string2int(String s, int radix)
042:                    throws NumberFormatException {
043:                if (radix == 10) {
044:                    return Integer.parseInt(s, radix);
045:                } else {
046:                    char[] cs = s.toCharArray();
047:                    int limit = Integer.MAX_VALUE / (radix / 2);
048:                    int n = 0;
049:                    for (int i = 0; i < cs.length; i++) {
050:                        int d = Character.digit(cs[i], radix);
051:                        if (n < 0 || n > limit
052:                                || n * radix > Integer.MAX_VALUE - d)
053:                            throw new NumberFormatException();
054:                        n = n * radix + d;
055:                    }
056:                    return n;
057:                }
058:            }
059:
060:            /** Convert string to long integer.
061:             */
062:            public static long string2long(String s, int radix)
063:                    throws NumberFormatException {
064:                if (radix == 10) {
065:                    return Long.parseLong(s, radix);
066:                } else {
067:                    char[] cs = s.toCharArray();
068:                    long limit = Long.MAX_VALUE / (radix / 2);
069:                    long n = 0;
070:                    for (int i = 0; i < cs.length; i++) {
071:                        int d = Character.digit(cs[i], radix);
072:                        if (n < 0 || n > limit
073:                                || n * radix > Long.MAX_VALUE - d)
074:                            throw new NumberFormatException();
075:                        n = n * radix + d;
076:                    }
077:                    return n;
078:                }
079:            }
080:
081:            /* Conversion routines between names, strings, and byte arrays in Utf8 format
082:             */
083:
084:            /** Convert `len' bytes from utf8 to characters.
085:             *  Parameters are as in System.arraycopy
086:             *  Return first index in `dst' past the last copied char.
087:             *  @param src        The array holding the bytes to convert.
088:             *  @param sindex     The start index from which bytes are converted.
089:             *  @param dst        The array holding the converted characters..
090:             *  @param dindex     The start index from which converted characters
091:             *                    are written.
092:             *  @param len        The maximum number of bytes to convert.
093:             */
094:            public static int utf2chars(byte[] src, int sindex, char[] dst,
095:                    int dindex, int len) {
096:                int i = sindex;
097:                int j = dindex;
098:                int limit = sindex + len;
099:                while (i < limit) {
100:                    int b = src[i++] & 0xFF;
101:                    if (b >= 0xE0) {
102:                        b = (b & 0x0F) << 12;
103:                        b = b | (src[i++] & 0x3F) << 6;
104:                        b = b | (src[i++] & 0x3F);
105:                    } else if (b >= 0xC0) {
106:                        b = (b & 0x1F) << 6;
107:                        b = b | (src[i++] & 0x3F);
108:                    }
109:                    dst[j++] = (char) b;
110:                }
111:                return j;
112:            }
113:
114:            /** Return bytes in Utf8 representation as an array of characters.
115:             *  @param src        The array holding the bytes.
116:             *  @param sindex     The start index from which bytes are converted.
117:             *  @param len        The maximum number of bytes to convert.
118:             */
119:            public static char[] utf2chars(byte[] src, int sindex, int len) {
120:                char[] dst = new char[len];
121:                int len1 = utf2chars(src, sindex, dst, 0, len);
122:                char[] result = new char[len1];
123:                System.arraycopy(dst, 0, result, 0, len1);
124:                return result;
125:            }
126:
127:            /** Return all bytes of a given array in Utf8 representation
128:             *  as an array of characters.
129:             *  @param src        The array holding the bytes.
130:             */
131:            public static char[] utf2chars(byte[] src) {
132:                return utf2chars(src, 0, src.length);
133:            }
134:
135:            /** Return bytes in Utf8 representation as a string.
136:             *  @param src        The array holding the bytes.
137:             *  @param sindex     The start index from which bytes are converted.
138:             *  @param len        The maximum number of bytes to convert.
139:             */
140:            public static String utf2string(byte[] src, int sindex, int len) {
141:                char dst[] = new char[len];
142:                int len1 = utf2chars(src, sindex, dst, 0, len);
143:                return new String(dst, 0, len1);
144:            }
145:
146:            /** Return all bytes of a given array in Utf8 representation
147:             *  as a string.
148:             *  @param src        The array holding the bytes.
149:             */
150:            public static String utf2string(byte[] src) {
151:                return utf2string(src, 0, src.length);
152:            }
153:
154:            /** Copy characters in source array to bytes in target array,
155:             *  converting them to Utf8 representation.
156:             *  The target array must be large enough to hold the result.
157:             *  returns first index in `dst' past the last copied byte.
158:             *  @param src        The array holding the characters to convert.
159:             *  @param sindex     The start index from which characters are converted.
160:             *  @param dst        The array holding the converted characters..
161:             *  @param dindex     The start index from which converted bytes
162:             *                    are written.
163:             *  @param len        The maximum number of characters to convert.
164:             */
165:            public static int chars2utf(char[] src, int sindex, byte[] dst,
166:                    int dindex, int len) {
167:                int j = dindex;
168:                int limit = sindex + len;
169:                for (int i = sindex; i < limit; i++) {
170:                    char ch = src[i];
171:                    if (1 <= ch && ch <= 0x7F) {
172:                        dst[j++] = (byte) ch;
173:                    } else if (ch <= 0x7FF) {
174:                        dst[j++] = (byte) (0xC0 | (ch >> 6));
175:                        dst[j++] = (byte) (0x80 | (ch & 0x3F));
176:                    } else {
177:                        dst[j++] = (byte) (0xE0 | (ch >> 12));
178:                        dst[j++] = (byte) (0x80 | ((ch >> 6) & 0x3F));
179:                        dst[j++] = (byte) (0x80 | (ch & 0x3F));
180:                    }
181:                }
182:                return j;
183:            }
184:
185:            /** Return characters as an array of bytes in Utf8 representation.
186:             *  @param src        The array holding the characters.
187:             *  @param sindex     The start index from which characters are converted.
188:             *  @param len        The maximum number of characters to convert.
189:             */
190:            public static byte[] chars2utf(char[] src, int sindex, int len) {
191:                byte[] dst = new byte[len * 3];
192:                int len1 = chars2utf(src, sindex, dst, 0, len);
193:                byte[] result = new byte[len1];
194:                System.arraycopy(dst, 0, result, 0, len1);
195:                return result;
196:            }
197:
198:            /** Return all characters in given array as an array of bytes
199:             *  in Utf8 representation.
200:             *  @param src        The array holding the characters.
201:             */
202:            public static byte[] chars2utf(char[] src) {
203:                return chars2utf(src, 0, src.length);
204:            }
205:
206:            /** Return string as an array of bytes in in Utf8 representation.
207:             */
208:            public static byte[] string2utf(String s) {
209:                return chars2utf(s.toCharArray());
210:            }
211:
212:            /**
213:             * Escapes each character in a string that has an escape sequence or
214:             * is non-printable ASCII.  Leaves non-ASCII characters alone.
215:             */
216:            public static String quote(String s) {
217:                StringBuilder buf = new StringBuilder();
218:                for (int i = 0; i < s.length(); i++) {
219:                    buf.append(quote(s.charAt(i)));
220:                }
221:                return buf.toString();
222:            }
223:
224:            /**
225:             * Escapes a character if it has an escape sequence or is
226:             * non-printable ASCII.  Leaves non-ASCII characters alone.
227:             */
228:            public static String quote(char ch) {
229:                switch (ch) {
230:                case '\b':
231:                    return "\\b";
232:                case '\f':
233:                    return "\\f";
234:                case '\n':
235:                    return "\\n";
236:                case '\r':
237:                    return "\\r";
238:                case '\t':
239:                    return "\\t";
240:                case '\'':
241:                    return "\\'";
242:                case '\"':
243:                    return "\\\"";
244:                case '\\':
245:                    return "\\\\";
246:                default:
247:                    return (ch > 127 || isPrintableAscii(ch)) ? String
248:                            .valueOf(ch) : String.format("\\%03o", (int) ch);
249:                }
250:            }
251:
252:            /**
253:             * Is a character printable ASCII?
254:             */
255:            private static boolean isPrintableAscii(char ch) {
256:                return ch >= ' ' && ch <= '~';
257:            }
258:
259:            /** Escape all unicode characters in string.
260:             */
261:            public static String escapeUnicode(String s) {
262:                int len = s.length();
263:                int i = 0;
264:                while (i < len) {
265:                    char ch = s.charAt(i);
266:                    if (ch > 255) {
267:                        StringBuffer buf = new StringBuffer();
268:                        buf.append(s.substring(0, i));
269:                        while (i < len) {
270:                            ch = s.charAt(i);
271:                            if (ch > 255) {
272:                                buf.append("\\u");
273:                                buf.append(Character.forDigit((ch >> 12) % 16,
274:                                        16));
275:                                buf.append(Character.forDigit((ch >> 8) % 16,
276:                                        16));
277:                                buf.append(Character.forDigit((ch >> 4) % 16,
278:                                        16));
279:                                buf.append(Character.forDigit((ch) % 16, 16));
280:                            } else {
281:                                buf.append(ch);
282:                            }
283:                            i++;
284:                        }
285:                        s = buf.toString();
286:                    } else {
287:                        i++;
288:                    }
289:                }
290:                return s;
291:            }
292:
293:            /* Conversion routines for qualified name splitting
294:             */
295:            /** Return the last part of a class name.
296:             */
297:            public static Name shortName(Name classname) {
298:                return classname.subName(classname.lastIndexOf((byte) '.') + 1,
299:                        classname.len);
300:            }
301:
302:            public static String shortName(String classname) {
303:                return classname.substring(classname.lastIndexOf('.') + 1);
304:            }
305:
306:            /** Return the package name of a class name, excluding the trailing '.',
307:             *  "" if not existent.
308:             */
309:            public static Name packagePart(Name classname) {
310:                return classname.subName(0, classname.lastIndexOf((byte) '.'));
311:            }
312:
313:            public static String packagePart(String classname) {
314:                int lastDot = classname.lastIndexOf('.');
315:                return (lastDot < 0 ? "" : classname.substring(0, lastDot));
316:            }
317:
318:            public static List<Name> enclosingCandidates(Name name) {
319:                List<Name> names = List.nil();
320:                int index;
321:                while ((index = name.lastIndexOf((byte) '$')) > 0) {
322:                    name = name.subName(0, index);
323:                    names = names.prepend(name);
324:                }
325:                return names;
326:            }
327:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.