SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Working with MemoryStream

Last post 09-16-2008 2:38 AM by WebSnozz. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 09-03-2008 3:27 PM

    Working with MemoryStream

    Once I populate MemoryStream I want to write it to database as a BLOB, my code looks like this:

    FileInfo exe = exel[0];
    string path = exe.FullName;
    FileStream fs = new FileStream(path, FileMode.Open);
    BinaryReader br = new BinaryReader(fs);
    MemoryStream str = new MemoryStream();
    using (ZipOutputStream zipOS = new ZipOutputStream(str))
     {
             zipOS.SetLevel(9);
             zipOS.UseZip64 = 0;
             ZipEntry zipEntry = new ZipEntry(path);
             zipOS.PutNextEntry(zipEntry);
             byte[ bin = br.ReadBytes(Convert.ToInt32(fs.Length));
             zipOS.Write(bin, 0, Convert.ToInt32(fs.Length));
     }
                                   
     int duzina = Convert.ToInt32(str.Length); //this line throws exception:         Message    "Cannot access a closed Stream." 

    byte[ binz = str.ToArray(); //but this one works fine

     

    What am I doing wrong?

     

  • 09-04-2008 10:32 AM In reply to

    Re: Working with MemoryStream

     I just have to place duzina= Convert.ToInt32(str.Length); inside curly brackets (I have to declare it outside of the scope though)

  • 09-16-2008 2:38 AM In reply to

    Re: Working with MemoryStream

    I took a peak at the .net 2.0 source code, and it looks like you can call ToArray after the stream has been closed. So you could do ToArray first, then get the length of the Array. 

     

    But like you said, you can also move the variable into the using block.

    The one thing I would worry about reading the length before closing the stream is whether everything has been flushed to the stream before you close it.  I couldn't really work that part out from the source code.

    Almost all the other functions and properties throw an exception if the stream is closed, probably because they are all part of the implementation of stream and so are designed to be consistent with the implementation of other Streams.

Page 1 of 1 (3 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.