Source Code Cross Referenced for CharParser.java in  » Web-Framework » RSF » uk » org » ponder » stringutil » 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 » Web Framework » RSF » uk.org.ponder.stringutil 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


01:        package uk.org.ponder.stringutil;
02:
03:        /** A useful class which quickly parses numbers from character arrays rather than
04:         * from strings as the slovenly Java libraries do.
05:         */
06:
07:        public class CharParser {
08:            private static final int MAXO10 = Integer.MAX_VALUE / 10; //  214748364
09:            private static final int MAXO16 = Integer.MAX_VALUE / 16; //  134217727
10:
11:            /** Parses a positive integer from the contents of a character array. The 
12:             * specified section of the array must contain no non-numeric characters.
13:             * @param buffer The array containing the data to be parsed.
14:             * @param start The start position of the numeric data.
15:             * @param length The length of the numeric data.
16:             * @exception NumberFormatException If the specified array section includes
17:             * non-numeric characters or represents an integer out of range for a positive
18:             * signed 32-bit integer.
19:             */
20:
21:            public static int parsePositiveInt(char[] buffer, int start,
22:                    int length) throws NumberFormatException {
23:                int accumulate = 0;
24:                for (int i = 0; i < length; ++i) {
25:                    char c = buffer[i + start];
26:                    if (c < '0' || c > '9')
27:                        throw new NumberFormatException("Invalid character "
28:                                + c + " in positive integer");
29:                    if (accumulate > MAXO10)
30:                        throw new NumberFormatException(
31:                                "Number found greater than maximum integer: "
32:                                        + new String(buffer, start, length));
33:                    accumulate *= 10;
34:                    int uncess = Integer.MAX_VALUE - accumulate;
35:                    int digit = (c - '0');
36:                    if (digit > uncess)
37:                        throw new NumberFormatException(
38:                                "Number found greater than maximum integer: "
39:                                        + new String(buffer, start, length));
40:                    accumulate += (c - '0');
41:                }
42:                return accumulate;
43:            }
44:
45:            /** Parses a single hex digit as its integer equivalent.
46:             * @return The integer corresponding to the supplied hex digit, or -1 if the
47:             * character is not a valid hex digit.
48:             */
49:
50:            public static int fromHex(char digit) {
51:                digit = Character.toUpperCase(digit);
52:                if (digit <= '9' && digit >= '0')
53:                    return digit - '0';
54:                else if (digit <= 'F' && digit >= 'A')
55:                    return 10 + digit - 'A';
56:                else
57:                    return -1;
58:            }
59:
60:            /** Parses a positive integer stored as hexadecimal from the contents of a 
61:             * character array. The specified section of the array must contain no non-hexadecimal
62:             * characters.
63:             * @param buffer The array containing the data to be parsed.
64:             * @param start The start position of the numeric data.
65:             * @param length The length of the numeric data.
66:             * @exception NumberFormatException If the specified array section includes
67:             * non-hexadecimal characters or represents an integer out of range for a positive
68:             * signed 32-bit integer.
69:             */
70:
71:            public static int parseHexInt(char[] buffer, int start, int length)
72:                    throws NumberFormatException {
73:                int accumulate = 0;
74:                for (int i = 0; i < length; ++i) {
75:                    char c = buffer[i + start];
76:                    int digit = fromHex(c);
77:                    if (digit == -1)
78:                        throw new NumberFormatException("Invalid character "
79:                                + c + " in hex string");
80:                    if (accumulate > MAXO16)
81:                        throw new NumberFormatException(
82:                                "Number found greater than maximum integer: "
83:                                        + new String(buffer, start, length));
84:                    accumulate *= 16;
85:                    int uncess = Integer.MAX_VALUE - accumulate;
86:                    if (digit > uncess)
87:                        throw new NumberFormatException(
88:                                "Number found greater than maximum integer: "
89:                                        + new String(buffer, start, length));
90:                    accumulate += digit;
91:                }
92:                return accumulate;
93:            }
94:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.