[Mono-dev] Should we replace MemoryStream?

Robert Jordan robertj at gmx.net
Tue Nov 10 08:48:46 EST 2009

Leszek Ciesielski wrote:
> Choice is not always good, and I think this is one of the cases when 
> the default (i.e. the MemoryStream implementation) should make the 
> choices instead presenting them to the user. Though I agree that the 
> case of constructing a MemoryStream from an existing byte[] would 
> require a special path in the code, as this is a stream that most 
> likely won't be resized and in this case users are expecting the 
> constructor to have a complexity of O(1) and GetBuffer to also be 
> O(1). The same expectation is probably also true with a fixed size 
> MemoryStream.

MemoryStream.GetBuffer's docs indirectly suggest that no copy
will be performed:

"Note that the buffer contains allocated bytes which might be unused.
For example, if the string "test" is written into the MemoryStream
object, the length of the buffer returned from GetBuffer is 256, not 4,
with 252 bytes unused. To obtain only the data in the buffer, use the
ToArray method; however, ToArray creates a copy of the data in memory."

So MemoryStream.GetBuffer must remain an O(1) operation in any case,
defeating any kind of optimization a chunked memory stream
implementation may introduce.


More information about the Mono-devel-list mailing list