According to .NET MemoryStream - Should I set the capacity?, it's preferred to set the initial capacity of a MemoryStream if you know it beforehand.
In my code, I'm receiving (from a third party library) a Stream object. This Stream
is wrapped around a GetObjectResponse
class.
Since this class will be disposed after the transaction is closed, I need to copy the received stream so I can continue using it afterwards.
The Stream
class exposes a Length property to determine the length of the stream, so as an initial step to begin the copying process I started writing:
Stream destination = new MemoryStream(response.ResponseStream.Length);
But the compiler throws an error, stating that MemoryStream
does not take a long
parameter, instead it takes an int
.
Why would they do this?
If MemoryStream
extends from Stream
, this means that it can have a long Length
. If it can have a long
size, why can't it take a long
initial capactiy?
I'm trying to avoid initializing the stream with no capacity, because I want to take advantage of the fact that I know how big the stream is going to be, but the constructor seems to be constraining this wish.
How can I approach this and why does MemoryStream
cannot take a long
capacity?
MemoryStream
does not support more than about 2GB of data right now. This is a current limitation. It does not have to exist in principle.
This is simply a case of a feature not implemented (yet).
Implementing a bigger stream is difficult on the current CLR which only supports arrays with up to about int.MaxValue
elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With