none
CSharp access a closed stream using DeflateStream and MemoryStream RRS feed

  • Question

  • Dear All,

    I came across the error of access a closed stream using DeflateStream and MemoryStream.

    Here is my code snippets shown below

    // class A
    public void Test()
    {
     MemoryStream compMS = new MemoryStream();
    
     DeflateStream compressedzipStream = new DeflateStream(compMS, CompressionMode.Compress, true);
             compressedzipStream.Write(tmpArr, 0, tmpArr.Length);
    
     compressedzipStream.Close();
     compMS.Close();
     byte[] arr = compMS.ToArray();
     int len = arr.Length();
     write(arr , 0, icomplen);
    }
    
    // class B
     private MemoryStream stream = new MemoryStream();
    
    public void write(byte[] ab, int off, int len)
    {
       stream.Write(ab, off, len);    // here is error came out
    }
    

    I hope someone will advise me to close case asap since I want to finish up by htis week.

    Thanks and best regards

    Wednesday, May 8, 2013 12:08 PM

Answers

  • Write following piece of code 

     byte[] arr = compMS.ToArray();
     int len = arr.Length();
     write(arr , 0, icomplen);

    before

    compressedzipStream.Close(); compMS.Close();

    because when the stream is closed, you cann't access it


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Thursday, May 9, 2013 10:01 AM
  • See code below

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.IO.Compression;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                A a = new A();
                a.Test();
            }
        }
        class A
        {
            public void Test()
            {
                MemoryStream compMS = new MemoryStream();
                byte[] tmpArr = new byte[1024];
                DeflateStream compressedzipStream = new DeflateStream(compMS, CompressionMode.Compress, true);
                compressedzipStream.Write(tmpArr, 0, tmpArr.Length);
                compressedzipStream.Close();
                compMS.Close();
                byte[] arr = compMS.ToArray();
                int icomplen = arr.Length;
                B b = new B();
                b.write(arr, 0, icomplen);
            }
        }
        class B
        {
            // class B
            private MemoryStream stream = new MemoryStream();
            public void write(byte[] ab, int off, int len)
            {
                stream.Write(ab, off, len);    // here is error came out
            }
        }
    }


    jdweng

    Thursday, May 9, 2013 11:02 AM
  • Hi All,

    The main thing is I am trying to emulate

    java.io.ByteArrayOutputStream .reset()

    without affecting next stream.Write

    Best regards

     

    Hi Koko,

    You can try to seek the stream to 0:http://msdn.microsoft.com/en-us/library/system.io.stream.seek.aspx

    stream.Seek(0, SeekOrigin.Begin)

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 10, 2013 9:06 AM
    Moderator

All replies

  • Write following piece of code 

     byte[] arr = compMS.ToArray();
     int len = arr.Length();
     write(arr , 0, icomplen);

    before

    compressedzipStream.Close(); compMS.Close();

    because when the stream is closed, you cann't access it


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Thursday, May 9, 2013 10:01 AM
  • See code below

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.IO.Compression;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                A a = new A();
                a.Test();
            }
        }
        class A
        {
            public void Test()
            {
                MemoryStream compMS = new MemoryStream();
                byte[] tmpArr = new byte[1024];
                DeflateStream compressedzipStream = new DeflateStream(compMS, CompressionMode.Compress, true);
                compressedzipStream.Write(tmpArr, 0, tmpArr.Length);
                compressedzipStream.Close();
                compMS.Close();
                byte[] arr = compMS.ToArray();
                int icomplen = arr.Length;
                B b = new B();
                b.write(arr, 0, icomplen);
            }
        }
        class B
        {
            // class B
            private MemoryStream stream = new MemoryStream();
            public void write(byte[] ab, int off, int len)
            {
                stream.Write(ab, off, len);    // here is error came out
            }
        }
    }


    jdweng

    Thursday, May 9, 2013 11:02 AM
  • Hi All,

    Thank you for your help and replies but the actual problem is coz of stream.Dispose(); in other class cause then can't access a closed stream.

    The main thing is how to delete memorystream or stream not to affect using this stream again for the next data like below pseudo code

    stream stream = new stream();

    // Add Data here to stream

    // Then delete steam

    // Then Add Data again

    // Then write data in the stream

    Best regards

    Friday, May 10, 2013 8:03 AM
  • Hi All,

    The main thing is I am trying to emulate

    java.io.ByteArrayOutputStream .reset()

    without affecting next stream.Write

    Best regards

     

    Friday, May 10, 2013 8:06 AM
  • Hi All,

    The main thing is I am trying to emulate

    java.io.ByteArrayOutputStream .reset()

    without affecting next stream.Write

    Best regards

     

    Hi Koko,

    You can try to seek the stream to 0:http://msdn.microsoft.com/en-us/library/system.io.stream.seek.aspx

    stream.Seek(0, SeekOrigin.Begin)

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 10, 2013 9:06 AM
    Moderator