Re: CryptoStream.Dispose closes target stream in 2.0

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Harold Howe <hhowe@xxxxxxxxxxxx> wrote:
> > In this case, however, your code can be improved in simplicity and will
> > still work in both cases:
>
> <snip>
>
> Thanks. It looks like the main addition in your code is this:
>
> data = msout.ToArray();
>
> So it is safe to use ToArray after the memory stream is closed? The
> entire concept of closing a memory stream is somewhat fuzzy to me.

Yes, ToArray is fine after closing the stream. From the docs:

<quote>
Note
This method works when the MemoryStream is closed.
</quote>

> > If you're dealing with other streams, you could use my
> > NonClosingStreamWrapper, available from
> > http://www.pobox.com/~skeet/csharp/miscutils
>
> Cool. Thanks.
>
> The correct link is http://www.pobox.com/~skeet/csharp/miscutil for
> anyone reading the archive of this.

Whoops - sorry!

> > CryptoStream.Dispose was broken in 1.1 in that it didn't close the
> > CryptoStream itself - IIRC it was actually a no-op.
>
> .net reflector showed that it wasn't quite a no-op, but it doesn't do a
> close or flush either.

The point I noticed when I looked before is that it doesn't flush the
final block (which is part of Close, IIRC).

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: CryptoStream.Dispose closes target stream in 2.0
    ... So it is safe to use ToArray after the memory stream is closed? ... The entire concept of closing a memory stream is somewhat fuzzy to me. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: GZip Compression :(
    ... base 64 step will bloat the string by a factor of 1.37 plus header data, ... ToArray on the memory stream and it seems to be pulling bytes. ... You might also be allocating the FileData array one too short - I'm not ...
    (microsoft.public.dotnet.general)
  • Re: GZip Compression :(
    ... I swithed to autoclosing the zip stream and using ToArray on the memory stream and it seems to be pulling bytes. ... You might also be allocating the FileData array one too short - I'm not sure ...
    (microsoft.public.dotnet.general)