20 namespace Lucene.Net.Store
28 public const int BUFFER_SIZE = 1024;
30 private int _bufferSize = BUFFER_SIZE;
32 protected internal byte[] buffer;
34 private long bufferStart = 0;
35 private int bufferLength = 0;
36 private int bufferPosition = 0;
38 public override byte ReadByte()
40 if (bufferPosition >= bufferLength)
42 return buffer[bufferPosition++];
52 CheckBufferSize(bufferSize);
53 this._bufferSize = bufferSize;
57 public virtual void SetBufferSize(
int newSize)
59 System.Diagnostics.Debug.Assert(buffer == null || _bufferSize == buffer.Length,
"buffer=" + buffer +
" bufferSize=" + _bufferSize +
" buffer.length=" +(buffer != null ? buffer.Length: 0));
60 if (newSize != _bufferSize)
62 CheckBufferSize(newSize);
63 _bufferSize = newSize;
69 byte[] newBuffer =
new byte[newSize];
70 int leftInBuffer = bufferLength - bufferPosition;
72 if (leftInBuffer > newSize)
75 numToCopy = leftInBuffer;
76 Array.Copy(buffer, bufferPosition, newBuffer, 0, numToCopy);
77 bufferStart += bufferPosition;
79 bufferLength = numToCopy;
85 protected internal virtual void NewBuffer(byte[] newBuffer)
93 public virtual int BufferSize
95 get {
return _bufferSize; }
98 private void CheckBufferSize(
int bufferSize)
101 throw new System.ArgumentException(
"bufferSize must be greater than 0 (got " + bufferSize +
")");
104 public override void ReadBytes(byte[] b,
int offset,
int len)
106 ReadBytes(b, offset, len,
true);
109 public override void ReadBytes(byte[] b,
int offset,
int len,
bool useBuffer)
112 if (len <= (bufferLength - bufferPosition))
117 Array.Copy(buffer, bufferPosition, b, offset, len);
118 bufferPosition += len;
123 int available = bufferLength - bufferPosition;
126 Array.Copy(buffer, bufferPosition, b, offset, available);
129 bufferPosition += available;
132 if (useBuffer && len < _bufferSize)
138 if (bufferLength < len)
141 Array.Copy(buffer, 0, b, offset, bufferLength);
142 throw new System.IO.IOException(
"read past EOF");
146 Array.Copy(buffer, 0, b, offset, len);
147 bufferPosition = len;
159 long after = bufferStart + bufferPosition + len;
160 if (after > Length())
161 throw new System.IO.IOException(
"read past EOF");
162 ReadInternal(b, offset, len);
170 private void Refill()
172 long start = bufferStart + bufferPosition;
173 long end = start + _bufferSize;
177 int newLength = (int) (end - start);
179 throw new System.IO.IOException(
"read past EOF");
183 NewBuffer(
new byte[_bufferSize]);
184 SeekInternal(bufferStart);
186 ReadInternal(buffer, 0, newLength);
187 bufferLength = newLength;
201 public abstract void ReadInternal(byte[] b,
int offset,
int length);
203 public override long FilePointer
205 get {
return bufferStart + bufferPosition; }
208 public override void Seek(
long pos)
210 if (pos >= bufferStart && pos < (bufferStart + bufferLength))
211 bufferPosition = (int) (pos - bufferStart);
227 public abstract void SeekInternal(
long pos);
229 public override System.Object Clone()
234 clone.bufferLength = 0;
235 clone.bufferPosition = 0;
236 clone.bufferStart = FilePointer;