forked from TASEmulators/BizHawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitcherStream.cs
86 lines (73 loc) · 2.42 KB
/
SwitcherStream.cs
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
using System;
using System.IO;
namespace BizHawk.Common
{
/// <summary>
/// This stream redirects all operations to another stream, specified by the user
/// You might think you can do this just by changing out the stream instance you operate on, but this was built to facilitate some features which were never built:
/// The ability to have the old stream automatically flushed, or for a derived class to manage two streams at a higher level and use these facilities to switch them
/// without this subclass's clients knowing about the existence of two streams.
/// Well, it could be useful, so here it is.
/// </summary>
public class SwitcherStream : Stream
{
// switchstream method? flush old stream?
private Stream _currStream;
public SwitcherStream()
{
}
/// <summary>
/// if this is enabled, seeks to Begin,0 will get ignored; anything else will be an exception
/// </summary>
public bool DenySeekHack = false;
public override bool CanRead { get { return _currStream.CanRead; } }
public override bool CanSeek { get { return _currStream.CanSeek; } }
public override bool CanWrite { get { return _currStream.CanWrite; } }
public override long Length { get { return _currStream.Length; } }
public override long Position
{
get
{
return _currStream.Position;
}
set
{
if (DenySeekHack)
{
if (value == 0) return;
else throw new InvalidOperationException("Cannot set position to non-zero in a SwitcherStream with DenySeekHack=true");
}
_currStream.Position = value;
}
}
public void SetCurrStream(Stream str)
{
_currStream = str;
}
public override void Flush()
{
_currStream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return _currStream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
if (DenySeekHack)
{
if (offset == 0 && origin == SeekOrigin.Begin) return 0;
else throw new InvalidOperationException("Cannot call Seek with non-zero offset or non-Begin origin in a SwitcherStream with DenySeekHack=true");
}
return _currStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
_currStream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
_currStream.Write(buffer, offset, count);
}
}
}