* KNotesQueryable.cs: Dont re-index all the notes when the notes file changes. Since...
[beagle.git] / beagled / Lucene.Net / QueryParser / FastCharStream.cs
blobb85b32c62908e7116d4a13da894c8c8010c2d807
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
19 namespace Lucene.Net.QueryParsers
22 /// <summary>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 /// Lucene.Net.analysis.Token} API.
26 /// </summary>
27 public sealed class FastCharStream : CharStream
29 internal char[] buffer = null;
31 internal int bufferLength = 0; // end of valid chars
32 internal int bufferPosition = 0; // next char to read
34 internal int tokenStart = 0; // offset in buffer
35 internal int bufferStart = 0; // position in file of buffer
37 internal System.IO.TextReader input; // source of chars
39 /// <summary>Constructs from a Reader. </summary>
40 public FastCharStream(System.IO.TextReader r)
42 input = r;
45 public char ReadChar()
47 if (bufferPosition >= bufferLength)
48 Refill();
49 return buffer[bufferPosition++];
52 private void Refill()
54 int newPosition = bufferLength - tokenStart;
56 if (tokenStart == 0)
58 // token won't fit in buffer
59 if (buffer == null)
61 // first time: alloc buffer
62 buffer = new char[2048];
64 else if (bufferLength == buffer.Length)
66 // grow buffer
67 char[] newBuffer = new char[buffer.Length * 2];
68 Array.Copy(buffer, 0, newBuffer, 0, bufferLength);
69 buffer = newBuffer;
72 else
74 // shift token to front
75 Array.Copy(buffer, tokenStart, buffer, 0, newPosition);
78 bufferLength = newPosition; // update state
79 bufferPosition = newPosition;
80 bufferStart += tokenStart;
81 tokenStart = 0;
83 int charsRead = 0;
85 try
87 charsRead = input.Read(buffer, newPosition, buffer.Length - newPosition);
89 catch
93 if (charsRead <= 0)
94 throw new System.IO.IOException("read past eof");
95 else
96 bufferLength += charsRead;
99 public char BeginToken()
101 tokenStart = bufferPosition;
102 return ReadChar();
105 public void Backup(int amount)
107 bufferPosition -= amount;
110 public System.String GetImage()
112 return new System.String(buffer, tokenStart, bufferPosition - tokenStart);
115 public char[] GetSuffix(int len)
117 char[] value_Renamed = new char[len];
118 Array.Copy(buffer, bufferPosition - len, value_Renamed, 0, len);
119 return value_Renamed;
122 public void Done()
126 input.Close();
128 catch (System.IO.IOException e)
130 System.Console.Error.WriteLine("Caught: " + e + "; ignoring.");
134 public int GetColumn()
136 return bufferStart + bufferPosition;
138 public int GetLine()
140 return 1;
142 public int GetEndColumn()
144 return bufferStart + bufferPosition;
146 public int GetEndLine()
148 return 1;
150 public int GetBeginColumn()
152 return bufferStart + tokenStart;
154 public int GetBeginLine()
156 return 1;