forked from Code-Sharp/uHttpSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLimitedStream.cs
More file actions
191 lines (165 loc) · 5.11 KB
/
Copy pathLimitedStream.cs
File metadata and controls
191 lines (165 loc) · 5.11 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
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
using System;
using System.IO;
using System.Text;
namespace uhttpsharp
{
internal class LoggingStream : Stream
{
private readonly Stream _child;
private readonly string _tempFileName = Path.GetTempFileName();
public LoggingStream(Stream child)
{
_child = child;
//Console.WriteLine($"Logging to {_tempFileName}");
}
public override void Flush()
{
_child.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
return _child.Seek(offset, origin);
}
public override void SetLength(long value)
{
_child.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count)
{
int retVal = _child.Read(buffer, offset, count);
using (FileStream stream = File.Open(_tempFileName, FileMode.Append))
{
stream.Seek(0, SeekOrigin.End);
stream.Write(buffer, offset, retVal);
}
return retVal;
}
public override int ReadByte()
{
int retVal = _child.ReadByte();
using (FileStream stream = File.Open(_tempFileName, FileMode.Append))
{
stream.Seek(0, SeekOrigin.End);
stream.WriteByte((byte)retVal);
}
return retVal;
}
public override void Write(byte[] buffer, int offset, int count)
{
_child.Write(buffer, offset, count);
}
public override void WriteByte(byte value)
{
_child.WriteByte(value);
}
public override bool CanRead => _child.CanRead;
public override bool CanSeek => _child.CanSeek;
public override bool CanWrite => _child.CanWrite;
public override long Length => _child.Length;
public override long Position
{
get => _child.Position;
set => _child.Position = value;
}
public override int ReadTimeout
{
get => _child.ReadTimeout;
set => _child.ReadTimeout = value;
}
public override int WriteTimeout
{
get => _child.WriteTimeout;
set => _child.WriteTimeout = value;
}
}
internal class LimitedStream : Stream
{
private const string ExceptionMessageFormat = "The Stream has exceeded the {0} limit specified.";
private readonly Stream _child;
private long _readLimit;
private long _writeLimit;
public LimitedStream(Stream child, long readLimit = -1, long writeLimit = -1)
{
_child = child;
_readLimit = readLimit;
_writeLimit = writeLimit;
}
public override void Flush()
{
_child.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
return _child.Seek(offset, origin);
}
public override void SetLength(long value)
{
_child.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count)
{
int retVal = _child.Read(buffer, offset, count);
AssertReadLimit(retVal);
return retVal;
}
private void AssertReadLimit(int coefficient)
{
if (_readLimit == -1)
{
return;
}
_readLimit -= coefficient;
if (_readLimit < 0)
{
throw new IOException(string.Format(ExceptionMessageFormat, "read"));
}
}
private void AssertWriteLimit(int coefficient)
{
if (_writeLimit == -1)
{
return;
}
_writeLimit -= coefficient;
if (_writeLimit < 0)
{
throw new IOException(string.Format(ExceptionMessageFormat, "write"));
}
}
public override int ReadByte()
{
int retVal = _child.ReadByte();
AssertReadLimit(1);
return retVal;
}
public override void Write(byte[] buffer, int offset, int count)
{
_child.Write(buffer, offset, count);
AssertWriteLimit(count);
}
public override void WriteByte(byte value)
{
_child.WriteByte(value);
AssertWriteLimit(1);
}
public override bool CanRead => _child.CanRead;
public override bool CanSeek => _child.CanSeek;
public override bool CanWrite => _child.CanWrite;
public override long Length => _child.Length;
public override long Position
{
get => _child.Position;
set => _child.Position = value;
}
public override int ReadTimeout
{
get => _child.ReadTimeout;
set => _child.ReadTimeout = value;
}
public override int WriteTimeout
{
get => _child.WriteTimeout;
set => _child.WriteTimeout = value;
}
}
}