KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > queryParser > FastCharStream


1 // FastCharStream.java
2
package org.apache.lucene.queryParser;
3
4 /**
5  * Copyright 2004 The Apache Software Foundation
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 import java.io.*;
21
22 /** An efficient implementation of JavaCC's CharStream interface. <p>Note that
23  * this does not do line-number counting, but instead keeps track of the
24  * character position of the token in the input, as required by Lucene's {@link
25  * org.apache.lucene.analysis.Token} API. */

26 public final class FastCharStream implements CharStream {
27   char[] buffer = null;
28
29   int bufferLength = 0; // end of valid chars
30
int bufferPosition = 0; // next char to read
31

32   int tokenStart = 0; // offset in buffer
33
int bufferStart = 0; // position in file of buffer
34

35   Reader input; // source of chars
36

37   /** Constructs from a Reader. */
38   public FastCharStream(Reader r) {
39     input = r;
40   }
41
42   public final char readChar() throws IOException {
43     if (bufferPosition >= bufferLength)
44       refill();
45     return buffer[bufferPosition++];
46   }
47
48   private final void refill() throws IOException {
49     int newPosition = bufferLength - tokenStart;
50
51     if (tokenStart == 0) { // token won't fit in buffer
52
if (buffer == null) { // first time: alloc buffer
53
buffer = new char[2048];
54       } else if (bufferLength == buffer.length) { // grow buffer
55
char[] newBuffer = new char[buffer.length*2];
56     System.arraycopy(buffer, 0, newBuffer, 0, bufferLength);
57     buffer = newBuffer;
58       }
59     } else { // shift token to front
60
System.arraycopy(buffer, tokenStart, buffer, 0, newPosition);
61     }
62
63     bufferLength = newPosition; // update state
64
bufferPosition = newPosition;
65     bufferStart += tokenStart;
66     tokenStart = 0;
67
68     int charsRead = // fill space in buffer
69
input.read(buffer, newPosition, buffer.length-newPosition);
70     if (charsRead == -1)
71       throw new IOException("read past eof");
72     else
73       bufferLength += charsRead;
74   }
75
76   public final char BeginToken() throws IOException {
77     tokenStart = bufferPosition;
78     return readChar();
79   }
80
81   public final void backup(int amount) {
82     bufferPosition -= amount;
83   }
84
85   public final String JavaDoc GetImage() {
86     return new String JavaDoc(buffer, tokenStart, bufferPosition - tokenStart);
87   }
88
89   public final char[] GetSuffix(int len) {
90     char[] value = new char[len];
91     System.arraycopy(buffer, bufferPosition - len, value, 0, len);
92     return value;
93   }
94
95   public final void Done() {
96     try {
97       input.close();
98     } catch (IOException e) {
99       System.err.println("Caught: " + e + "; ignoring.");
100     }
101   }
102
103   public final int getColumn() {
104     return bufferStart + bufferPosition;
105   }
106   public final int getLine() {
107     return 1;
108   }
109   public final int getEndColumn() {
110     return bufferStart + bufferPosition;
111   }
112   public final int getEndLine() {
113     return 1;
114   }
115   public final int getBeginColumn() {
116     return bufferStart + tokenStart;
117   }
118   public final int getBeginLine() {
119     return 1;
120   }
121 }
122
Popular Tags