-
-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathEnhancedStreamWriter.cs
More file actions
65 lines (55 loc) · 1.94 KB
/
EnhancedStreamWriter.cs
File metadata and controls
65 lines (55 loc) · 1.94 KB
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
namespace MiniExcelLib.Core.Helpers;
public partial class EnhancedStreamWriter(Stream stream, Encoding encoding, int bufferSize) : IDisposable
{
private readonly StreamWriter _streamWriter = new(stream, encoding, bufferSize);
private bool _disposed;
[CreateSyncVersion]
public async Task WriteAsync(string content, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
if (!string.IsNullOrEmpty(content))
{
await _streamWriter.WriteAsync(content).ConfigureAwait(false);
}
}
[CreateSyncVersion]
public async Task<long> WriteAndFlushAsync(string content, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
await WriteAsync(content, cancellationToken).ConfigureAwait(false);
return await FlushAsync(cancellationToken).ConfigureAwait(false);
}
[CreateSyncVersion]
public async Task WriteWhitespaceAsync(int length)
{
await _streamWriter.WriteAsync(new string(' ', length)).ConfigureAwait(false);
}
[CreateSyncVersion]
public async Task<long> FlushAsync(CancellationToken cancellationToken = default)
{
await _streamWriter.FlushAsync(
#if NET8_0_OR_GREATER
cancellationToken
#endif
).ConfigureAwait(false);
return _streamWriter.BaseStream.Position;
}
public void SetPosition(long position)
{
_streamWriter.BaseStream.Position = position;
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
_streamWriter.Dispose();
_disposed = true;
}
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}