/
FileBufferingWriter.Reader.cs
163 lines (123 loc) · 6.19 KB
/
FileBufferingWriter.Reader.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
namespace DotNext.IO;
public partial class FileBufferingWriter
{
private sealed class ReaderStream : Stream
{
private readonly Stream source;
private ReadSession session;
internal ReaderStream(FileBufferingWriter writer, bool useAsyncIO)
{
source = writer.GetWrittenContentAsStream(useAsyncIO);
session = writer.EnterReadMode(this);
}
public override long Position
{
get => source.Position;
set => source.Position = value;
}
public override long Length => source.Length;
public override void SetLength(long value)
=> throw new NotSupportedException();
public override long Seek(long offset, SeekOrigin origin)
=> source.Seek(offset, origin);
public override bool CanRead => true;
public override bool CanWrite => false;
public override bool CanSeek => source.CanSeek;
public override bool CanTimeout => source.CanTimeout;
public override int ReadByte()
=> source.ReadByte();
public override int Read(Span<byte> output)
=> source.Read(output);
public override int Read(byte[] buffer, int offset, int count)
{
ValidateBufferArguments(buffer, offset, count);
return source.Read(buffer, offset, count);
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
{
ValidateBufferArguments(buffer, offset, count);
return source.BeginRead(buffer, offset, count, callback, state);
}
public override int EndRead(IAsyncResult asyncResult)
=> source.EndRead(asyncResult);
public override ValueTask<int> ReadAsync(Memory<byte> output, CancellationToken token)
=> source.ReadAsync(output, token);
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken token)
=> source.ReadAsync(buffer, offset, count, token);
public override void Flush() => source.Flush();
public override Task FlushAsync(CancellationToken token) => source.FlushAsync(token);
public override void CopyTo(Stream destination, int bufferSize)
{
ValidateCopyToArguments(destination, bufferSize);
source.CopyTo(destination, bufferSize);
}
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken token)
=> source.CopyToAsync(destination, bufferSize, token);
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
public override void Write(ReadOnlySpan<byte> buffer) => throw new NotSupportedException();
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken token)
=> token.IsCancellationRequested ? Task.FromCanceled(token) : Task.FromException(new NotSupportedException());
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken token)
=> token.IsCancellationRequested ? ValueTask.FromCanceled(token) : ValueTask.FromException(new NotSupportedException());
public override void WriteByte(byte value) => throw new NotSupportedException();
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
=> throw new NotSupportedException();
public override void EndWrite(IAsyncResult ar) => throw new InvalidOperationException();
protected override void Dispose(bool disposing)
{
if (disposing)
{
source.Dispose();
session.Dispose();
session = default;
}
base.Dispose(disposing);
}
public override async ValueTask DisposeAsync()
{
try
{
await source.DisposeAsync().ConfigureAwait(false);
}
finally
{
await base.DisposeAsync().ConfigureAwait(false);
}
}
}
private Stream GetWrittenContentAsStream(bool useAsyncIO)
{
const FileOptions withAsyncIO = FileOptions.Asynchronous | FileOptions.SequentialScan;
const FileOptions withoutAsyncIO = FileOptions.SequentialScan;
// reuse the same handle when opening file for read
return fileName is null
? StreamSource.AsStream(buffer.Memory.Slice(0, position))
: new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, fileProvider.BufferSize, useAsyncIO ? withAsyncIO : withoutAsyncIO);
}
/// <summary>
/// Gets written content as read-only stream.
/// </summary>
/// <returns>Read-only stream representing the written content.</returns>
/// <exception cref="InvalidOperationException">The stream is already obtained but not disposed.</exception>
public Stream GetWrittenContentAsStream()
{
if (IsReading)
throw new InvalidOperationException(ExceptionMessages.WriterInReadMode);
Flush();
return new ReaderStream(this, false);
}
/// <summary>
/// Gets written content as read-only stream asynchronously.
/// </summary>
/// <param name="token">The token that can be used to cancel the operation.</param>
/// <returns>Read-only stream representing the written content.</returns>
/// <exception cref="InvalidOperationException">The stream is already obtained but not disposed.</exception>
/// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
public async ValueTask<Stream> GetWrittenContentAsStreamAsync(CancellationToken token = default)
{
if (IsReading)
throw new InvalidOperationException(ExceptionMessages.WriterInReadMode);
await FlushAsync(flushToDisk: false, token).ConfigureAwait(false);
return new ReaderStream(this, true);
}
}