KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > oro > text > awk > AwkStreamInput


1 package org.apache.oro.text.awk;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2000 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
29  * must not be used to endorse or promote products derived from this
30  * software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
35  * name, without prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  *
56  * Portions of this software are based upon software originally written
57  * by Daniel F. Savarese. We appreciate his contributions.
58  */

59
60 import java.io.*;
61 import org.apache.oro.text.regex.*;
62
63 /**
64  * The AwkStreamInput class is used to look for pattern matches in an
65  * input stream (actually a java.io.Reader instance) in conjunction with
66  * the AwkMatcher class. It is called
67  * AwkStreamInput instead of AwkInputStream to stress that it is a form
68  * of streamed input for the AwkMatcher class to use rather than a subclass of
69  * InputStream.
70  * AwkStreamInput performs special internal buffering to accelerate
71  * pattern searches through a stream. You can determine the size of this
72  * buffer and how it grows by using the appropriate constructor.
73  * <p>
74  * If you want to perform line by line
75  * matches on an input stream, you should use a DataInput or BufferedReader
76  * instance in conjunction
77  * with one of the PatternMatcher methods taking a String, char[], or
78  * PatternMatcherInput as an argument. The DataInput and BufferedReader
79  * readLine() methods will likely be implemented as native methods and
80  * therefore more efficient than supporting line by line searching within
81  * AwkStreamInput.
82  * <p>
83  * In the future the programmer will be able to set this class to save
84  * all the input it sees so that it can be accessed later. This will avoid
85  * having to read a stream more than once for whatever reason.
86
87  @author <a HREF="dfs@savarese.org">Daniel F. Savarese</a>
88  @version $Id: AwkStreamInput.java,v 1.1.1.1 2000/07/23 23:08:50 jon Exp $
89
90  * @see AwkMatcher
91  */

92 public final class AwkStreamInput {
93   static final int _DEFAULT_BUFFER_INCREMENT = 2048;
94   private Reader __searchStream;
95   private int __bufferIncrementUnit;
96   boolean _endOfStreamReached;
97   // The offset into the stream corresponding to buffer[0]
98
int _bufferSize, _bufferOffset, _currentOffset;
99   char[] _buffer;
100
101   /**
102    * We use this default contructor only within the package to create a dummy
103    * AwkStreamInput instance.
104    */

105   AwkStreamInput() {
106     _currentOffset = 0;
107   }
108
109
110   /**
111    * Creates an AwkStreamInput instance bound to a Reader with a
112    * specified initial buffer size and default buffer increment.
113    * <p>
114    * @param input The InputStream to associate with the AwkStreamInput
115    * instance.
116    * @param bufferIncrement The initial buffer size and the default buffer
117    * increment to use when the input buffer has to be increased in
118    * size.
119    */

120   public AwkStreamInput(Reader input, int bufferIncrement) {
121     __searchStream = input;
122     __bufferIncrementUnit = bufferIncrement;
123     _buffer = new char[bufferIncrement];
124     _bufferOffset = _bufferSize = _currentOffset = 0;
125     _endOfStreamReached = false;
126   }
127
128
129   /**
130    * Creates an AwkStreamInput instance bound to a Reader with an
131    * initial buffer size and default buffer increment of 2048 bytes.
132    * <p>
133    * @param input The InputStream to associate with the AwkStreamInput
134    * instance.
135    */

136   public AwkStreamInput(Reader input) {
137     this(input, _DEFAULT_BUFFER_INCREMENT);
138   }
139
140   // Only called when buffer overflows
141
int _reallocate(int initialOffset) throws IOException {
142     int offset, bytesRead;
143     char[] tmpBuffer;
144
145     if(_endOfStreamReached)
146       return _bufferSize;
147
148     offset = _bufferSize - initialOffset;
149     tmpBuffer = new char[offset + __bufferIncrementUnit];
150
151     bytesRead =
152       __searchStream.read(tmpBuffer, offset, __bufferIncrementUnit);
153
154     if(bytesRead <= 0){
155       _endOfStreamReached = true;
156       /* bytesRead should never equal zero, but if it does, we don't
157      want to continue to try and read, running the risk of entering
158      an infinite loop. Throw an IOException instead, because this
159      really IS an exception. */

160       if(bytesRead == 0)
161     throw new IOException("read from input stream returned 0 bytes.");
162       return _bufferSize;
163     } else {
164       _bufferOffset += initialOffset;
165       _bufferSize = offset + bytesRead;
166
167       System.arraycopy(_buffer, initialOffset, tmpBuffer, 0, offset);
168       _buffer = tmpBuffer;
169     }
170
171     return offset;
172   }
173
174   boolean read() throws IOException {
175     _bufferOffset+=_bufferSize;
176     _bufferSize = __searchStream.read(_buffer);
177     _endOfStreamReached = (_bufferSize == -1);
178     return (!_endOfStreamReached);
179   }
180
181   public boolean endOfStream() { return _endOfStreamReached; }
182
183 }
184
Popular Tags