Using Memory Streams

Memory streams are special objects that act like file streams but that work in memory, providing the ability to manipulate binary data. The following code creates a MemoryStream with 2 Kbytes capacity and puts in a string:

Dim ms As New MemoryStream(2048)Dim bs As New BinaryWriter(ms)bs.Write("Some text written as binary")bs.Close()ms.Close()

To retrieve data, you use a BinaryReader pointing to the MemoryStream as you saw in the paragraph for binary files. So, in this example, you can invoke ReadString as follows:

'The stream must be still openUsing br As New BinaryReader(ms)    If ms IsNot Nothing AndAlso ms.Length > 0 Then        Dim data As String = br.ReadString ...

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.