Add --enable-deletion option to buildindex. If used, buildindex will remove deleted...
[beagle.git] / beagled / Lucene.Net / Store / BufferedIndexInput.cs
blob5f2b899baf72d2f2558bf5780a19a621f27c6903
1 /*
2 * Copyright 2005 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.
16 using System;
17 namespace Lucene.Net.Store
20 /// <summary>Base implementation class for buffered {@link IndexInput}. </summary>
21 public abstract class BufferedIndexInput : IndexInput, System.ICloneable
23 internal static readonly int BUFFER_SIZE;
25 private byte[] buffer;
27 private long bufferStart = 0; // position in file of buffer
28 private int bufferLength = 0; // end of valid bytes
29 private int bufferPosition = 0; // next byte to read
31 public override byte ReadByte()
33 if (bufferPosition >= bufferLength)
34 Refill();
35 return buffer[bufferPosition++];
38 public override void ReadBytes(byte[] b, int offset, int len)
40 if (len < BUFFER_SIZE)
42 for (int i = 0; i < len; i++)
43 // read byte-by-byte
44 b[i + offset] = (byte) ReadByte();
46 else
48 // read all-at-once
49 long start = GetFilePointer();
50 SeekInternal(start);
51 ReadInternal(b, offset, len);
53 bufferStart = start + len; // adjust stream variables
54 bufferPosition = 0;
55 bufferLength = 0; // trigger refill() on read
59 private void Refill()
61 long start = bufferStart + bufferPosition;
62 long end = start + BUFFER_SIZE;
63 if (end > Length())
64 // don't read past EOF
65 end = Length();
66 bufferLength = (int) (end - start);
67 if (bufferLength <= 0)
68 throw new System.IO.IOException("read past EOF");
70 if (buffer == null)
71 buffer = new byte[BUFFER_SIZE]; // allocate buffer lazily
72 ReadInternal(buffer, 0, bufferLength);
74 bufferStart = start;
75 bufferPosition = 0;
78 /// <summary>Expert: implements buffer refill. Reads bytes from the current position
79 /// in the input.
80 /// </summary>
81 /// <param name="b">the array to read bytes into
82 /// </param>
83 /// <param name="offset">the offset in the array to start storing bytes
84 /// </param>
85 /// <param name="length">the number of bytes to read
86 /// </param>
87 public abstract void ReadInternal(byte[] b, int offset, int length);
89 public override long GetFilePointer()
91 return bufferStart + bufferPosition;
94 public override void Seek(long pos)
96 if (pos >= bufferStart && pos < (bufferStart + bufferLength))
97 bufferPosition = (int) (pos - bufferStart);
98 // seek within buffer
99 else
101 bufferStart = pos;
102 bufferPosition = 0;
103 bufferLength = 0; // trigger refill() on read()
104 SeekInternal(pos);
108 /// <summary>Expert: implements seek. Sets current position in this file, where the
109 /// next {@link #ReadInternal(byte[],int,int)} will occur.
110 /// </summary>
111 /// <seealso cref="#ReadInternal(byte[],int,int)">
112 /// </seealso>
113 public abstract void SeekInternal(long pos);
115 public override System.Object Clone()
117 BufferedIndexInput clone = (BufferedIndexInput) base.Clone();
119 if (buffer != null)
121 clone.buffer = new byte[BUFFER_SIZE];
122 Array.Copy(buffer, 0, clone.buffer, 0, bufferLength);
125 return clone;
128 static BufferedIndexInput()
130 BUFFER_SIZE = BufferedIndexOutput.BUFFER_SIZE;