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
ReusableStringReader.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 using System;
19 using Lucene.Net.Support;
20 
21 namespace Lucene.Net.Index
22 {
23 
24  /// <summary>Used by DocumentsWriter to implemented a StringReader
25  /// that can be reset to a new string; we use this when
26  /// tokenizing the string value from a Field.
27  /// </summary>
28  sealed class ReusableStringReader : System.IO.TextReader
29  {
30  internal int upto;
31  internal int left;
32  internal System.String s;
33  internal void Init(System.String s)
34  {
35  this.s = s;
36  left = s.Length;
37  this.upto = 0;
38  }
39 
40  public int Read(char[] c)
41  {
42  return Read(c, 0, c.Length);
43  }
44 
45  public override int Read(System.Char[] c, int off, int len)
46  {
47  if (left > len)
48  {
49  TextSupport.GetCharsFromString(s, upto, upto + len, c, off);
50  upto += len;
51  left -= len;
52  return len;
53  }
54  else if (0 == left)
55  {
56  // don't keep a reference (s could have been very large)
57  s = null;
58  return 0;
59  }
60  else
61  {
62  TextSupport.GetCharsFromString(s, upto, upto + left, c, off);
63  int r = left;
64  left = 0;
65  upto = s.Length;
66  return r;
67  }
68  }
69 
70  //[Obsolete("Use Dispose() instead")]
71  public override void Close()
72  {
73  Dispose();
74  }
75 
76  public override int Read()
77  {
78  if (left > 0)
79  {
80  char ch = s[upto];
81  upto += 1;
82  left -= 1;
83  return (int)ch;
84  }
85  return -1;
86  }
87 
88  public override int ReadBlock(char[] buffer, int index, int count)
89  {
90  return Read(buffer, index, count);
91  }
92 
93  public override string ReadLine()
94  {
95  int i;
96  for (i = upto; i < s.Length; i++)
97  {
98  char c = s[i];
99  if (c == '\r' || c == '\n')
100  {
101  string result = s.Substring(upto, i - upto);
102  upto = i + 1;
103  left = s.Length - upto;
104  if (c == '\r' && upto < s.Length && s[upto] == '\n')
105  {
106  upto++;
107  left--;
108  }
109  return result;
110  }
111  }
112  if (i > upto)
113  {
114  return ReadToEnd();
115  }
116  return null;
117  }
118 
119  public override int Peek()
120  {
121  if (left > 0)
122  {
123  return (int)s[upto];
124  }
125  return -1;
126  }
127 
128  public override string ReadToEnd()
129  {
130  string result = s.Substring(upto, left);
131  left = 0;
132  upto = s.Length - 1;
133  return result;
134  }
135  }
136 }