I want to emulate a network-type stream on a single PC.
I've done this by creating a Stream
that takes in 2 underlying streams, one to read from and another to write to.
I then create 2 instances of this class swapping the 2 streams. Currently I'm using MemoryStream
s as the 2 underlying streams.
The trouble i have now is that if I write X bytes to a MemoryStream
then its position will be X and if i then do a Read
I get no data, as i'm at the end of the stream.
Given that I'll typically be doing a couple of reads/writes (so can't just reset the postion to 0 after every write) what Stream
can i use to get this behaviour?
Effectively I want a sort of byte queue that I can write to and read to in the form of a stream.
i.e. (ignoring actual method arguments)
MyStream.Write({ 1, 2, 3, 4, 5, 6 });
MyStream.Write({ 7, 8 });
MyStream.Read(3) // Returns { 1, 2, 3 }
MyStream.Read(4) // Returns { 4, 5, 6, 7 }