MemoryStream Class creates a stream whose backing store is memory.
Imports System
Imports System.IO
Imports System.Text
Module MemStream
Sub Main()
Dim count As Integer
Dim byteArray As Byte()
Dim charArray As Char()
Dim uniEncoding As New UnicodeEncoding()
Dim firstString As Byte() = uniEncoding.GetBytes("this is a test: ")
Dim secondString As Byte() = uniEncoding.GetBytes("this is another test")
Dim memStream As New MemoryStream(100)
Try
memStream.Write(firstString, 0 , firstString.Length)
count = 0
While(count < secondString.Length)
memStream.WriteByte(secondString(count))
count += 1
End While
memStream.Seek(0, SeekOrigin.Begin)
byteArray = New Byte(CType(memStream.Length, Integer)){}
count = memStream.Read(byteArray, 0, 20)
While(count < memStream.Length)
byteArray(count) = Convert.ToByte(memStream.ReadByte())
count += 1
End While
charArray = New Char(uniEncoding.GetCharCount(byteArray, 0, count)){}
uniEncoding.GetDecoder().GetChars(byteArray, 0, count, charArray, 0)
Console.WriteLine(charArray)
Finally
memStream.Close()
End Try
End Sub
End Module
Related examples in the same category