-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicFileByteProvider.cs
569 lines (502 loc) · 20.6 KB
/
DynamicFileByteProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
using System;
using System.Text;
using System.IO;
namespace Be.Windows.Forms
{
/// <summary>
/// Implements a fully editable byte provider for file data of any size.
/// </summary>
/// <remarks>
/// Only changes to the file are stored in memory with reads from the
/// original data occurring as required.
/// </remarks>
public sealed class DynamicFileByteProvider : IByteProvider, IDisposable
{
const int COPY_BLOCK_SIZE = 4096;
string _fileName;
Stream _stream;
DataMap _dataMap;
long _totalLength;
bool _readOnly;
/// <summary>
/// Constructs a new <see cref="DynamicFileByteProvider" /> instance.
/// </summary>
/// <param name="fileName">The name of the file from which bytes should be provided.</param>
public DynamicFileByteProvider(string fileName) : this(fileName, false) { }
/// <summary>
/// Constructs a new <see cref="DynamicFileByteProvider" /> instance.
/// </summary>
/// <param name="fileName">The name of the file from which bytes should be provided.</param>
/// <param name="readOnly">True, opens the file in read-only mode.</param>
public DynamicFileByteProvider(string fileName, bool readOnly)
{
_fileName = fileName;
if (!readOnly)
{
_stream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
}
else
{
_stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
_readOnly = readOnly;
ReInitialize();
}
/// <summary>
/// Constructs a new <see cref="DynamicFileByteProvider" /> instance.
/// </summary>
/// <param name="stream">the stream containing the data.</param>
/// <remarks>
/// The stream must supported seek operations.
/// </remarks>
public DynamicFileByteProvider(Stream stream)
{
if (stream == null)
throw new ArgumentNullException("stream");
if (!stream.CanSeek)
throw new ArgumentException("stream must supported seek operations(CanSeek)");
_stream = stream;
_readOnly = !stream.CanWrite;
ReInitialize();
}
#region IByteProvider Members
/// <summary>
/// See <see cref="IByteProvider.LengthChanged" /> for more information.
/// </summary>
public event EventHandler LengthChanged;
/// <summary>
/// See <see cref="IByteProvider.Changed" /> for more information.
/// </summary>
public event EventHandler Changed;
/// <summary>
/// See <see cref="IByteProvider.ReadByte" /> for more information.
/// </summary>
public byte ReadByte(long index)
{
long blockOffset;
DataBlock block = GetDataBlock(index, out blockOffset);
FileDataBlock fileBlock = block as FileDataBlock;
if (fileBlock != null)
{
return ReadByteFromFile(fileBlock.FileOffset + index - blockOffset);
}
else
{
MemoryDataBlock memoryBlock = (MemoryDataBlock)block;
return memoryBlock.Data[index - blockOffset];
}
}
/// <summary>
/// See <see cref="IByteProvider.WriteByte" /> for more information.
/// </summary>
public void WriteByte(long index, byte value)
{
try
{
// Find the block affected.
long blockOffset;
DataBlock block = GetDataBlock(index, out blockOffset);
// If the byte is already in a memory block, modify it.
MemoryDataBlock memoryBlock = block as MemoryDataBlock;
if (memoryBlock != null)
{
memoryBlock.Data[index - blockOffset] = value;
return;
}
FileDataBlock fileBlock = (FileDataBlock)block;
// If the byte changing is the first byte in the block and the previous block is a memory block, extend that.
if (blockOffset == index && block.PreviousBlock != null)
{
MemoryDataBlock previousMemoryBlock = block.PreviousBlock as MemoryDataBlock;
if (previousMemoryBlock != null)
{
previousMemoryBlock.AddByteToEnd(value);
fileBlock.RemoveBytesFromStart(1);
if (fileBlock.Length == 0)
{
_dataMap.Remove(fileBlock);
}
return;
}
}
// If the byte changing is the last byte in the block and the next block is a memory block, extend that.
if (blockOffset + fileBlock.Length - 1 == index && block.NextBlock != null)
{
MemoryDataBlock nextMemoryBlock = block.NextBlock as MemoryDataBlock;
if (nextMemoryBlock != null)
{
nextMemoryBlock.AddByteToStart(value);
fileBlock.RemoveBytesFromEnd(1);
if (fileBlock.Length == 0)
{
_dataMap.Remove(fileBlock);
}
return;
}
}
// Split the block into a prefix and a suffix and place a memory block in-between.
FileDataBlock prefixBlock = null;
if (index > blockOffset)
{
prefixBlock = new FileDataBlock(fileBlock.FileOffset, index - blockOffset);
}
FileDataBlock suffixBlock = null;
if (index < blockOffset + fileBlock.Length - 1)
{
suffixBlock = new FileDataBlock(
fileBlock.FileOffset + index - blockOffset + 1,
fileBlock.Length - (index - blockOffset + 1));
}
block = _dataMap.Replace(block, new MemoryDataBlock(value));
if (prefixBlock != null)
{
_dataMap.AddBefore(block, prefixBlock);
}
if (suffixBlock != null)
{
_dataMap.AddAfter(block, suffixBlock);
}
}
finally
{
OnChanged(EventArgs.Empty);
}
}
/// <summary>
/// See <see cref="IByteProvider.InsertBytes" /> for more information.
/// </summary>
public void InsertBytes(long index, byte[] bs)
{
try
{
// Find the block affected.
long blockOffset;
DataBlock block = GetDataBlock(index, out blockOffset);
// If the insertion point is in a memory block, just insert it.
MemoryDataBlock memoryBlock = block as MemoryDataBlock;
if (memoryBlock != null)
{
memoryBlock.InsertBytes(index - blockOffset, bs);
return;
}
FileDataBlock fileBlock = (FileDataBlock)block;
// If the insertion point is at the start of a file block, and the previous block is a memory block, append it to that block.
if (blockOffset == index && block.PreviousBlock != null)
{
MemoryDataBlock previousMemoryBlock = block.PreviousBlock as MemoryDataBlock;
if (previousMemoryBlock != null)
{
previousMemoryBlock.InsertBytes(previousMemoryBlock.Length, bs);
return;
}
}
// Split the block into a prefix and a suffix and place a memory block in-between.
FileDataBlock prefixBlock = null;
if (index > blockOffset)
{
prefixBlock = new FileDataBlock(fileBlock.FileOffset, index - blockOffset);
}
FileDataBlock suffixBlock = null;
if (index < blockOffset + fileBlock.Length)
{
suffixBlock = new FileDataBlock(
fileBlock.FileOffset + index - blockOffset,
fileBlock.Length - (index - blockOffset));
}
block = _dataMap.Replace(block, new MemoryDataBlock(bs));
if (prefixBlock != null)
{
_dataMap.AddBefore(block, prefixBlock);
}
if (suffixBlock != null)
{
_dataMap.AddAfter(block, suffixBlock);
}
}
finally
{
_totalLength += bs.Length;
OnLengthChanged(EventArgs.Empty);
OnChanged(EventArgs.Empty);
}
}
/// <summary>
/// See <see cref="IByteProvider.DeleteBytes" /> for more information.
/// </summary>
public void DeleteBytes(long index, long length)
{
try
{
long bytesToDelete = length;
// Find the first block affected.
long blockOffset;
DataBlock block = GetDataBlock(index, out blockOffset);
// Truncate or remove each block as necessary.
while ((bytesToDelete > 0) && (block != null))
{
long blockLength = block.Length;
DataBlock nextBlock = block.NextBlock;
// Delete the appropriate section from the block (this may result in two blocks or a zero length block).
long count = Math.Min(bytesToDelete, blockLength - (index - blockOffset));
block.RemoveBytes(index - blockOffset, count);
if (block.Length == 0)
{
_dataMap.Remove(block);
if (_dataMap.FirstBlock == null)
{
_dataMap.AddFirst(new MemoryDataBlock(new byte[0]));
}
}
bytesToDelete -= count;
blockOffset += block.Length;
block = (bytesToDelete > 0) ? nextBlock : null;
}
}
finally
{
_totalLength -= length;
OnLengthChanged(EventArgs.Empty);
OnChanged(EventArgs.Empty);
}
}
/// <summary>
/// See <see cref="IByteProvider.Length" /> for more information.
/// </summary>
public long Length
{
get
{
return _totalLength;
}
}
/// <summary>
/// See <see cref="IByteProvider.HasChanges" /> for more information.
/// </summary>
public bool HasChanges()
{
if (_readOnly)
return false;
if (_totalLength != _stream.Length)
{
return true;
}
long offset = 0;
for (DataBlock block = _dataMap.FirstBlock; block != null; block = block.NextBlock)
{
FileDataBlock fileBlock = block as FileDataBlock;
if (fileBlock == null)
{
return true;
}
if (fileBlock.FileOffset != offset)
{
return true;
}
offset += fileBlock.Length;
}
return (offset != _stream.Length);
}
/// <summary>
/// See <see cref="IByteProvider.ApplyChanges" /> for more information.
/// </summary>
public void ApplyChanges()
{
if (_readOnly)
throw new OperationCanceledException("File is in read-only mode");
// This method is implemented to efficiently save the changes to the same file stream opened for reading.
// Saving to a separate file would be a much simpler implementation.
// Firstly, extend the file length (if necessary) to ensure that there is enough disk space.
if (_totalLength > _stream.Length)
{
_stream.SetLength(_totalLength);
}
// Secondly, shift around any file sections that have moved.
long dataOffset = 0;
for (DataBlock block = _dataMap.FirstBlock; block != null; block = block.NextBlock)
{
FileDataBlock fileBlock = block as FileDataBlock;
if (fileBlock != null && fileBlock.FileOffset != dataOffset)
{
MoveFileBlock(fileBlock, dataOffset);
}
dataOffset += block.Length;
}
// Next, write in-memory changes.
dataOffset = 0;
for (DataBlock block = _dataMap.FirstBlock; block != null; block = block.NextBlock)
{
MemoryDataBlock memoryBlock = block as MemoryDataBlock;
if (memoryBlock != null)
{
_stream.Position = dataOffset;
for (int memoryOffset = 0; memoryOffset < memoryBlock.Length; memoryOffset += COPY_BLOCK_SIZE)
{
_stream.Write(memoryBlock.Data, memoryOffset, (int)Math.Min(COPY_BLOCK_SIZE, memoryBlock.Length - memoryOffset));
}
}
dataOffset += block.Length;
}
// Finally, if the file has shortened, truncate the stream.
_stream.SetLength(_totalLength);
ReInitialize();
}
/// <summary>
/// See <see cref="IByteProvider.SupportsWriteByte" /> for more information.
/// </summary>
public bool SupportsWriteByte()
{
return !_readOnly;
}
/// <summary>
/// See <see cref="IByteProvider.SupportsInsertBytes" /> for more information.
/// </summary>
public bool SupportsInsertBytes()
{
return !_readOnly;
}
/// <summary>
/// See <see cref="IByteProvider.SupportsDeleteBytes" /> for more information.
/// </summary>
public bool SupportsDeleteBytes()
{
return !_readOnly;
}
#endregion
#region IDisposable Members
/// <summary>
/// See <see cref="Object.Finalize" /> for more information.
/// </summary>
~DynamicFileByteProvider()
{
Dispose();
}
/// <summary>
/// See <see cref="IDisposable.Dispose" /> for more information.
/// </summary>
public void Dispose()
{
if (_stream != null)
{
_stream.Close();
_stream = null;
}
_fileName = null;
_dataMap = null;
GC.SuppressFinalize(this);
}
#endregion
/// <summary>
/// Gets a value, if the file is opened in read-only mode.
/// </summary>
public bool ReadOnly
{
get { return _readOnly; }
}
void OnLengthChanged(EventArgs e)
{
if (LengthChanged != null)
LengthChanged(this, e);
}
void OnChanged(EventArgs e)
{
if (Changed != null)
{
Changed(this, e);
}
}
DataBlock GetDataBlock(long findOffset, out long blockOffset)
{
if (findOffset < 0 || findOffset > _totalLength)
{
throw new ArgumentOutOfRangeException("findOffset");
}
// Iterate over the blocks until the block containing the required offset is encountered.
blockOffset = 0;
for (DataBlock block = _dataMap.FirstBlock; block != null; block = block.NextBlock)
{
if ((blockOffset <= findOffset && blockOffset + block.Length > findOffset) || block.NextBlock == null)
{
return block;
}
blockOffset += block.Length;
}
return null;
}
FileDataBlock GetNextFileDataBlock(DataBlock block, long dataOffset, out long nextDataOffset)
{
// Iterate over the remaining blocks until a file block is encountered.
nextDataOffset = dataOffset + block.Length;
block = block.NextBlock;
while (block != null)
{
FileDataBlock fileBlock = block as FileDataBlock;
if (fileBlock != null)
{
return fileBlock;
}
nextDataOffset += block.Length;
block = block.NextBlock;
}
return null;
}
byte ReadByteFromFile(long fileOffset)
{
// Move to the correct position and read the byte.
if (_stream.Position != fileOffset)
{
_stream.Position = fileOffset;
}
return (byte)_stream.ReadByte();
}
void MoveFileBlock(FileDataBlock fileBlock, long dataOffset)
{
// First, determine whether the next file block needs to move before this one.
long nextDataOffset;
FileDataBlock nextFileBlock = GetNextFileDataBlock(fileBlock, dataOffset, out nextDataOffset);
if (nextFileBlock != null && dataOffset + fileBlock.Length > nextFileBlock.FileOffset)
{
// The next block needs to move first, so do that now.
MoveFileBlock(nextFileBlock, nextDataOffset);
}
// Now, move the block.
if (fileBlock.FileOffset > dataOffset)
{
// Move the section to earlier in the file stream (done in chunks starting at the beginning of the section).
byte[] buffer = new byte[COPY_BLOCK_SIZE];
for (long relativeOffset = 0; relativeOffset < fileBlock.Length; relativeOffset += buffer.Length)
{
long readOffset = fileBlock.FileOffset + relativeOffset;
int bytesToRead = (int)Math.Min(buffer.Length, fileBlock.Length - relativeOffset);
_stream.Position = readOffset;
_stream.Read(buffer, 0, bytesToRead);
long writeOffset = dataOffset + relativeOffset;
_stream.Position = writeOffset;
_stream.Write(buffer, 0, bytesToRead);
}
}
else
{
// Move the section to later in the file stream (done in chunks starting at the end of the section).
byte[] buffer = new byte[COPY_BLOCK_SIZE];
for (long relativeOffset = 0; relativeOffset < fileBlock.Length; relativeOffset += buffer.Length)
{
int bytesToRead = (int)Math.Min(buffer.Length, fileBlock.Length - relativeOffset);
long readOffset = fileBlock.FileOffset + fileBlock.Length - relativeOffset - bytesToRead;
_stream.Position = readOffset;
_stream.Read(buffer, 0, bytesToRead);
long writeOffset = dataOffset + fileBlock.Length - relativeOffset - bytesToRead;
_stream.Position = writeOffset;
_stream.Write(buffer, 0, bytesToRead);
}
}
// This block now points to a different position in the file.
fileBlock.SetFileOffset(dataOffset);
}
void ReInitialize()
{
_dataMap = new DataMap();
_dataMap.AddFirst(new FileDataBlock(0, _stream.Length));
_totalLength = _stream.Length;
}
}
}