2 package org.apache.lucene.queryParser;
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 /** An efficient implementation of JavaCC's CharStream interface. <p>Note that
25 * this does not do line-number counting, but instead keeps track of the
26 * character position of the token in the input, as required by Lucene's {@link
27 * org.apache.lucene.analysis.Token} API.
29 public final class FastCharStream implements CharStream {
32 int bufferLength = 0; // end of valid chars
33 int bufferPosition = 0; // next char to read
35 int tokenStart = 0; // offset in buffer
36 int bufferStart = 0; // position in file of buffer
38 Reader input; // source of chars
40 /** Constructs from a Reader. */
41 public FastCharStream(Reader r) {
45 public final char readChar() throws IOException {
46 if (bufferPosition >= bufferLength)
48 return buffer[bufferPosition++];
51 private final void refill() throws IOException {
52 int newPosition = bufferLength - tokenStart;
54 if (tokenStart == 0) { // token won't fit in buffer
55 if (buffer == null) { // first time: alloc buffer
56 buffer = new char[2048];
57 } else if (bufferLength == buffer.length) { // grow buffer
58 char[] newBuffer = new char[buffer.length*2];
59 System.arraycopy(buffer, 0, newBuffer, 0, bufferLength);
62 } else { // shift token to front
63 System.arraycopy(buffer, tokenStart, buffer, 0, newPosition);
66 bufferLength = newPosition; // update state
67 bufferPosition = newPosition;
68 bufferStart += tokenStart;
71 int charsRead = // fill space in buffer
72 input.read(buffer, newPosition, buffer.length-newPosition);
74 throw new IOException("read past eof");
76 bufferLength += charsRead;
79 public final char BeginToken() throws IOException {
80 tokenStart = bufferPosition;
84 public final void backup(int amount) {
85 bufferPosition -= amount;
88 public final String GetImage() {
89 return new String(buffer, tokenStart, bufferPosition - tokenStart);
92 public final char[] GetSuffix(int len) {
93 char[] value = new char[len];
94 System.arraycopy(buffer, bufferPosition - len, value, 0, len);
98 public final void Done() {
101 } catch (IOException e) {
102 System.err.println("Caught: " + e + "; ignoring.");
106 public final int getColumn() {
107 return bufferStart + bufferPosition;
109 public final int getLine() {
112 public final int getEndColumn() {
113 return bufferStart + bufferPosition;
115 public final int getEndLine() {
118 public final int getBeginColumn() {
119 return bufferStart + tokenStart;
121 public final int getBeginLine() {