-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathTemporaryProgressStream.cs
More file actions
114 lines (93 loc) · 3 KB
/
TemporaryProgressStream.cs
File metadata and controls
114 lines (93 loc) · 3 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
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
using System.Threading;
using System.Threading.Tasks;
using Waher.Events;
namespace Waher.Runtime.Temporary
{
/// <summary>
/// Class managing the contents of a temporary stream. When the class is disposed, any temporary file is deleted.
/// The class provides an event letting the caller receive events on progress.
/// </summary>
public class TemporaryProgressStream : TemporaryStream
{
private long nrReadTotal = 0;
private long nrWrittenTotal = 0;
/// <summary>
/// Class managing the contents of a temporary stream. When the class is disposed, any temporary file is deleted.
/// The class provides an event letting the caller receive events on progress.
/// </summary>
public TemporaryProgressStream()
: base()
{
}
/// <summary>
/// Class managing the contents of a temporary stream. When the class is disposed, any temporary file is deleted.
/// The class provides an event letting the caller receive events on progress.
/// </summary>
/// <param name="BufferSize">Buffer size.</param>
public TemporaryProgressStream(int BufferSize)
: base(BufferSize)
{
}
/// <summary>
/// Event raised when data has been read from the file.
/// </summary>
public event EventHandlerAsync<IoBytesEventArgs> OnRead;
/// <summary>
/// Event raised when data has been written to the file.
/// </summary>
public event EventHandlerAsync<IoBytesEventArgs> OnWrite;
/// <inheritdoc/>
public override void Write(byte[] array, int offset, int count)
{
base.Write(array, offset, count);
this.nrWrittenTotal += count;
this.OnWrite.Raise(this, new IoBytesEventArgs(count, this.nrWrittenTotal));
}
/// <inheritdoc/>
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
await base.WriteAsync(buffer, offset, count, cancellationToken);
this.nrWrittenTotal += count;
await this.OnWrite.Raise(this, new IoBytesEventArgs(count, this.nrWrittenTotal));
}
/// <inheritdoc/>
public override void WriteByte(byte value)
{
base.WriteByte(value);
this.nrWrittenTotal++;
this.OnWrite.Raise(this, new IoBytesEventArgs(1, this.nrWrittenTotal));
}
/// <inheritdoc/>
public override int Read(byte[] array, int offset, int count)
{
int i = base.Read(array, offset, count);
if (i > 0)
{
this.nrReadTotal += i;
this.OnRead.Raise(this, new IoBytesEventArgs(i, this.nrReadTotal));
}
return i;
}
/// <inheritdoc/>
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
int i = await base.ReadAsync(buffer, offset, count, cancellationToken);
if (i > 0)
{
this.nrReadTotal += i;
await this.OnRead.Raise(this, new IoBytesEventArgs(i, this.nrReadTotal));
}
return i;
}
/// <inheritdoc/>
public override int ReadByte()
{
int i = base.ReadByte();
if (i < 0)
return i;
this.nrReadTotal++;
this.OnRead.Raise(this, new IoBytesEventArgs(i, this.nrReadTotal));
return i;
}
}
}