You are using using
statements. using
statements are compiler directives for a try finally
to be wrapped around the block of code and IDisposible.Dispose()
will be called when the finally
is executed.
Long story short, calling dispose on the BinaryWriter
, BZip2InputStream
, and FileStream
are probably prematurely disposing of the parent MemoryStream
.
Try removing the three using
blocks from within the MemoryStream
and see if that fixes your issue.
Edit
Your BinaryWriter
is writing a single byte
to the MemoryStream
. I don't believe you need a BinaryWriter
for this as MemoryStream
has a WriteByte()
method.
Then your BZip2InputStream
is trying to read from the MemoryStream
. But the MemoryStream
has it's position at the end of the stream. There is no data to read, hence the EndOfStreamException
.