Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
FastCharStream.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 // FastCharStream.java
19 
20 using System;
21 
22 namespace Lucene.Net.QueryParsers
23 {
24 
25  /// <summary>An efficient implementation of JavaCC's CharStream interface. <p/>Note that
26  /// this does not do line-number counting, but instead keeps track of the
27  /// character position of the token in the input, as required by Lucene's <see cref="Lucene.Net.Analysis.Token" />
28  /// API.
29  ///
30  /// </summary>
31  public sealed class FastCharStream : ICharStream
32  {
33  internal char[] buffer = null;
34 
35  internal int bufferLength = 0; // end of valid chars
36  internal int bufferPosition = 0; // next char to read
37 
38  internal int tokenStart = 0; // offset in buffer
39  internal int bufferStart = 0; // position in file of buffer
40 
41  internal System.IO.TextReader input; // source of chars
42 
43  /// <summary>Constructs from a Reader. </summary>
44  public FastCharStream(System.IO.TextReader r)
45  {
46  input = r;
47  }
48 
49  public char ReadChar()
50  {
51  if (bufferPosition >= bufferLength)
52  Refill();
53  return buffer[bufferPosition++];
54  }
55 
56  private void Refill()
57  {
58  int newPosition = bufferLength - tokenStart;
59 
60  if (tokenStart == 0)
61  {
62  // token won't fit in buffer
63  if (buffer == null)
64  {
65  // first time: alloc buffer
66  buffer = new char[2048];
67  }
68  else if (bufferLength == buffer.Length)
69  {
70  // grow buffer
71  char[] newBuffer = new char[buffer.Length * 2];
72  Array.Copy(buffer, 0, newBuffer, 0, bufferLength);
73  buffer = newBuffer;
74  }
75  }
76  else
77  {
78  // shift token to front
79  Array.Copy(buffer, tokenStart, buffer, 0, newPosition);
80  }
81 
82  bufferLength = newPosition; // update state
83  bufferPosition = newPosition;
84  bufferStart += tokenStart;
85  tokenStart = 0;
86 
87  int charsRead = input.Read(buffer, newPosition, buffer.Length - newPosition);
88  if (charsRead <= 0)
89  throw new System.IO.IOException("read past eof");
90  else
91  bufferLength += charsRead;
92  }
93 
94  public char BeginToken()
95  {
96  tokenStart = bufferPosition;
97  return ReadChar();
98  }
99 
100  public void Backup(int amount)
101  {
102  bufferPosition -= amount;
103  }
104 
105  public string Image
106  {
107  get { return new System.String(buffer, tokenStart, bufferPosition - tokenStart); }
108  }
109 
110  public char[] GetSuffix(int len)
111  {
112  char[] value_Renamed = new char[len];
113  Array.Copy(buffer, bufferPosition - len, value_Renamed, 0, len);
114  return value_Renamed;
115  }
116 
117  public void Done()
118  {
119  try
120  {
121  input.Close();
122  }
123  catch (System.IO.IOException e)
124  {
125  System.Console.Error.WriteLine("Caught: " + e + "; ignoring.");
126  }
127  }
128 
129  public int Column
130  {
131  get { return bufferStart + bufferPosition; }
132  }
133 
134  public int Line
135  {
136  get { return 1; }
137  }
138 
139  public int EndColumn
140  {
141  get { return bufferStart + bufferPosition; }
142  }
143 
144  public int EndLine
145  {
146  get { return 1; }
147  }
148 
149  public int BeginColumn
150  {
151  get { return bufferStart + tokenStart; }
152  }
153 
154  public int BeginLine
155  {
156  get { return 1; }
157  }
158  }
159 }