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
CharReader.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> CharReader is a Reader wrapper. It reads chars from
22  /// Reader and outputs <see cref="CharStream" />, defining an
23  /// identify function <see cref="CorrectOffset" /> method that
24  /// simply returns the provided offset.
25  /// </summary>
26  public sealed class CharReader:CharStream
27  {
28  private long currentPosition = -1;
29 
30  private bool isDisposed;
31 
32  internal System.IO.StreamReader input;
33 
34  public static CharStream Get(System.IO.TextReader input)
35  {
36  var charStream = input as CharStream;
37  if (charStream != null)
38  return charStream;
39 
40  // {{Aroush-2.9}} isn't there a better (faster) way to do this?
41  var theString = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(input.ReadToEnd()));
42  return new CharReader(new System.IO.StreamReader(theString));
43  //return input is CharStream?(CharStream) input:new CharReader(input);
44  }
45 
46  private CharReader(System.IO.StreamReader in_Renamed) : base(in_Renamed)
47  {
48  input = in_Renamed;
49  }
50 
51  public override int CorrectOffset(int currentOff)
52  {
53  return currentOff;
54  }
55 
56  protected override void Dispose(bool disposing)
57  {
58  if (isDisposed) return;
59 
60  if (disposing)
61  {
62  if (input != null)
63  {
64  input.Close();
65  }
66  }
67 
68  input = null;
69  isDisposed = true;
70  base.Dispose(disposing);
71  }
72 
73  public override int Read(System.Char[] cbuf, int off, int len)
74  {
75  return input.Read(cbuf, off, len);
76  }
77 
78  public bool MarkSupported()
79  {
80  return input.BaseStream.CanSeek;
81  }
82 
83  public void Mark(int readAheadLimit)
84  {
85  currentPosition = input.BaseStream.Position;
86  input.BaseStream.Position = readAheadLimit;
87  }
88 
89  public void Reset()
90  {
91  input.BaseStream.Position = currentPosition;
92  }
93  }
94 }