SafeDeflateStream
Posted by Mark Nelson on 24th January 2006 | Permanent Link
Jason Thomas gives Microsoft props for integrating deflate code into .NET 2.0:
With .NET 2.0, Microsoft finally decided to integrate the deflate algorithm into the framework. Great! This means, we no longer need to buy the Component One library and modify it to true asynchronous usage, or to hack ICSharpCodes for similar functionality.
But just as Jason giveth, Jason can taketh away:
Unfortunately, Microsoft’s implementation suffers a severe problem. If you are decompressing a stream using async methods (BeginRead) and the stream is corrupt, the framework throws an un-catchable exception from which you cannot recover.
Yeah, this is a serious bummer. It’s not necessarily so bad if your code is living in a highly controlled environment, but if you need to accept input from the wild, there’s no telling what you might get. So Jason cooked up a C# wrapper class that catches exceptions, even when using asynch methods. Thanks Jason!