2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
25 /// <seealso cref="Directory">
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)">
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
39 /// <param name="offset">the offset in the array to start storing bytes
41 /// <param name="len">the number of bytes to read
43 /// <seealso cref="IndexOutput.WriteBytes(byte[],int)">
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)">
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
59 /// <seealso cref="IndexOutput.WriteVInt(int)">
61 public virtual int ReadVInt()
65 for (int shift
= 7; (b
& 0x80) != 0; shift
+= 7)
68 i
|= (b
& 0x7F) << shift
;
73 /// <summary>Reads eight bytes and returns a long.</summary>
74 /// <seealso cref="IndexOutput.WriteLong(long)">
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
85 public virtual long ReadVLong()
89 for (int shift
= 7; (b
& 0x80) != 0; shift
+= 7)
92 i
|= (b
& 0x7FL
) << shift
;
97 /// <summary>Reads a string.</summary>
98 /// <seealso cref="IndexOutput.WriteString(String)">
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
112 /// <param name="start">the offset in the array to start storing characters
114 /// <param name="length">the number of characters to read
116 /// <seealso cref="IndexOutput.WriteChars(String,int,int)">
118 public virtual void ReadChars(char[] buffer
, int start
, int length
)
120 int end
= start
+ length
;
121 for (int i
= start
; i
< end
; i
++)
125 buffer
[i
] = (char) (b
& 0x7F);
126 else if ((b
& 0xE0) != 0xE0)
128 buffer
[i
] = (char) (((b
& 0x1F) << 6) | (ReadByte() & 0x3F));
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
141 /// <seealso cref="Seek(long)">
143 public abstract long GetFilePointer();
145 /// <summary>Sets current position in this file, where the next read will occur.</summary>
146 /// <seealso cref="GetFilePointer()">
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.
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.
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.
162 public virtual System
.Object
Clone()
164 IndexInput clone
= null;
167 clone
= (IndexInput
) base.MemberwiseClone();
169 catch (System
.Exception
)