Be careful with .GetBuffer() on a MemoryStream in .NET

Watch out when using the .GetBuffer function on a MemoryStream in .NET. Recently we did a change in an application where we query an inbox for emails with attachments. Those attachments used to be saved to a file first and then send over the network as a base64 string. With the change we no longer save to a filestream but to a memorystream, skipping the unnecessary IO.

All went fine until we received a complaint that a user couldn’t open an extracted attachment. The file appeared to be corrupt. After some investigation we noticed that all of the attachments had a strange file size: 8kb, 16kb, 32kb, 64kb, … What happened? We used .GetBuffer to get the byte array of a memorystream. A memorystream automatically allocates a buffer and when using .GetBuffer you also retrieve the unused bytes. If you only need the used bytes you have to use the ToArray() function.

Strangely enough most of the documents (PDF) opened well in Acrobat Reader, even with the unnecessary/empty bytes.  Only when the empty buffer at the back of the file grew too large, we got an error. After trimming all of the unnecessary bytes at the back of these files all opened fine again.

MSDN describes this behaviour: http://msdn.microsoft.com/en-us/library/system.io.memorystream.getbuffer.aspx

Advertisement