Make the current point of a C# stream the beginning

I have to read some bytes of a C# stream and then pass the stream to a library method, which should read the rest of it. Unfortunately, this method calls a stream.Seek(0, SeekOrigin.Begin). Thus, it will read the first bytes, which it cannot understand.

Can I reset the beginning of a stream to its current position? I do not want to copy the whole rest of the stream into a MemoryStream, because it might be quite large.

Jon Skeet
people
quotationmark

No, there's nothing that does that... but what you could do is create your own wrapper stream which maintains a reference to the original stream, and delegates all calls to it... modifying any Seek() or Position calls appropriately. You may find that for your purposes, you can make some of the more esoteric calls (such as async support) just throw NotImplementedException, for simplicity.

people

See more on this question at Stackoverflow