Source Code Cross Referenced for CharArraySource.java in  » Parser » JTopas » de » susebox » jtopas » 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 » Parser » JTopas » de.susebox.jtopas 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * CharArraySource.java: TokenizerSource implementation for character buffers.
003:         *
004:         * Copyright (C) 2003 Heiko Blau
005:         *
006:         * This file belongs to the JTopas Library.
007:         * JTopas is free software; you can redistribute it and/or modify it 
008:         * under the terms of the GNU Lesser General Public License as published by the 
009:         * Free Software Foundation; either version 2.1 of the License, or (at your 
010:         * option) any later version.
011:         *
012:         * This software is distributed in the hope that it will be useful, but WITHOUT
013:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
014:         * FITNESS FOR A PARTICULAR PURPOSE. 
015:         * See the GNU Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public License along
018:         * with JTopas. If not, write to the
019:         *
020:         *   Free Software Foundation, Inc.
021:         *   59 Temple Place, Suite 330, 
022:         *   Boston, MA 02111-1307 
023:         *   USA
024:         *
025:         * or check the Internet: http://www.fsf.org
026:         *
027:         * Contact:
028:         *   email: heiko@susebox.de 
029:         */
030:
031:        package de.susebox.jtopas;
032:
033:        //-----------------------------------------------------------------------------
034:        // Class CharArraySource
035:        //
036:
037:        /**
038:         * Implementation of the {@link TokenizerSource} and its extension 
039:         * {@link CharSequenceTokenizerSource} for character arrays. It is a shortcut for:
040:         *<block><pre>
041:         *   char[]           myData = { ... };
042:         *   TokenizerSource  source = new ReaderSource(new CharArrayReader(myData));
043:         *</pre></block>
044:         * The class also provides a faster access to the data in a character array
045:         * through the methods of the {@link java.lang.CharSequence} interface.
046:         * Since this interface was only introduced with Java 1.4, this class can only
047:         * be used with 1.4 or higher Java versions.
048:         *
049:         * @see     TokenizerSource
050:         * @see     CharSequenceTokenizerSource
051:         * @see     StringSource
052:         * @author  Heiko Blau
053:         */
054:        public class CharArraySource implements  CharSequenceTokenizerSource {
055:
056:            //---------------------------------------------------------------------------
057:            // Constructors
058:            //
059:
060:            /**
061:             * Constructing a <code>CharArraySource</code> instance using the
062:             * given character buffer starting with offset 0 and the full length of the
063:             * array.
064:             *
065:             * @param data  the character data to tokenize
066:             */
067:            public CharArraySource(char[] data) {
068:                this (data, 0, (data != null) ? data.length : -1);
069:            }
070:
071:            /**
072:             * Constructing an <code>CharArraySource</code> instance using the
073:             * given character buffer starting with the given offset 0 and the given length.
074:             *
075:             * @param data    the character data to tokenize
076:             * @param offset  there to start in the given data
077:             * @param length  count of characters to tokenize
078:             */
079:            public CharArraySource(char[] data, int offset, int length) {
080:                _offset = offset;
081:                if (data == null || length < 0) {
082:                    _data = null;
083:                    _length = -1;
084:                } else {
085:                    _data = data;
086:                    _length = length;
087:                }
088:            }
089:
090:            //---------------------------------------------------------------------------
091:            // Methods of the TokenizerSource interface
092:            //
093:
094:            /**
095:             * This method copies the available data into the given buffer according to 
096:             * the given offset and maximum character count. See {@link TokenizerSource#read}
097:             * for details.
098:             *
099:             * @param cbuf      buffer to receive data
100:             * @param offset    position from where the data should be inserted in <code>cbuf</code>
101:             * @param maxChars  maximum number of characters to be read into <code>cbuf</code>
102:             * @return actually read characters or -1 on an end-of-file condition
103:             */
104:            public int read(char[] cbuf, final int offset, final int maxChars)
105:                    throws Exception {
106:                int left = Math.min(_length - _readOffset, maxChars);
107:
108:                if (left > 0) {
109:                    System.arraycopy(_data, _offset + _readOffset, cbuf,
110:                            offset, left);
111:                    _readOffset += left;
112:                    return left;
113:                } else {
114:                    return -1;
115:                }
116:            }
117:
118:            //---------------------------------------------------------------------------
119:            // Methods of the CharSequence interface
120:            //
121:
122:            /**
123:             * Implements {@link java.lang.CharSequence#charAt} for this class. Note that
124:             * the given index is relative to the offset that was passed when this instance
125:             * was constructed ({@link #CharArraySource(char[], int, int)})
126:             *
127:             * @param index   which character to retrieve
128:             * @return the character at the given index
129:             */
130:            public char charAt(int index) {
131:                return _data[_offset + index];
132:            }
133:
134:            /**
135:             * Implements {@link java.lang.CharSequence#length} for this class.
136:             *
137:             * @return the number of available characters
138:             */
139:            public int length() {
140:                return _length;
141:            }
142:
143:            /**
144:             * Implements {@link java.lang.CharSequence#subSequence} for this class.
145:             *
146:             * @param start   the new <code>CharSequence</code> contains the characters 
147:             *                from and including this position
148:             * @param end     the new <code>CharSequence</code> contains the characters 
149:             *                up to and excluding this position
150:             * @return a part of this <code>CharSequence</code>
151:             */
152:            public CharSequence subSequence(int start, int end) {
153:                return new CharArraySource(_data, _offset + start, end - start);
154:            }
155:
156:            /**
157:             * Returns the string representation of this <code>CharArraySource</code> according
158:             * to the {@link java.lang.CharSequence} interface contract.
159:             *
160:             * @return  a string containing all characters of this <code>CharArraySource</code>
161:             */
162:            public String toString() {
163:                return new String(_data, _offset, _length);
164:            }
165:
166:            //---------------------------------------------------------------------------
167:            // Members
168:            //
169:            private char[] _data = null;
170:            private int _offset = 0;
171:            private int _length = -1;
172:            private int _readOffset = 0;
173:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.