MemoryStream |
IObject | |
Stream |
Name | Description |
---|---|
BufferMode | The method a specified byte array should be referenced. |
Name | Description |
---|---|
CanRead (get) | Gets value indicating whether the current stream supports reading. |
CanSeek (get) | Gets value indicating whether the current stream supports seeking. |
CanTimeout (get) | Gets if the MemoryStream object supports timeout events. |
CanWrite (get) | Gets value indicating whether the current stream supports writing. |
Capacity (get) | Gets the number of bytes allocated for this stream. |
Capacity (let) | Sets the number of bytes allocated for this stream. |
Length (get) | Gets the length of the stream in bytes. |
Position (get) | Returns the current position within the stream. |
Position (let) | Sets the current position within the stream. |
ReadTimeout (get) | Gets the timeout duration for a read operation. |
ReadTimeout (let) | Sets the timeout duration for a read operation. |
WriteTimeout (get) | Gets the timeout duration of a write operation. |
WriteTimeout (let) | Sets the timeout duration for a write operation. |
Name | Description |
---|---|
BeginRead | Begins an asynchronous buffer read. For a MemoryStream, calling an asynchronous buffer read is identical to calling ReadBlock. |
BeginWrite | Begins an asynchronous buffer write. For a MemoryStream, having an asynchronous buffer write is of no use. |
CloseStream | Closes the MemoryStream and releases any resources allocated. |
CopyTo | Reads the bytes from the current stream and writes them to another stream. |
EndRead | Signifies the end of an asynchronous read from the stream. |
EndWrite | Signifies the end of an asynchronous write to the stream. |
Equals | Returns a boolean indicating if the value and this object instance are the same instance. |
Flush | This has no purpose in a MemoryStream |
GetHashCode | Returns a pseudo-unique number identifying this instance. |
ReadBlock | Reads a specifiec number of bytes from the stream. |
ReadByte | Reads a single byte from the stream. |
SeekPosition | Sets the position within the current stream to the specified value. |
SetLength | Sets the current length of the stream. |
ToArray | Writes the stream contents to a byte array, regardless of the Position property. |
ToString | Returns a string representation of this object instance. |
WriteBlock | Writes an array of bytes to the underlying stream. |
WriteByte | Writes a single byte to the underlying stream. |
WriteTo | Writes the entire contents of this memory stream to another stream. |
The MemoryStream uses an array of bytes to back the data in memory. When the object is first created, no array is allocated until the first time the data must be accessed. The default capacity of the array is 4096 bytes. The capacity will grow by doubling the size of the current capacity. If The capacity needs to be larger, consider setting the Capacity directly to prevent the array from having to be resized as new data is written to the stream.
If the MemoryStream is created using a supplied Byte array, then no internal array will be allocated. The MemoryStream will access the supplied Byte array as if it were created by the MemoryStream object. As the underlying array is modified, the changes can be seen by directly accessing the array. Also, by modifying the array directly, the underlying stream will reflect the new data.
The Currency type is used for consistency across all stream objects. Internally, the MemoryStream only supports 2 ^ 31 bytes. Other streams may support more.
If a Byte array is being supplied to the MemoryStream and Mode is ShareMode, then the array must out-live the MemoryStream object. It is the responsibility of the user to destroy the array after the stream has been destroyed. The MemoryStream object maintains a reference to the same array in memory, but does not deallocate the array when finished. While the object is alive, the array is locked to prevent it from being ReDimed or Erased. Closing the stream will release the lock and reference to the array, at which point the user has full control of the array.
The following code example shows how to read and write data using memory as a backing store.
Public Sub Main() Dim FirstString() As Byte Dim SecondString() As Byte Dim ByteArray() As Byte Dim CharArray() As Integer Dim UniEncoding As New UnicodeEncoding Dim MemStream As New MemoryStream Dim Count As Long Dim i As Long FirstString = UniEncoding.GetBytes("Invalid file path characters are:") SecondString = UniEncoding.GetBytes(Path.GetInvalidPathChars) MemStream.WriteBlock FirstString, 0, CorArray.Length(FirstString) For i = LBound(SecondString) To UBound(SecondString) MemStream.WriteByte SecondString(i) Next Debug.Print CorString.Format(t("Capacity = {0}, Length = {1}, Position = {2}\n"), MemStream.Capacity, MemStream.Length, MemStream.Position) MemStream.SeekPosition 0, SeekOrigin.FromBeginning ReDim ByteArray(0 To MemStream.Length - 1) Count = MemStream.ReadBlock(ByteArray, 0, 20) Do While Count < MemStream.Length ByteArray(Count) = MemStream.ReadByte Count = Count + 1 Loop ReDim CharArray(0 To UniEncoding.GetCharCount(ByteArray, 0, Count) - 1) UniEncoding.GetDecoder.GetChars ByteArray, 0, Count, CharArray, 0 Debug.Print NewString(CharArray) End Sub ' The example code produces the following output. ' ' Capacity = 256, Length = 138, Position = 138 ' ' Invalid file path characters are:"<>| ' '