/
FileBufferingWriter.cs
962 lines (812 loc) · 36.3 KB
/
FileBufferingWriter.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
using System.Buffers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Metrics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SafeFileHandle = Microsoft.Win32.SafeHandles.SafeFileHandle;
namespace DotNext.IO;
using Buffers;
/// <summary>
/// Represents builder of contiguous block of memory that may
/// switch to file as its backing store.
/// </summary>
/// <remarks>
/// This class can be used to write buffered content to a target <see cref="Stream"/>
/// or <see cref="IBufferWriter{T}"/>. Additionally, it's possible to get buffered
/// content as <see cref="Memory{T}"/>. If memory threshold was not reached then
/// returned <see cref="Memory{T}"/> instance references bytes in memory. Otherwise,
/// it references memory-mapped file.
/// </remarks>
public sealed partial class FileBufferingWriter : Stream, IBufferWriter<byte>, IGrowableBuffer<byte>, IFlushable
{
[StructLayout(LayoutKind.Auto)]
private readonly struct ReadSession : IDisposable
{
private readonly WeakReference refHolder;
internal ReadSession(WeakReference obj)
=> refHolder = obj;
public void Dispose()
{
if (refHolder is not null)
refHolder.Target = null;
}
}
private sealed unsafe class NativeMemoryManager : MemoryManager<byte>
{
private int length;
private ReadSession session;
private void* ptr;
internal NativeMemoryManager(FileBufferingWriter writer, int length)
{
Debug.Assert(length > 0);
Debug.Assert(writer.fileBackend is not null);
this.length = length;
ptr = NativeMemory.Alloc((nuint)length);
session = writer.EnterReadMode(this);
Debug.Assert(writer.IsReading);
}
internal void SetLength(int value)
{
Debug.Assert(value > 0);
length = value;
}
public override Span<byte> GetSpan()
{
ObjectDisposedException.ThrowIf(ptr is null, this);
return new(ptr, length);
}
public override Memory<byte> Memory => CreateMemory(length);
public override MemoryHandle Pin(int elementIndex)
{
ObjectDisposedException.ThrowIf(ptr is null, this);
return new(Unsafe.Add<byte>(ptr, elementIndex));
}
public override void Unpin() => ObjectDisposedException.ThrowIf(ptr is null, this);
protected override void Dispose(bool disposing)
{
if (disposing)
{
session.Dispose();
session = default;
}
if (ptr is not null)
NativeMemory.Free(ptr);
ptr = null;
length = 0;
}
}
private sealed class BufferedMemoryManager : MemoryManager<byte>
{
private ReadSession session;
private Memory<byte> memory;
internal BufferedMemoryManager()
{
// no need to initialize memory block as empty block
}
internal BufferedMemoryManager(FileBufferingWriter writer, in Range range)
{
memory = writer.WrittenMemory[range];
session = writer.EnterReadMode(this);
Debug.Assert(writer.IsReading);
}
public override Span<byte> GetSpan()
=> memory.Span;
public override Memory<byte> Memory => memory;
public override MemoryHandle Pin(int elementIndex)
=> memory.Slice(elementIndex).Pin();
public override void Unpin()
{
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
memory = default;
session.Dispose();
session = default;
}
}
}
private enum MemoryEvaluationResult
{
Success = 0,
PersistExistingBuffer,
PersistAll,
}
private static readonly Histogram<int> AllocationMeter;
static FileBufferingWriter()
{
var meter = new Meter("DotNext.Buffers.FileBufferingWriter");
AllocationMeter = meter.CreateHistogram<int>("buffer-size", unit: "B", "In-Memory Buffer Size");
}
private readonly TagList measurementTags;
private readonly BackingFileProvider fileProvider;
private readonly int memoryThreshold;
private readonly MemoryAllocator<byte>? allocator;
private MemoryOwner<byte> buffer;
private int position;
private string? fileName;
private SafeFileHandle? fileBackend;
private long filePosition;
// If null or .Target is null then there is no active readers.
// Weak reference allows to track leaked readers when Dispose() was not called on them
private WeakReference? reader;
/// <summary>
/// Initializes a new writer.
/// </summary>
/// <param name="allocator">The allocator of internal buffer.</param>
/// <param name="memoryThreshold">The maximum amount of memory in bytes to allocate before switching to a file on disk.</param>
/// <param name="initialCapacity">Initial capacity of internal buffer. Should not be greater than <paramref name="memoryThreshold"/>.</param>
/// <param name="tempDir">
/// The location of the directory to write buffered contents to.
/// When unspecified, uses the value specified by the environment variable <c>ASPNETCORE_TEMP</c> if available, otherwise
/// uses the value returned by <see cref="Path.GetTempPath"/>.
/// </param>
/// <param name="asyncIO"><see langword="true"/> if you will use asynchronous methods of the instance; otherwise, <see langword="false"/>.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="memoryThreshold"/> is less than or equal to zero; or <paramref name="initialCapacity"/> is less than zero or greater than <paramref name="memoryThreshold"/>.</exception>
/// <exception cref="DirectoryNotFoundException"><paramref name="tempDir"/> doesn't exist.</exception>
public FileBufferingWriter(MemoryAllocator<byte>? allocator = null, int memoryThreshold = Options.DefaultMemoryThreshold, int initialCapacity = 0, string? tempDir = null, bool asyncIO = true)
: this(new Options { MemoryAllocator = allocator, MemoryThreshold = memoryThreshold, InitialCapacity = initialCapacity, TempDir = tempDir, AsyncIO = asyncIO })
{
}
/// <summary>
/// Initializes a new writer.
/// </summary>
/// <param name="options">Buffer writer options.</param>
/// <exception cref="DirectoryNotFoundException">Temporary directory for the backing file doesn't exist.</exception>
public FileBufferingWriter(in Options options)
{
if (options.UseTemporaryFile && !Directory.Exists(options.Path))
throw new DirectoryNotFoundException(ExceptionMessages.DirectoryNotFound(options.Path));
allocator = options.MemoryAllocator;
var memoryThreshold = options.MemoryThreshold;
if (options.InitialCapacity > 0)
{
buffer = allocator.AllocateAtLeast(options.InitialCapacity);
if (buffer.Length > memoryThreshold)
memoryThreshold = buffer.Length < Array.MaxLength ? buffer.Length + 1 : Array.MaxLength;
}
this.memoryThreshold = memoryThreshold;
fileProvider = new BackingFileProvider(in options);
measurementTags = options.MeasurementTags;
writeCallback = OnWrite;
writeAndFlushCallback = OnWriteAndFlush;
writeAndCopyCallback = OnWriteAndCopy;
}
/// <inheritdoc/>
public override bool CanRead => false;
/// <inheritdoc/>
public override bool CanSeek => false;
/// <inheritdoc/>
public override bool CanWrite => true;
/// <inheritdoc/>
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
/// <inheritdoc/>
public override long Length => position + filePosition;
/// <inheritdoc />
long IGrowableBuffer<byte>.WrittenCount => Length;
/// <inheritdoc />
void IGrowableBuffer<byte>.CopyTo<TConsumer>(TConsumer consumer)
=> CopyTo(consumer, Options.DefaultFileBufferSize, CancellationToken.None);
private bool IsReading => reader?.Target is not null;
[MemberNotNull(nameof(reader))]
private ReadSession EnterReadMode(IDisposable obj)
{
if (reader is { } refHolder)
{
refHolder.Target = obj;
}
else
{
refHolder = reader = new(obj, trackResurrection: false);
}
return new(refHolder);
}
/// <summary>
/// Removes all written data.
/// </summary>
/// <param name="reuseBuffer"><see langword="true"/> to keep internal buffer alive; otherwise, <see langword="false"/>.</param>
/// <exception cref="InvalidOperationException">Attempt to cleanup this writer while reading.</exception>
public void Clear(bool reuseBuffer = true)
{
if (IsReading)
throw new InvalidOperationException(ExceptionMessages.WriterInReadMode);
ClearCore(reuseBuffer);
}
/// <inheritdoc/>
void IGrowableBuffer<byte>.Clear() => Clear(reuseBuffer: false);
private void ClearCore(bool reuseBuffer)
{
if (!reuseBuffer)
{
buffer.Dispose();
}
if (fileBackend is not null)
{
fileBackend.Dispose();
fileBackend = null;
}
fileName = null;
filePosition = position = 0;
}
/// <inheritdoc/>
public Memory<byte> GetMemory(int sizeHint = 0)
{
switch (sizeHint)
{
case < 0:
throw new ArgumentOutOfRangeException(nameof(sizeHint));
case 0:
sizeHint = Math.Max(1, buffer.Length - position);
break;
}
switch (PrepareMemory(sizeHint, out var result))
{
case MemoryEvaluationResult.Success:
break;
case MemoryEvaluationResult.PersistExistingBuffer:
PersistBuffer(flushToDisk: false);
result = buffer.Memory.Slice(0, sizeHint);
break;
default:
throw new InsufficientMemoryException();
}
return result;
}
/// <inheritdoc/>
public Span<byte> GetSpan(int sizeHint = 0)
=> GetMemory(sizeHint).Span;
/// <inheritdoc/>
public void Advance(int count)
{
ArgumentOutOfRangeException.ThrowIfNegative(count);
if (position > buffer.Length - count)
ThrowInvalidOperationException();
position += count;
[DoesNotReturn]
[StackTraceHidden]
static void ThrowInvalidOperationException()
=> throw new InvalidOperationException();
}
private MemoryEvaluationResult PrepareMemory(int size, out Memory<byte> output)
{
var newSize = position + size;
if ((uint)newSize > (uint)memoryThreshold)
{
output = default;
return size <= memoryThreshold ? MemoryEvaluationResult.PersistExistingBuffer : MemoryEvaluationResult.PersistAll;
}
var bufLen = buffer.Length;
// expand buffer if necessary
if (bufLen < newSize)
{
bufLen <<= 1; // optimistically doubles buffer size to reduce the number of memory rentals
if ((uint)bufLen > (uint)newSize && (uint)bufLen <= (uint)memoryThreshold)
newSize = bufLen;
buffer.Resize(newSize, allocator);
AllocationMeter.Record(buffer.Length, measurementTags);
}
output = buffer.Memory.Slice(position, size);
return MemoryEvaluationResult.Success;
}
private bool HasBufferedData => position > 0;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private Memory<byte> WrittenMemory => buffer.Memory.Slice(0, position);
private ReadOnlySpan<byte> WrittenSpan => buffer.Span.Slice(0, position);
[MemberNotNull(nameof(fileBackend))]
private ValueTask PersistBufferAsync(bool flushToDisk, CancellationToken token)
{
Debug.Assert(HasBufferedData);
EnsureBackingStore();
return Submit(RandomAccess.WriteAsync(fileBackend, WrittenMemory, filePosition, token), flushToDisk ? writeAndCopyCallback : writeCallback);
}
[MemberNotNull(nameof(fileBackend))]
private void PersistBuffer(bool flushToDisk)
{
Debug.Assert(HasBufferedData);
EnsureBackingStore();
RandomAccess.Write(fileBackend, WrittenSpan, filePosition);
filePosition += position;
position = 0;
if (flushToDisk)
RandomAccess.FlushToDisk(fileBackend);
}
/// <inheritdoc/>
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken token = default)
{
if (IsReading)
return ValueTask.FromException(new InvalidOperationException(ExceptionMessages.WriterInReadMode));
switch (PrepareMemory(buffer.Length, out var output))
{
default:
return ValueTask.CompletedTask;
case MemoryEvaluationResult.Success:
buffer.CopyTo(output);
position += buffer.Length;
goto default;
case MemoryEvaluationResult.PersistExistingBuffer:
return PersistExistingBufferAsync(buffer, token);
case MemoryEvaluationResult.PersistAll:
return PersistAllAsync(buffer, token);
}
}
private ValueTask PersistExistingBufferAsync(ReadOnlyMemory<byte> buffer, CancellationToken token)
{
Debug.Assert(HasBufferedData);
EnsureBackingStore();
secondBuffer = buffer;
return Submit(RandomAccess.WriteAsync(fileBackend, WrittenMemory, filePosition, token), writeAndCopyCallback);
}
private ValueTask PersistAllAsync(ReadOnlyMemory<byte> buffer, CancellationToken token)
{
EnsureBackingStore();
ValueTask task;
if (HasBufferedData)
{
secondBuffer = buffer;
task = RandomAccess.WriteAsync(fileBackend, (IReadOnlyList<ReadOnlyMemory<byte>>)this.As<IDynamicInterfaceCastable>(), filePosition, token);
}
else
{
position = buffer.Length;
task = RandomAccess.WriteAsync(fileBackend, buffer, filePosition, token);
}
return Submit(task, writeCallback);
}
/// <inheritdoc/>
public override void Write(ReadOnlySpan<byte> buffer)
{
if (IsReading)
throw new InvalidOperationException(ExceptionMessages.WriterInReadMode);
switch (PrepareMemory(buffer.Length, out var output))
{
case MemoryEvaluationResult.Success:
buffer.CopyTo(output.Span);
position += buffer.Length;
break;
case MemoryEvaluationResult.PersistExistingBuffer:
PersistBuffer(flushToDisk: false);
buffer.CopyTo(this.buffer.Span);
position = buffer.Length;
break;
case MemoryEvaluationResult.PersistAll:
if (HasBufferedData)
PersistBuffer(flushToDisk: false);
else
EnsureBackingStore();
RandomAccess.Write(fileBackend, buffer, filePosition);
filePosition += buffer.Length;
break;
}
}
[MemberNotNull(nameof(fileBackend))]
private void EnsureBackingStore() => fileBackend ??= fileProvider.CreateBackingFileHandle(position, out fileName);
/// <inheritdoc/>
public override void Write(byte[] buffer, int offset, int count)
{
ValidateBufferArguments(buffer, offset, count);
Write(new ReadOnlySpan<byte>(buffer, offset, count));
}
/// <inheritdoc/>
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken token)
=> WriteAsync(new ReadOnlyMemory<byte>(buffer, offset, count), token).AsTask();
/// <inheritdoc/>
public override void WriteByte(byte value)
=> Write(new(ref value));
/// <inheritdoc/>
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
=> TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count), callback, state);
/// <inheritdoc/>
public override void EndWrite(IAsyncResult ar) => TaskToAsyncResult.End(ar);
/// <inheritdoc/>
public override void Flush() => Flush(flushToDisk: false);
/// <summary>
/// Flushes the internal buffer with the file and optionally
/// synchronize a file's in-core state with storage device.
/// </summary>
/// <param name="flushToDisk"><see langword="true"/> to synchronize a file's in-core state with storage device; otherwise, <see langword="false"/>.</param>
public void Flush(bool flushToDisk)
{
if (fileBackend is null)
{
// jump to exit
}
else if (HasBufferedData)
{
PersistBuffer(flushToDisk);
}
else if (flushToDisk)
{
RandomAccess.FlushToDisk(fileBackend);
}
}
/// <summary>
/// Flushes the internal buffer with the file and optionally
/// synchronize a file's in-core state with storage device.
/// </summary>
/// <param name="flushToDisk"><see langword="true"/> to synchronize a file's in-core state with storage device; otherwise, <see langword="false"/>.</param>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
/// <returns>The asynchronous result of the operation.</returns>
public ValueTask FlushAsync(bool flushToDisk, CancellationToken token = default)
{
var result = ValueTask.CompletedTask;
if (fileBackend is null)
{
// jump to exit
}
else if (HasBufferedData)
{
result = PersistBufferAsync(flushToDisk, token);
}
else if (flushToDisk)
{
try
{
RandomAccess.FlushToDisk(fileBackend);
}
catch (Exception e)
{
result = ValueTask.FromException(e);
}
}
return result;
}
/// <inheritdoc/>
public override Task FlushAsync(CancellationToken token)
=> FlushAsync(flushToDisk: false, token).AsTask();
/// <inheritdoc/>
public override int ReadByte()
=> throw new NotSupportedException();
/// <inheritdoc/>
public override int Read(byte[] buffer, int offset, int count)
=> throw new NotSupportedException();
/// <inheritdoc/>
public override int Read(Span<byte> buffer)
=> throw new NotSupportedException();
/// <inheritdoc/>
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken token)
=> Task.FromException<int>(new NotSupportedException());
/// <inheritdoc/>
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken token)
=> ValueTask.FromException<int>(new NotSupportedException());
/// <inheritdoc/>
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
=> throw new NotSupportedException();
/// <inheritdoc/>
public override int EndRead(IAsyncResult asyncResult)
=> throw new InvalidOperationException();
/// <inheritdoc/>
public override void SetLength(long value)
=> throw new NotSupportedException();
/// <summary>
/// Drains the written content to the consumer asynchronously.
/// </summary>
/// <typeparam name="TConsumer">The type of the consumer.</typeparam>
/// <param name="consumer">The consumer of the written content.</param>
/// <param name="bufferSize">The size, in bytes, of the buffer used to copy bytes.</param>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <returns>The task representing asynchronous execution of this method.</returns>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public async Task CopyToAsync<TConsumer>(TConsumer consumer, int bufferSize, CancellationToken token)
where TConsumer : notnull, ISupplier<ReadOnlyMemory<byte>, CancellationToken, ValueTask>
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bufferSize);
if (fileBackend is not null)
{
using var buffer = allocator.AllocateAtLeast(bufferSize);
int count;
for (long offset = 0L; (count = await RandomAccess.ReadAsync(fileBackend, buffer.Memory, offset, token).ConfigureAwait(false)) > 0; offset += count)
await consumer.Invoke(buffer.Memory.Slice(0, count), token).ConfigureAwait(false);
}
if (HasBufferedData)
await consumer.Invoke(WrittenMemory, token).ConfigureAwait(false);
}
/// <inheritdoc />
ValueTask IGrowableBuffer<byte>.CopyToAsync<TConsumer>(TConsumer consumer, CancellationToken token)
=> new(CopyToAsync(consumer, Options.DefaultFileBufferSize, token));
/// <summary>
/// Drains the written content to the consumer synchronously.
/// </summary>
/// <typeparam name="TConsumer">The type of the consumer.</typeparam>
/// <param name="consumer">The consumer of the written content.</param>
/// <param name="bufferSize">The size, in bytes, of the buffer used to copy bytes.</param>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public void CopyTo<TConsumer>(TConsumer consumer, int bufferSize, CancellationToken token)
where TConsumer : notnull, IReadOnlySpanConsumer<byte>
{
if (fileBackend is not null)
{
using var buffer = allocator.AllocateAtLeast(bufferSize);
int count;
for (long offset = 0L; (count = RandomAccess.Read(fileBackend, buffer.Span, offset)) > 0; offset += count, token.ThrowIfCancellationRequested())
consumer.Invoke(buffer.Span.Slice(0, count));
}
if (HasBufferedData)
consumer.Invoke(WrittenSpan);
}
/// <summary>
/// Drains buffered content to the stream asynchronously.
/// </summary>
/// <param name="destination">The stream to drain buffered contents to.</param>
/// <param name="bufferSize">The size, in bytes, of the buffer used to copy bytes.</param>
/// <param name="token">The token to monitor for cancellation requests.</param>
/// <returns>A task that represents the asynchronous copy operation.</returns>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken token)
=> CopyToAsync<StreamConsumer>(destination, bufferSize, token);
/// <summary>
/// Drains buffered content to the stream synchronously.
/// </summary>
/// <param name="destination">The stream to drain buffered contents to.</param>
/// <param name="bufferSize">The size, in bytes, of the buffer used to copy bytes.</param>
public override void CopyTo(Stream destination, int bufferSize)
{
ValidateCopyToArguments(destination, bufferSize);
CopyTo<StreamConsumer>(destination, bufferSize, CancellationToken.None);
}
/// <summary>
/// Drains buffered content to the buffer asynchronously.
/// </summary>
/// <param name="destination">The buffer to drain buffered contents to.</param>
/// <param name="bufferSize">The size, in bytes, of the buffer used to copy bytes.</param>
/// <param name="token">The token to monitor for cancellation requests.</param>
/// <returns>The task representing asynchronous execution of this method.</returns>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public Task CopyToAsync(IBufferWriter<byte> destination, int bufferSize = 1024, CancellationToken token = default)
=> CopyToAsync(new BufferConsumer<byte>(destination), bufferSize, token);
/// <summary>
/// Drains buffered content to the buffer synchronously.
/// </summary>
/// <param name="destination">The buffer to drain buffered contents to.</param>
/// <param name="bufferSize">The size, in bytes, of the buffer used to copy bytes.</param>
/// <param name="token">The token to monitor for cancellation requests.</param>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public void CopyTo(IBufferWriter<byte> destination, int bufferSize = 1024, CancellationToken token = default)
=> CopyTo(new BufferConsumer<byte>(destination), bufferSize, token);
/// <summary>
/// Drains buffered content synchronously.
/// </summary>
/// <param name="reader">The content reader.</param>
/// <param name="arg">The argument to be passed to the callback.</param>
/// <param name="bufferSize">The size, in bytes, of the buffer used to copy bytes.</param>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <typeparam name="TArg">The type of the argument to be passed to the callback.</typeparam>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public void CopyTo<TArg>(ReadOnlySpanAction<byte, TArg> reader, TArg arg, int bufferSize = 1024, CancellationToken token = default)
=> CopyTo(new DelegatingReadOnlySpanConsumer<byte, TArg>(reader, arg), bufferSize, token);
/// <summary>
/// Drains buffered content asynchronously.
/// </summary>
/// <param name="reader">The content reader.</param>
/// <param name="arg">The argument to be passed to the callback.</param>
/// <param name="bufferSize">The size, in bytes, of the buffer used to copy bytes.</param>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <typeparam name="TArg">The type of the argument to be passed to the callback.</typeparam>
/// <returns>The task representing asynchronous execution of the operation.</returns>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public Task CopyToAsync<TArg>(ReadOnlySpanAction<byte, TArg> reader, TArg arg, int bufferSize = 1024, CancellationToken token = default)
=> CopyToAsync(new DelegatingReadOnlySpanConsumer<byte, TArg>(reader, arg), bufferSize, token);
/// <summary>
/// Drains buffered content to the memory block synchronously.
/// </summary>
/// <param name="output">The memory block used as a destination for copy operation.</param>
/// <returns>The actual number of copied elements.</returns>
public int CopyTo(Span<byte> output)
{
var totalBytes = 0;
if (output.IsEmpty)
goto exit;
if (fileBackend is not null)
{
totalBytes = RandomAccess.Read(fileBackend, output, 0L);
output = output.Slice(totalBytes);
}
if (HasBufferedData)
{
WrittenSpan.CopyTo(output, out var subCount);
totalBytes += subCount;
}
exit:
return totalBytes;
}
/// <summary>
/// Drains buffered content to the memory block asynchronously.
/// </summary>
/// <param name="output">The memory block used as a destination for copy operation.</param>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <returns>The actual number of copied elements.</returns>
public async ValueTask<int> CopyToAsync(Memory<byte> output, CancellationToken token = default)
{
var totalBytes = 0;
if (output.IsEmpty)
goto exit;
if (fileBackend is not null)
{
totalBytes = await RandomAccess.ReadAsync(fileBackend, output, 0L, token).ConfigureAwait(false);
output = output.Slice(totalBytes);
}
if (HasBufferedData)
{
WrittenSpan.CopyTo(output.Span, out var subCount);
totalBytes += subCount;
}
exit:
return totalBytes;
}
private static (long Offset, long Length) GetOffsetAndLength(in Range range, long length)
{
long start = range.Start.Value;
if (range.Start.IsFromEnd)
start = length - start;
long end = range.End.Value;
if (range.End.IsFromEnd)
end = length - end;
return (start, end - start);
}
/// <summary>
/// Returns buffered content as a source of <see cref="Memory{T}"/> instances synchronously.
/// </summary>
/// <param name="range">The range of buffered content to return.</param>
/// <returns>The memory manager providing access to buffered content.</returns>
/// <exception cref="InvalidOperationException">The memory manager is already obtained but not disposed.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="range"/> is invalid.</exception>
/// <exception cref="OutOfMemoryException">The size of buffered content is too large and cannot be represented by <see cref="Memory{T}"/> instance.</exception>
public IMemoryOwner<byte> GetWrittenContent(Range range)
{
if (IsReading)
throw new InvalidOperationException(ExceptionMessages.WriterInReadMode);
if (fileBackend is null)
return new BufferedMemoryManager(this, range);
if (HasBufferedData)
PersistBuffer(flushToDisk: false);
var (offset, length) = GetOffsetAndLength(range, filePosition);
switch ((offset, length))
{
case (< 0L, _):
case (_, < 0L):
throw new ArgumentOutOfRangeException(nameof(range));
case (0L, 0L):
return new BufferedMemoryManager();
case (_, > int.MaxValue):
throw new InsufficientMemoryException();
}
var result = new NativeMemoryManager(this, unchecked((int)length));
try
{
result.SetLength(RandomAccess.Read(fileBackend, result.GetSpan(), offset));
}
catch
{
result.As<IDisposable>().Dispose();
throw;
}
return result;
}
/// <summary>
/// Returns the whole buffered content as a source of <see cref="Memory{T}"/> instances synchronously.
/// </summary>
/// <remarks>
/// Use <see cref="GetWrittenContent(Range)"/> if buffered content is too large.
/// </remarks>
/// <returns>The memory manager providing access to buffered content.</returns>
/// <exception cref="InvalidOperationException">The memory manager is already obtained but not disposed.</exception>
/// <exception cref="OutOfMemoryException">The size of buffered content is too large and cannot be represented by <see cref="Memory{T}"/> instance.</exception>
public IMemoryOwner<byte> GetWrittenContent()
=> GetWrittenContent(Range.All);
/// <summary>
/// Returns buffered content as a source of <see cref="Memory{T}"/> instances asynchronously.
/// </summary>
/// <param name="range">The range of buffered content to return.</param>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <returns>The memory manager providing access to buffered content.</returns>
/// <exception cref="InvalidOperationException">The memory manager is already obtained but not disposed.</exception>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="range"/> is invalid.</exception>
/// <exception cref="OutOfMemoryException">The size of buffered content is too large and cannot be represented by <see cref="Memory{T}"/> instance.</exception>
public async ValueTask<IMemoryOwner<byte>> GetWrittenContentAsync(Range range, CancellationToken token = default)
{
if (IsReading)
throw new InvalidOperationException(ExceptionMessages.WriterInReadMode);
if (fileBackend is null)
return new BufferedMemoryManager(this, range);
if (HasBufferedData)
await PersistBufferAsync(flushToDisk: false, token).ConfigureAwait(false);
var (offset, length) = GetOffsetAndLength(range, filePosition);
switch ((offset, length))
{
case (< 0L, _):
case (_, < 0L):
throw new ArgumentOutOfRangeException(nameof(range));
case (0L, 0L):
return new BufferedMemoryManager();
case (_, > int.MaxValue):
throw new InsufficientMemoryException();
}
var result = new NativeMemoryManager(this, unchecked((int)length));
try
{
result.SetLength(await RandomAccess.ReadAsync(fileBackend, result.Memory, offset, token).ConfigureAwait(false));
}
catch
{
result.As<IDisposable>().Dispose();
throw;
}
return result;
}
/// <summary>
/// Returns the whole buffered content as a source of <see cref="Memory{T}"/> instances asynchronously.
/// </summary>
/// <remarks>
/// Use <see cref="GetWrittenContentAsync(Range, CancellationToken)"/> if buffered content is too large.
/// </remarks>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <returns>The memory manager providing access to buffered content.</returns>
/// <exception cref="InvalidOperationException">The memory manager is already obtained but not disposed.</exception>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
/// <exception cref="OutOfMemoryException">The size of buffered content is too large and cannot be represented by <see cref="Memory{T}"/> instance.</exception>
public ValueTask<IMemoryOwner<byte>> GetWrittenContentAsync(CancellationToken token = default)
=> GetWrittenContentAsync(Range.All, token);
/// <summary>
/// Attempts to get written content if it is located in memory.
/// </summary>
/// <remarks>
/// If this method returns <see langword="false"/> then
/// use <see cref="GetWrittenContent()"/>, <see cref="GetWrittenContent(Range)"/>,
/// <see cref="GetWrittenContentAsync(CancellationToken)"/> or <see cref="GetWrittenContentAsync(Range, CancellationToken)"/>
/// to obtain the content.
/// </remarks>
/// <param name="content">The written content.</param>
/// <returns><see langword="true"/> if whole content is in memory and available without allocation of <see cref="MemoryManager{T}"/>; otherwise, <see langword="false"/>.</returns>
public bool TryGetWrittenContent(out ReadOnlyMemory<byte> content)
{
if (fileBackend is null)
{
content = WrittenMemory;
return true;
}
content = default;
return false;
}
/// <summary>
/// Attempts to get written content if it is located in memory.
/// </summary>
/// <remarks>
/// If this method returns <see langword="false"/> then
/// <paramref name="fileName"/> contains full path to the file containing the written content.
/// This method is useful only if the file was not created as temporary file.
/// </remarks>
/// <param name="content">The written content.</param>
/// <param name="fileName">The path to the file used as a buffer if this writer switched to the file.</param>
/// <returns><see langword="true"/> if whole content is in memory and available without allocation of <see cref="MemoryManager{T}"/>; otherwise, <see langword="false"/>.</returns>
public bool TryGetWrittenContent(out ReadOnlyMemory<byte> content, [NotNullWhen(false)] out string? fileName)
{
if ((fileName = this.fileName) is null)
{
content = WrittenMemory;
return true;
}
content = default;
return false;
}
/// <inheritdoc/>
public override long Seek(long offset, SeekOrigin origin)
=> throw new NotSupportedException();
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (disposing)
{
ClearCore(reuseBuffer: false);
reader = null;
}
base.Dispose(disposing);
}
}