Mike
Mike

Reputation:

Duplicating a stream

I have a producer which provides a System.IO.Stream instance. I also have several clients which consume this stream.

Is it possible to give each client a "private view" of the stream? For example, if clientA reads from the stream, it doesn't affect the position clientB sees (i.e. if clientB start reading from the stream, it gets the beginning of it, not from where clientA left the position). If it makes any difference, the clients are only reading from the stream.

Hope it makes sense.

Thanks in advance, Mike

Upvotes: 3

Views: 264

Answers (3)

Richard
Richard

Reputation: 108995

I think you'll need to create your own custom "TssStream" which handles the buffering from the source stream to multiple streams.

The number of methods you'll need to override from Stream is limited to handle read only, but coordinating where each client (likely via a helper) will require a little thought.

Upvotes: 2

gabor
gabor

Reputation: 1030

You can do this by reading the stream, caching the data and opening new streams to the cache for each client.

Upvotes: 0

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421988

You could inherit a class from Stream that takes the underlying stream in the constructor and keeps track of the position of that instance of private view. This works only if the base stream is seekable.

Upvotes: 1

Related Questions