Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
IndexInput.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.Store
22 {
23 
29  public abstract class IndexInput : System.ICloneable, IDisposable
30  {
31  private bool preUTF8Strings; // true if we are reading old (modified UTF8) string format
32 
36  public abstract byte ReadByte();
37 
47  public abstract void ReadBytes(byte[] b, int offset, int len);
48 
66  public virtual void ReadBytes(byte[] b, int offset, int len, bool useBuffer)
67  {
68  // Default to ignoring useBuffer entirely
69  ReadBytes(b, offset, len);
70  }
71 
75  public virtual int ReadInt()
76  {
77  return ((ReadByte() & 0xFF) << 24) | ((ReadByte() & 0xFF) << 16) | ((ReadByte() & 0xFF) << 8) | (ReadByte() & 0xFF);
78  }
79 
86  public virtual int ReadVInt()
87  {
88  byte b = ReadByte();
89  int i = b & 0x7F;
90  for (int shift = 7; (b & 0x80) != 0; shift += 7)
91  {
92  b = ReadByte();
93  i |= (b & 0x7F) << shift;
94  }
95  return i;
96  }
97 
101  public virtual long ReadLong()
102  {
103  return (((long) ReadInt()) << 32) | (ReadInt() & 0xFFFFFFFFL);
104  }
105 
110  public virtual long ReadVLong()
111  {
112  byte b = ReadByte();
113  long i = b & 0x7F;
114  for (int shift = 7; (b & 0x80) != 0; shift += 7)
115  {
116  b = ReadByte();
117  i |= (b & 0x7FL) << shift;
118  }
119  return i;
120  }
121 
127  public virtual void SetModifiedUTF8StringsMode()
128  {
129  preUTF8Strings = true;
130  }
131 
135  public virtual System.String ReadString()
136  {
137  if (preUTF8Strings)
138  return ReadModifiedUTF8String();
139  int length = ReadVInt();
140  byte[] bytes = new byte[length];
141  ReadBytes(bytes, 0, length);
142  return System.Text.Encoding.UTF8.GetString(bytes, 0, length);
143  }
144 
145  private System.String ReadModifiedUTF8String()
146  {
147  int length = ReadVInt();
148  char[] chars = new char[length];
149  ReadChars(chars, 0, length);
150  return new System.String(chars, 0, length);
151  }
152 
168  [Obsolete("-- please use ReadString or ReadBytes instead, and construct the string from those utf8 bytes")]
169  public virtual void ReadChars(char[] buffer, int start, int length)
170  {
171  int end = start + length;
172  for (int i = start; i < end; i++)
173  {
174  byte b = ReadByte();
175  if ((b & 0x80) == 0)
176  buffer[i] = (char) (b & 0x7F);
177  else if ((b & 0xE0) != 0xE0)
178  {
179  buffer[i] = (char) (((b & 0x1F) << 6) | (ReadByte() & 0x3F));
180  }
181  else
182  buffer[i] = (char) (((b & 0x0F) << 12) | ((ReadByte() & 0x3F) << 6) | (ReadByte() & 0x3F));
183  }
184  }
185 
198  [Obsolete("this method operates on old \"modified utf8\" encoded strings")]
199  public virtual void SkipChars(int length)
200  {
201  for (int i = 0; i < length; i++)
202  {
203  byte b = ReadByte();
204  if ((b & 0x80) == 0)
205  {
206  //do nothing, we only need one byte
207  }
208  else if ((b & 0xE0) != 0xE0)
209  {
210  ReadByte(); //read an additional byte
211  }
212  else
213  {
214  //read two additional bytes.
215  ReadByte();
216  ReadByte();
217  }
218  }
219  }
220 
221  [Obsolete("Use Dispose() instead.")]
222  public void Close()
223  {
224  Dispose();
225  }
226 
228  public void Dispose()
229  {
230  Dispose(true);
231  }
232 
233  protected abstract void Dispose(bool disposing);
234 
240  public abstract long FilePointer { get; }
241 
245  public abstract void Seek(long pos);
246 
248  public abstract long Length();
249 
259  public virtual System.Object Clone()
260  {
261  IndexInput clone = null;
262  try
263  {
264  clone = (IndexInput) base.MemberwiseClone();
265  }
266  catch (System.Exception)
267  {
268  }
269 
270  return clone;
271  }
272 
273  // returns Map<String, String>
274  public virtual System.Collections.Generic.IDictionary<string,string> ReadStringStringMap()
275  {
276  var map = new HashMap<string, string>();
277  int count = ReadInt();
278  for (int i = 0; i < count; i++)
279  {
280  System.String key = ReadString();
281  System.String val = ReadString();
282  map[key] = val;
283  }
284 
285  return map;
286  }
287 
288  /*public abstract void Dispose();*/
289  }
290 }