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: }
|