2 package org.apache.lucene.queryParser.surround.parser;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 {
29 int bufferLength = 0; // end of valid chars
30 int bufferPosition = 0; // next char to read
32 int tokenStart = 0; // offset in buffer
33 int bufferStart = 0; // position in file of buffer
35 Reader input; // source of chars
37 /** Constructs from a Reader. */
38 public FastCharStream(Reader r) {
42 public final char readChar() throws IOException {
43 if (bufferPosition >= bufferLength)
45 return buffer[bufferPosition++];
48 private final void refill() throws IOException {
49 int newPosition = bufferLength - tokenStart;
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);
59 } else { // shift token to front
60 System.arraycopy(buffer, tokenStart, buffer, 0, newPosition);
63 bufferLength = newPosition; // update state
64 bufferPosition = newPosition;
65 bufferStart += tokenStart;
68 int charsRead = // fill space in buffer
69 input.read(buffer, newPosition, buffer.length-newPosition);
71 throw new IOException("read past eof");
73 bufferLength += charsRead;
76 public final char BeginToken() throws IOException {
77 tokenStart = bufferPosition;
81 public final void backup(int amount) {
82 bufferPosition -= amount;
85 public final String GetImage() {
86 return new String(buffer, tokenStart, bufferPosition - tokenStart);
89 public final char[] GetSuffix(int len) {
90 char[] value = new char[len];
91 System.arraycopy(buffer, bufferPosition - len, value, 0, len);
95 public final void Done() {
98 } catch (IOException e) {
99 System.err.println("Caught: " + e + "; ignoring.");
103 public final int getColumn() {
104 return bufferStart + bufferPosition;
106 public final int getLine() {
109 public final int getEndColumn() {
110 return bufferStart + bufferPosition;
112 public final int getEndLine() {
115 public final int getBeginColumn() {
116 return bufferStart + tokenStart;
118 public final int getBeginLine() {