20 namespace Lucene.Net.Store
28 internal static readonly
int BUFFER_SIZE;
33 private byte[] currentBuffer;
34 private int currentBufferIndex;
36 private int bufferPosition;
37 private long bufferStart;
38 private int bufferLength;
44 if (length / BUFFER_SIZE >= System.Int32.MaxValue)
46 throw new System.IO.IOException(
"Too large RAMFile! " + length);
51 currentBufferIndex = - 1;
55 protected override void Dispose(
bool disposing)
60 public override long Length()
65 public override byte ReadByte()
67 if (bufferPosition >= bufferLength)
70 SwitchCurrentBuffer(
true);
72 return currentBuffer[bufferPosition++];
75 public override void ReadBytes(byte[] b,
int offset,
int len)
79 if (bufferPosition >= bufferLength)
82 SwitchCurrentBuffer(
true);
85 int remainInBuffer = bufferLength - bufferPosition;
86 int bytesToCopy = len < remainInBuffer?len:remainInBuffer;
87 Array.Copy(currentBuffer, bufferPosition, b, offset, bytesToCopy);
88 offset += bytesToCopy;
90 bufferPosition += bytesToCopy;
94 private void SwitchCurrentBuffer(
bool enforceEOF)
96 if (currentBufferIndex >= file.NumBuffers())
100 throw new System.IO.IOException(
"Read past EOF");
104 currentBufferIndex--;
105 bufferPosition = BUFFER_SIZE;
110 currentBuffer = file.GetBuffer(currentBufferIndex);
112 bufferStart = (long) BUFFER_SIZE * (
long) currentBufferIndex;
113 long buflen = length - bufferStart;
114 bufferLength = buflen > BUFFER_SIZE?BUFFER_SIZE:(int) buflen;
118 public override long FilePointer
120 get {
return currentBufferIndex < 0 ? 0 : bufferStart + bufferPosition; }
123 public override void Seek(
long pos)
125 if (currentBuffer == null || pos < bufferStart || pos >= bufferStart + BUFFER_SIZE)
127 currentBufferIndex = (int) (pos / BUFFER_SIZE);
128 SwitchCurrentBuffer(
false);
130 bufferPosition = (int) (pos % BUFFER_SIZE);