locked
Memory Stream is not expandable

    Question

  • Hi,

    Suppose we create a memory stream using a byte array as follows:

    byte

     

     

    [] bs = new byte[8];

    memoryStream =

     

    new MemoryStream(bs, true);

    This creates a writable, non-expandable MemoryStream.

    Is it possible to find out from the memoryStream object whether the stream is expandable or not?

    Thanks

    Friday, October 15, 2010 12:46 PM

Answers

  • Hi,

    You could use Reflector or referencesource.microsoft.com to see how it finds out that the stream is expandable. At first time I don't see anything public and this is likely specific to memory streams.

    Not sure what is your overall goal ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Proposed as answer by Jesse Houwing Friday, October 15, 2010 3:22 PM
    • Marked as answer by Figo Fei Monday, October 25, 2010 3:11 AM
    Friday, October 15, 2010 1:13 PM
  • The only way is to use the SetLength method with a value larger than the allocated capacity and catch the NotSupportedException if you want to find out through a publicly supported mechanism.

    Or use private reflection to get the following field:

    private bool _expandable;

    • Proposed as answer by Jesse Houwing Friday, October 15, 2010 3:22 PM
    • Marked as answer by Figo Fei Monday, October 25, 2010 3:11 AM
    Friday, October 15, 2010 1:23 PM

All replies

  • Hi,

    You could use Reflector or referencesource.microsoft.com to see how it finds out that the stream is expandable. At first time I don't see anything public and this is likely specific to memory streams.

    Not sure what is your overall goal ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Proposed as answer by Jesse Houwing Friday, October 15, 2010 3:22 PM
    • Marked as answer by Figo Fei Monday, October 25, 2010 3:11 AM
    Friday, October 15, 2010 1:13 PM
  • The only way is to use the SetLength method with a value larger than the allocated capacity and catch the NotSupportedException if you want to find out through a publicly supported mechanism.

    Or use private reflection to get the following field:

    private bool _expandable;

    • Proposed as answer by Jesse Houwing Friday, October 15, 2010 3:22 PM
    • Marked as answer by Figo Fei Monday, October 25, 2010 3:11 AM
    Friday, October 15, 2010 1:23 PM
  • This is private. My personal preference would be likely to just let the exception to happen (providing a non expandable stream when an expandable one is needed just seems a programming error).

    Else you'll have to catch the exception and do whatever you want. As usual knowing the overall goal could help to raise suggestions...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Friday, October 15, 2010 2:52 PM
  • Hi,

    Suppose we create a memory stream using a byte array as follows:

    byte

     

     

    [] bs = new byte[8];

     

    new MemoryStream(bs, true);

    This creates a writable, non-expandable MemoryStream.

    Is it possible to find out from the memoryStream object whether the stream is expandable or not?

    Thanks

     

     

     

    memoryStream =

     

    One way is to derive a new stream class from MemoryStream.

    In there if user calls the constructor overload that takes a Byte[] array, then you can set an internal private flag "expandable = false".

    then provide a new property "Exapandable" with just a get accessor.

    Then in your code always use this new class, call it MemoryStreamer or something.

    No reflection, no exception stuff !

    Cap'n

     

     

    Friday, October 15, 2010 9:30 PM