KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > impl > lucene > FastCharStream


1 // FastCharStream.java
2
package org.alfresco.repo.search.impl.lucene;
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.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22
23 /** An efficient implementation of JavaCC's CharStream interface. <p>Note that
24  * this does not do line-number counting, but instead keeps track of the
25  * character position of the token in the input, as required by Lucene's {@link
26  * org.apache.lucene.analysis.Token} API. */

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

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

36   Reader JavaDoc input; // source of chars
37

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