cvsimport
[beagle.git] / beagled / Lucene.Net / Store / IndexInput.cs
blob5e45244485303b057590eb2d37b879a27f1cbe59
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
19 namespace Lucene.Net.Store
22 /// <summary>Abstract base class for input from a file in a {@link Directory}. A
23 /// random-access input stream. Used for all Lucene index input operations.
24 /// </summary>
25 /// <seealso cref="Directory">
26 /// </seealso>
27 public abstract class IndexInput : System.ICloneable
29 private char[] chars; // used by readString()
31 /// <summary>Reads and returns a single byte.</summary>
32 /// <seealso cref="IndexOutput.WriteByte(byte)">
33 /// </seealso>
34 public abstract byte ReadByte();
36 /// <summary>Reads a specified number of bytes into an array at the specified offset.</summary>
37 /// <param name="b">the array to read bytes into
38 /// </param>
39 /// <param name="offset">the offset in the array to start storing bytes
40 /// </param>
41 /// <param name="len">the number of bytes to read
42 /// </param>
43 /// <seealso cref="IndexOutput.WriteBytes(byte[],int)">
44 /// </seealso>
45 public abstract void ReadBytes(byte[] b, int offset, int len);
47 /// <summary>Reads four bytes and returns an int.</summary>
48 /// <seealso cref="IndexOutput.WriteInt(int)">
49 /// </seealso>
50 public virtual int ReadInt()
52 return ((ReadByte() & 0xFF) << 24) | ((ReadByte() & 0xFF) << 16) | ((ReadByte() & 0xFF) << 8) | (ReadByte() & 0xFF);
55 /// <summary>Reads an int stored in variable-length format. Reads between one and
56 /// five bytes. Smaller values take fewer bytes. Negative numbers are not
57 /// supported.
58 /// </summary>
59 /// <seealso cref="IndexOutput.WriteVInt(int)">
60 /// </seealso>
61 public virtual int ReadVInt()
63 byte b = ReadByte();
64 int i = b & 0x7F;
65 for (int shift = 7; (b & 0x80) != 0; shift += 7)
67 b = ReadByte();
68 i |= (b & 0x7F) << shift;
70 return i;
73 /// <summary>Reads eight bytes and returns a long.</summary>
74 /// <seealso cref="IndexOutput.WriteLong(long)">
75 /// </seealso>
76 public virtual long ReadLong()
78 return (((long) ReadInt()) << 32) | (ReadInt() & 0xFFFFFFFFL);
81 /// <summary>Reads a long stored in variable-length format. Reads between one and
82 /// nine bytes. Smaller values take fewer bytes. Negative numbers are not
83 /// supported.
84 /// </summary>
85 public virtual long ReadVLong()
87 byte b = ReadByte();
88 long i = b & 0x7F;
89 for (int shift = 7; (b & 0x80) != 0; shift += 7)
91 b = ReadByte();
92 i |= (b & 0x7FL) << shift;
94 return i;
97 /// <summary>Reads a string.</summary>
98 /// <seealso cref="IndexOutput.WriteString(String)">
99 /// </seealso>
100 public virtual System.String ReadString()
102 int length = ReadVInt();
103 if (chars == null || length > chars.Length)
104 chars = new char[length];
105 ReadChars(chars, 0, length);
106 return new System.String(chars, 0, length);
109 /// <summary>Reads UTF-8 encoded characters into an array.</summary>
110 /// <param name="buffer">the array to read characters into
111 /// </param>
112 /// <param name="start">the offset in the array to start storing characters
113 /// </param>
114 /// <param name="length">the number of characters to read
115 /// </param>
116 /// <seealso cref="IndexOutput.WriteChars(String,int,int)">
117 /// </seealso>
118 public virtual void ReadChars(char[] buffer, int start, int length)
120 int end = start + length;
121 for (int i = start; i < end; i++)
123 byte b = ReadByte();
124 if ((b & 0x80) == 0)
125 buffer[i] = (char) (b & 0x7F);
126 else if ((b & 0xE0) != 0xE0)
128 buffer[i] = (char) (((b & 0x1F) << 6) | (ReadByte() & 0x3F));
130 else
131 buffer[i] = (char) (((b & 0x0F) << 12) | ((ReadByte() & 0x3F) << 6) | (ReadByte() & 0x3F));
135 /// <summary>Closes the stream to futher operations. </summary>
136 public abstract void Close();
138 /// <summary>Returns the current position in this file, where the next read will
139 /// occur.
140 /// </summary>
141 /// <seealso cref="Seek(long)">
142 /// </seealso>
143 public abstract long GetFilePointer();
145 /// <summary>Sets current position in this file, where the next read will occur.</summary>
146 /// <seealso cref="GetFilePointer()">
147 /// </seealso>
148 public abstract void Seek(long pos);
150 /// <summary>The number of bytes in the file. </summary>
151 public abstract long Length();
153 /// <summary>Returns a clone of this stream.
154 ///
155 /// <p>Clones of a stream access the same data, and are positioned at the same
156 /// point as the stream they were cloned from.
157 ///
158 /// <p>Expert: Subclasses must ensure that clones may be positioned at
159 /// different points in the input from each other and from the stream they
160 /// were cloned from.
161 /// </summary>
162 public virtual System.Object Clone()
164 IndexInput clone = null;
167 clone = (IndexInput) base.MemberwiseClone();
169 catch (System.Exception)
173 clone.chars = null;
175 return clone;