5 public class BufferedFile : File
19 // bufferSize = 65536;
20 property::bufferSize = 512 * 1024;
21 //property::bufferSize = 10 * 1024 * 1024;
22 property::bufferRead = 1 * 1024;
40 int Read(byte * buffer, uint size, uint count)
44 uint totalBytesRead = 0;
45 uint bufferCount = this.bufferCount;
46 uint bufferPos = this.bufferPos;
47 byte * fileBuffer = this.buffer + bufferPos;
48 uint readCount = count;
54 uint bytesRead = (bufferCount > bufferPos) ? (bufferCount - bufferPos) : 0;
55 if(bytesRead > readCount) bytesRead = readCount;
58 memcpy(buffer + totalBytesRead, fileBuffer, bytesRead);
59 bufferPos += bytesRead;
60 totalBytesRead += bytesRead;
61 readCount -= bytesRead;
66 if(readCount < bufferSize /*&& bufferPos <= bufferCount*/)
68 read = Max(readCount, bufferRead);
69 if(bufferPos > bufferCount)
71 if(bufferPos + readCount - bufferCount > read && (bufferPos + readCount - bufferCount < bufferCount))
72 read = bufferPos + readCount - bufferCount;
79 if(bufferCount + read > bufferSize)
91 handle.Seek(pos + totalBytesRead - bufferPos + bufferCount, start);
92 read = handle.Read(this.buffer + bufferCount, 1, read);
93 fileBuffer = this.buffer + bufferPos;
104 this.bufferCount = bufferCount;
105 this.bufferPos = bufferPos;
106 pos += totalBytesRead;
107 return totalBytesRead / size;
112 int Write(const byte * buffer, uint size, uint count)
118 handle.Seek(pos, start);
119 result = handle.Write(buffer, size, count);
120 numBytes = result * size;
121 bytesToBuffer = (bufferSize > bufferPos) ? (bufferSize - bufferPos) : 0;
122 missing = numBytes - bytesToBuffer;
124 fileSize = Max(fileSize, pos);
129 if(bytesToBuffer < numBytes && bufferCount >= bufferPos && numBytes < bufferSize && missing < bufferPos)
131 memcpy(this.buffer, this.buffer + missing, bufferPos - missing);
132 bufferPos -= missing;
133 bufferCount -= missing;
134 bytesToBuffer += missing;
137 if(bytesToBuffer >= numBytes)
139 bytesToBuffer = numBytes;
140 memcpy(this.buffer + bufferPos, buffer, bytesToBuffer);
141 bufferPos += bytesToBuffer;
142 bufferCount = Max(bufferCount, bufferPos);
146 bytesToBuffer = Min(numBytes, bufferSize);
147 memcpy(this.buffer, buffer + numBytes - bytesToBuffer, bytesToBuffer);
148 bufferPos = bytesToBuffer;
149 bufferCount = bytesToBuffer;
156 // return Read(ch, 1,1) ? true : false;
161 if(bufferCount > bufferPos)
163 *ch = *(buffer + bufferPos);
171 handle.Seek(pos, start);
172 bufferCount = handle.Read(buffer, 1, bufferSize);
186 int written = Write(&ch, 1, 1);
190 bool Puts(const char * string)
192 int len = strlen(string);
193 int written = Write(string, 1, len);
194 return written == len;
197 bool Seek(int pos, FileSeekMode mode)
199 uint newPosition = this.pos;
211 newPosition = fileSize + pos;
215 if(this.pos != newPosition)
217 if(newPosition >= this.pos - bufferPos && newPosition < this.pos + bufferSize)
219 if(newPosition < this.pos - bufferPos + bufferCount)
220 bufferPos += newPosition - this.pos;
223 uint read = newPosition - this.pos - bufferCount;
224 if(read < bufferCount * 2)
226 if(read > bufferSize)
233 handle.Seek(this.pos - bufferPos + bufferCount, start);
234 read = handle.Read(this.buffer + bufferCount, 1, read);
235 bufferPos += newPosition - this.pos;
252 eof = newPosition > fileSize;
253 this.pos = newPosition;
275 bool Truncate(FileSize size)
277 uint bytesAhead = size - (pos - bufferPos);
278 handle.Truncate(size);
279 bufferCount = Min(bufferCount, bytesAhead);
280 fileSize = Min(fileSize, size);
284 bool Lock(FileLock type, uint64 start, uint64 length, bool wait)
286 return handle.Lock(type, start, length, wait);
289 bool Unlock(uint64 start, uint64 length, bool wait)
291 return handle.Unlock(start, length, wait);
315 property uint bufferSize
320 buffer = renew buffer byte[value];
321 if(bufferCount > bufferSize)
322 bufferCount = bufferSize;
329 property uint bufferRead
342 public BufferedFile FileOpenBuffered(const char * fileName, FileOpenMode mode)
344 BufferedFile result = null;
347 File handle = FileOpen(fileName, mode);
355 fileSize = handle.GetSize();
357 handle.buffered = true;