I’m writing a VB.net application (but you can answer in C# if you want, no problem) that uses a 3rd party .NET library. In particular, one method in this library takes an IO.Stream as input (among other things) and writes the results of its processing to said stream. My problem is that the method CLOSES the stream after writing to it, so I can’t read back the data that it wrote in it!
To be more specific: it works, of course, if the stream is a FIeStream , since it writes the data on disk, but what if I want to read the data directly to memory? I tried using a MemoryStream, but as I said, when the method returns the stream is already closed, and I can’t read back anything from it 🙁
EDIT: Solved by doing something similar to what “usr” suggested. Here’s the code, if anyone needs it:
Imports System.IO
Public Class CloseHijackedMemoryStream
Inherits MemoryStream
Public Overrides Sub Close()
'We don't do anything, so the stream is still open
End Sub
Public Sub RealClose()
MyBase.Close()
End Sub
End Class
You can write a wrapper class around a stream that forwards all calls to the underlying (wrapped) stream but does not forward the Close and Dispose calls. That will prevent the library from closing it.
Derive from Stream, take an “inner stream” as a ctor arg and implement all methods by delegating to to the inner stream.