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
CharFilter.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 namespace Lucene.Net.Analysis
19 {
20 
21  /// <summary> Subclasses of CharFilter can be chained to filter CharStream.
22  /// They can be used as <see cref="System.IO.TextReader" /> with additional offset
23  /// correction. <see cref="Tokenizer" />s will automatically use <see cref="CorrectOffset" />
24  /// if a CharFilter/CharStream subclass is used.
25  ///
26  /// </summary>
27  /// <version> $Id$
28  ///
29  /// </version>
30  public abstract class CharFilter : CharStream
31  {
32  private long currentPosition = -1;
33  private bool isDisposed;
34  protected internal CharStream input;
35 
36  protected internal CharFilter(CharStream in_Renamed) : base(in_Renamed)
37  {
38  input = in_Renamed;
39  }
40 
41  /// <summary>Subclass may want to override to correct the current offset.</summary>
42  /// <param name="currentOff">current offset</param>
43  /// <returns>corrected offset</returns>
44  protected internal virtual int Correct(int currentOff)
45  {
46  return currentOff;
47  }
48 
49  /// <summary> Chains the corrected offset through the input
50  /// CharFilter.
51  /// </summary>
52  public override int CorrectOffset(int currentOff)
53  {
54  return input.CorrectOffset(Correct(currentOff));
55  }
56 
57  protected override void Dispose(bool disposing)
58  {
59  if (isDisposed) return;
60 
61  if (disposing)
62  {
63  if (input != null)
64  {
65  input.Close();
66  }
67  }
68 
69  input = null;
70  isDisposed = true;
71  base.Dispose(disposing);
72  }
73 
74  public override int Read(System.Char[] cbuf, int off, int len)
75  {
76  return input.Read(cbuf, off, len);
77  }
78 
79  public bool MarkSupported()
80  {
81  return input.BaseStream.CanSeek;
82  }
83 
84  public void Mark(int readAheadLimit)
85  {
86  currentPosition = input.BaseStream.Position;
87  input.BaseStream.Position = readAheadLimit;
88  }
89 
90  public void Reset()
91  {
92  input.BaseStream.Position = currentPosition;
93  }
94  }
95 }