The Artima Developer Community
Sponsored Link

.NET Buzz Forum
[Tip]:MemoryStream.GetBuffer() vs. MemoryStream.ToArray()

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Roy Osherove

Posts: 1807
Nickname: royo
Registered: Sep, 2003

Roy Osherove is a .Net consultant based in Israel
[Tip]:MemoryStream.GetBuffer() vs. MemoryStream.ToArray() Posted: Oct 6, 2003 9:13 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Roy Osherove.
Original Post: [Tip]:MemoryStream.GetBuffer() vs. MemoryStream.ToArray()
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Roy Osherove
Latest Posts From ISerializable

Advertisement

One of my readers posted a comment on my Site-To-RSS article that just solved me a bunch of trouble!

In the article, I was using a memory stream to write the XML into memory. I was using the MemoryStream.GetBuffer() method to retrieve the text inside the stream. This caused some ugly code because to get the buffer correctly, I had to know the length of the actual string in the stream, meaning the stream had to be open for me to know that. Even so, it didn't work very well and sometimes retrieved corrupted data.

So, TheCShark (huh?) solved all my problems in one fell swoop by posting one simple line:

Tip: use ToArray instead of GetBuffer, ToArray works when the MemoryStream is closed.

Amazing. All my problems are now solved:

  • No longer need to keep stream open
  • Calling code is much simpler:
    • Dim retval As String = Encoding.UTF8.GetString(ms.ToArray())

  • It works without a hitch!

So, we learned that we should always use ToArray() to get the value. Always. Phew. How silly of me not to notice that!

Thanks, CShark!

Read: [Tip]:MemoryStream.GetBuffer() vs. MemoryStream.ToArray()

Topic: Blogger's Dinner Previous Topic   Next Topic Topic: Blog Your Cat Day

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use