20 namespace Lucene.Net.Store
28 internal const int BUFFER_SIZE = 1024;
32 private byte[] currentBuffer;
33 private int currentBufferIndex;
35 private bool isDisposed;
37 private int bufferPosition;
38 private long bufferStart;
39 private int bufferLength;
52 currentBufferIndex = - 1;
60 long end = file.length;
65 int length = BUFFER_SIZE;
66 long nextPos = pos + length;
70 length = (int) (end - pos);
72 out_Renamed.
WriteBytes(file.GetBuffer(buffer++), length);
78 public virtual void Reset()
81 currentBufferIndex = -1;
89 protected override void Dispose(
bool disposing)
91 if (isDisposed)
return;
101 public override void Seek(
long pos)
106 if (pos < bufferStart || pos >= bufferStart + bufferLength)
108 currentBufferIndex = (int) (pos / BUFFER_SIZE);
109 SwitchCurrentBuffer();
112 bufferPosition = (int) (pos % BUFFER_SIZE);
115 public override long Length
117 get {
return file.length; }
120 public override void WriteByte(byte b)
122 if (bufferPosition == bufferLength)
124 currentBufferIndex++;
125 SwitchCurrentBuffer();
127 currentBuffer[bufferPosition++] = b;
130 public override void WriteBytes(byte[] b,
int offset,
int len)
132 System.Diagnostics.Debug.Assert(b != null);
135 if (bufferPosition == bufferLength)
137 currentBufferIndex++;
138 SwitchCurrentBuffer();
141 int remainInBuffer = currentBuffer.Length - bufferPosition;
142 int bytesToCopy = len < remainInBuffer?len:remainInBuffer;
143 Array.Copy(b, offset, currentBuffer, bufferPosition, bytesToCopy);
144 offset += bytesToCopy;
146 bufferPosition += bytesToCopy;
150 private void SwitchCurrentBuffer()
152 if (currentBufferIndex == file.NumBuffers())
154 currentBuffer = file.AddBuffer(BUFFER_SIZE);
158 currentBuffer = file.GetBuffer(currentBufferIndex);
161 bufferStart = (long) BUFFER_SIZE * (
long) currentBufferIndex;
162 bufferLength = currentBuffer.Length;
165 private void SetFileLength()
167 long pointer = bufferStart + bufferPosition;
168 if (pointer > file.length)
170 file.Length = pointer;
174 public override void Flush()
176 file.LastModified = (DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond);
180 public override long FilePointer
182 get {
return currentBufferIndex < 0 ? 0 : bufferStart + bufferPosition; }
186 public virtual long SizeInBytes()
188 return file.NumBuffers() * BUFFER_SIZE;