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
| using System;
using System.IO;
namespace Null.Library.EventedStream
{
class ReadStreamEventArgs : EventArgs
{
public byte[] Buffer;
public int Offset, Count;
public bool Denied = false;
}
class WriteStreamEventArgs : EventArgs
{
public byte[] Buffer;
public int Offset, Count;
public bool Denied = false;
}
public class FlushStreamEventArgs : EventArgs
{
public bool Denied = false;
}
public class SetStreamLengthEventArgs : EventArgs
{
public long Value;
public bool Denied = false;
}
public class SeekStreamEventArgs : EventArgs
{
public long Offset;
public SeekOrigin SeekOrigin;
public bool Denied = false;
}
class EventedStream : Stream, IDisposable
{
MemoryStream baseMemory = new MemoryStream();
public override bool CanRead => baseMemory.CanRead;
public override bool CanSeek => baseMemory.CanSeek;
public override bool CanWrite => baseMemory.CanWrite;
public override long Length => baseMemory.Length;
public override long Position { get => baseMemory.Position; set => baseMemory.Position = value; }
public override void Flush()
{
if (PreviewFlush != null)
{
FlushStreamEventArgs args = new FlushStreamEventArgs();
PreviewFlush.Invoke(this, args);
if (args.Denied)
return;
}
baseMemory.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
if (PreviewRead != null)
{
ReadStreamEventArgs args = new ReadStreamEventArgs()
{
Buffer = buffer,
Offset = offset,
Count = count
};
PreviewRead.Invoke(this, args);
if (args.Denied)
return 0;
}
return baseMemory.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
if (PreviewSeek != null)
{
SeekStreamEventArgs args = new SeekStreamEventArgs()
{
Offset = offset,
SeekOrigin = origin
};
PreviewSeek.Invoke(this, args);
if (args.Denied)
return Position;
}
return baseMemory.Seek(offset, origin);
}
public override void SetLength(long value)
{
if (PreviewSetLength != null)
{
SetStreamLengthEventArgs args = new SetStreamLengthEventArgs()
{
Value = value
};
PreviewSetLength.Invoke(this, args);
if (args.Denied)
return;
}
baseMemory.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
if (PreviewWrite != null)
{
WriteStreamEventArgs args = new WriteStreamEventArgs()
{
Buffer = buffer,
Offset = offset,
Count = count
};
PreviewWrite.Invoke(this, args);
if (args.Denied)
return;
}
baseMemory.Write(buffer, offset, count);
}
public new void Dispose()
{
baseMemory.Dispose();
}
public event EventHandler<FlushStreamEventArgs> PreviewFlush;
public event EventHandler<SetStreamLengthEventArgs> PreviewSetLength;
public event EventHandler<SeekStreamEventArgs> PreviewSeek;
public event EventHandler<WriteStreamEventArgs> PreviewWrite;
public event EventHandler<ReadStreamEventArgs> PreviewRead;
}
}
|