Source Code Cross Referenced for AwkStreamInput.java in  » Library » jakarta-oro-2.0.8 » org » apache » oro » text » awk » 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 » Library » jakarta oro 2.0.8 » org.apache.oro.text.awk 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: AwkStreamInput.java,v 1.7 2003/11/07 20:16:24 dfs Exp $
003:         *
004:         * ====================================================================
005:         * The Apache Software License, Version 1.1
006:         *
007:         * Copyright (c) 2000 The Apache Software Foundation.  All rights
008:         * reserved.
009:         *
010:         * Redistribution and use in source and binary forms, with or without
011:         * modification, are permitted provided that the following conditions
012:         * are met:
013:         *
014:         * 1. Redistributions of source code must retain the above copyright
015:         *    notice, this list of conditions and the following disclaimer.
016:         *
017:         * 2. Redistributions in binary form must reproduce the above copyright
018:         *    notice, this list of conditions and the following disclaimer in
019:         *    the documentation and/or other materials provided with the
020:         *    distribution.
021:         *
022:         * 3. The end-user documentation included with the redistribution,
023:         *    if any, must include the following acknowledgment:
024:         *       "This product includes software developed by the
025:         *        Apache Software Foundation (http://www.apache.org/)."
026:         *    Alternately, this acknowledgment may appear in the software itself,
027:         *    if and wherever such third-party acknowledgments normally appear.
028:         *
029:         * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro" 
030:         *    must not be used to endorse or promote products derived from this
031:         *    software without prior written permission. For written
032:         *    permission, please contact apache@apache.org.
033:         *
034:         * 5. Products derived from this software may not be called "Apache" 
035:         *    or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their 
036:         *    name, without prior written permission of the Apache Software Foundation.
037:         *
038:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
039:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
040:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
041:         * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
042:         * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
043:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
044:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
045:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
046:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
047:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
048:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049:         * SUCH DAMAGE.
050:         * ====================================================================
051:         *
052:         * This software consists of voluntary contributions made by many
053:         * individuals on behalf of the Apache Software Foundation.  For more
054:         * information on the Apache Software Foundation, please see
055:         * <http://www.apache.org/>.
056:         */
057:
058:        package org.apache.oro.text.awk;
059:
060:        import java.io.*;
061:        import org.apache.oro.text.regex.*;
062:
063:        /**
064:         * The AwkStreamInput class is used to look for pattern matches in an
065:         * input stream (actually a java.io.Reader instance) in conjunction with
066:         * the AwkMatcher class.  It is called
067:         * AwkStreamInput instead of AwkInputStream to stress that it is a form
068:         * of streamed input for the AwkMatcher class to use rather than a subclass of
069:         * InputStream.
070:         * AwkStreamInput performs special internal buffering to accelerate
071:         * pattern searches through a stream.  You can determine the size of this
072:         * buffer and how it grows by using the appropriate constructor.
073:         * <p>
074:         * If you want to perform line by line
075:         * matches on an input stream, you should use a DataInput or BufferedReader
076:         * instance in conjunction
077:         * with one of the PatternMatcher methods taking a String, char[], or
078:         * PatternMatcherInput as an argument.  The DataInput and BufferedReader
079:         * readLine() methods will likely be implemented as native methods and
080:         * therefore more efficient than supporting line by line searching within
081:         * AwkStreamInput.
082:         * <p>
083:         * In the future the programmer will be able to set this class to save
084:         * all the input it sees so that it can be accessed later.  This will avoid
085:         * having to read a stream more than once for whatever reason.
086:         *
087:         * @version @version@
088:         * @since 1.0
089:         * @see AwkMatcher
090:         */
091:        public final class AwkStreamInput {
092:            static final int _DEFAULT_BUFFER_INCREMENT = 2048;
093:            private Reader __searchStream;
094:            private int __bufferIncrementUnit;
095:            boolean _endOfStreamReached;
096:            // The offset into the stream corresponding to buffer[0]
097:            int _bufferSize, _bufferOffset, _currentOffset;
098:            char[] _buffer;
099:
100:            /**
101:             * We use this default contructor only within the package to create a dummy
102:             * AwkStreamInput instance.
103:             */
104:            AwkStreamInput() {
105:                _currentOffset = 0;
106:            }
107:
108:            /**
109:             * Creates an AwkStreamInput instance bound to a Reader with a
110:             * specified initial buffer size and default buffer increment.
111:             * <p>
112:             * @param input  The InputStream to associate with the AwkStreamInput
113:             *        instance.
114:             * @param bufferIncrement  The initial buffer size and the default buffer
115:             *      increment to use when the input buffer has to be increased in
116:             *      size.
117:             */
118:            public AwkStreamInput(Reader input, int bufferIncrement) {
119:                __searchStream = input;
120:                __bufferIncrementUnit = bufferIncrement;
121:                _buffer = new char[bufferIncrement];
122:                _bufferOffset = _bufferSize = _currentOffset = 0;
123:                _endOfStreamReached = false;
124:            }
125:
126:            /**
127:             * Creates an AwkStreamInput instance bound to a Reader with an
128:             * initial buffer size and default buffer increment of 2048 bytes.
129:             * <p>
130:             * @param input  The InputStream to associate with the AwkStreamInput
131:             *        instance.
132:             */
133:            public AwkStreamInput(Reader input) {
134:                this (input, _DEFAULT_BUFFER_INCREMENT);
135:            }
136:
137:            // Only called when buffer overflows
138:            int _reallocate(int initialOffset) throws IOException {
139:                int offset, bytesRead;
140:                char[] tmpBuffer;
141:
142:                if (_endOfStreamReached)
143:                    return _bufferSize;
144:
145:                offset = _bufferSize - initialOffset;
146:                tmpBuffer = new char[offset + __bufferIncrementUnit];
147:
148:                bytesRead = __searchStream.read(tmpBuffer, offset,
149:                        __bufferIncrementUnit);
150:
151:                if (bytesRead <= 0) {
152:                    _endOfStreamReached = true;
153:                    /* bytesRead should never equal zero, but if it does, we don't
154:                    want to continue to try and read, running the risk of entering
155:                    an infinite loop.  Throw an IOException instead, because this
156:                    really IS an exception. */
157:                    if (bytesRead == 0)
158:                        throw new IOException(
159:                                "read from input stream returned 0 bytes.");
160:                    return _bufferSize;
161:                } else {
162:                    _bufferOffset += initialOffset;
163:                    _bufferSize = offset + bytesRead;
164:
165:                    System.arraycopy(_buffer, initialOffset, tmpBuffer, 0,
166:                            offset);
167:                    _buffer = tmpBuffer;
168:                }
169:
170:                return offset;
171:            }
172:
173:            boolean read() throws IOException {
174:                _bufferOffset += _bufferSize;
175:                _bufferSize = __searchStream.read(_buffer);
176:                _endOfStreamReached = (_bufferSize == -1);
177:                return (!_endOfStreamReached);
178:            }
179:
180:            public boolean endOfStream() {
181:                return _endOfStreamReached;
182:            }
183:
184:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.