-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputFilterStream.cs
More file actions
85 lines (71 loc) · 2.55 KB
/
OutputFilterStream.cs
File metadata and controls
85 lines (71 loc) · 2.55 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
using System;
using System.IO;
namespace RequestResponseModule
{
// Credit to "mckamey"
// https://stackoverflow.com/a/1792864/3682729
/// <summary>
/// A stream which keeps an in-memory copy as it passes the bytes through
/// </summary>
public class OutputFilterStream : Stream
{
private readonly Stream InnerStream;
private readonly MemoryStream CopyStream;
public OutputFilterStream(Stream inner) {
this.InnerStream = inner;
this.CopyStream = new MemoryStream();
}
public string ReadStream() {
lock(this.InnerStream) {
if(this.CopyStream.Length <= 0L ||
!this.CopyStream.CanRead ||
!this.CopyStream.CanSeek) {
return String.Empty;
}
long pos = this.CopyStream.Position;
this.CopyStream.Position = 0L;
try {
return new StreamReader(this.CopyStream).ReadToEnd();
} finally {
try {
this.CopyStream.Position = pos;
} catch { }
}
}
}
public override bool CanRead {
get { return this.InnerStream.CanRead; }
}
public override bool CanSeek {
get { return this.InnerStream.CanSeek; }
}
public override bool CanWrite {
get { return this.InnerStream.CanWrite; }
}
public override void Flush() {
this.InnerStream.Flush();
}
public override long Length {
get { return this.InnerStream.Length; }
}
public override long Position {
get { return this.InnerStream.Position; }
set { this.CopyStream.Position = this.InnerStream.Position = value; }
}
public override int Read(byte[] buffer, int offset, int count) {
return this.InnerStream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin) {
this.CopyStream.Seek(offset, origin);
return this.InnerStream.Seek(offset, origin);
}
public override void SetLength(long value) {
this.CopyStream.SetLength(value);
this.InnerStream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count) {
this.CopyStream.Write(buffer, offset, count);
this.InnerStream.Write(buffer, offset, count);
}
}
}