Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » queryParser » [javadoc | source]
    1   // FastCharStream.java
    2   package org.apache.lucene.queryParser;
    3   
    4   /**
    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
   11    *
   12    *     http://www.apache.org/licenses/LICENSE-2.0
   13    *
   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.
   19    *  
   20    */
   21   
   22   import java.io;
   23   
   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. 
   28    * */
   29   public final class FastCharStream implements CharStream {
   30     char[] buffer = null;
   31   
   32     int bufferLength = 0;				  // end of valid chars
   33     int bufferPosition = 0;			  // next char to read
   34   
   35     int tokenStart = 0;				  // offset in buffer
   36     int bufferStart = 0;				  // position in file of buffer
   37   
   38     Reader input;					  // source of chars
   39   
   40     /** Constructs from a Reader. */
   41     public FastCharStream(Reader r) {
   42       input = r;
   43     }
   44   
   45     public final char readChar() throws IOException {
   46       if (bufferPosition >= bufferLength)
   47         refill();
   48       return buffer[bufferPosition++];
   49     }
   50   
   51     private final void refill() throws IOException {
   52       int newPosition = bufferLength - tokenStart;
   53   
   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);
   60   	buffer = newBuffer;
   61         }
   62       } else {					  // shift token to front
   63         System.arraycopy(buffer, tokenStart, buffer, 0, newPosition);
   64       }
   65   
   66       bufferLength = newPosition;			  // update state
   67       bufferPosition = newPosition;
   68       bufferStart += tokenStart;
   69       tokenStart = 0;
   70   
   71       int charsRead =				  // fill space in buffer
   72         input.read(buffer, newPosition, buffer.length-newPosition);
   73       if (charsRead == -1)
   74         throw new IOException("read past eof");
   75       else
   76         bufferLength += charsRead;
   77     }
   78   
   79     public final char BeginToken() throws IOException {
   80       tokenStart = bufferPosition;
   81       return readChar();
   82     }
   83   
   84     public final void backup(int amount) {
   85       bufferPosition -= amount;
   86     }
   87   
   88     public final String GetImage() {
   89       return new String(buffer, tokenStart, bufferPosition - tokenStart);
   90     }
   91   
   92     public final char[] GetSuffix(int len) {
   93       char[] value = new char[len];
   94       System.arraycopy(buffer, bufferPosition - len, value, 0, len);
   95       return value;
   96     }
   97   
   98     public final void Done() {
   99       try {
  100         input.close();
  101       } catch (IOException e) {
  102         System.err.println("Caught: " + e + "; ignoring.");
  103       }
  104     }
  105   
  106     public final int getColumn() {
  107       return bufferStart + bufferPosition;
  108     }
  109     public final int getLine() {
  110       return 1;
  111     }
  112     public final int getEndColumn() {
  113       return bufferStart + bufferPosition;
  114     }
  115     public final int getEndLine() {
  116       return 1;
  117     }
  118     public final int getBeginColumn() {
  119       return bufferStart + tokenStart;
  120     }
  121     public final int getBeginLine() {
  122       return 1;
  123     }
  124   }

Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » queryParser » [javadoc | source]