MemoryStream

MemoryStream


Provides an in-memory stream of data that can be read/written and modified using Stream methods.


Remarks

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.

CAUTION: If a Byte array is being supplied to the MemoryStream, then the array must outlive 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 Currency datatypes used are there for consistency across all stream objects. Internally, the MemoryStream only supports 2 ^ 31 bytes. Other streams may support more.

See Also

Project VBCorLib Overview | Constructors | Stream

Implements:

IObject 
Stream 

Public:

Properties:

NameDescription
 CanRead (get)Determines if the current MemoryStream can be read. 
 CanSeek (get)Determines if the current MemoryStream can seek positions within the stream. 
 CanTimeout (get)Returns if the MemoryStream object supports timeout events. 
 CanWrite (get)Determines if the current MemoryStream can be written to. 
 Capacity (get)Returns the current capacity of the internal stream buffer. 
 Capacity (let)Sets the current capacity for the internal stream buffer. 
 Length (get)Returns the current length of the stream. 
 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. 

Methods:

NameDescription
 BeginReadBegins an asynchronous buffer read. For a MemoryStream, calling an asynchronous buffer read is identical to calling ReadBlock. 
 BeginWriteBegins an asynchronous buffer write. For a MemoryStream, having an asynchronous buffer write is of no use. 
 CloseStreamCloses the MemoryStream and releases any resources allocated. 
 EndReadSignifies the end of an asynchronous read from the stream. 
 EndWriteSignifies the end of an asynchronous write to the stream. 
 EqualsReturns a boolean indicating if the value and this object instance are the same instance. 
 FlushThis has no purpose in a MemoryStream 
 GetHashCodeReturns a pseudo-unique number identifying this instance. 
 ReadBlockReads a specifiec number of bytes from the stream. 
 ReadByteReads a single byte from the stream. 
 SeekPositionChanges the position within the stream. 
 SetLengthSets the current length of the stream. 
 ToArrayReturns a byte array copy of the underlying byte stream. 
 ToStringReturns a string representation of this object instance. 
 WriteBlockWrites an array of bytes to the underlying stream. 
 WriteByteWrites a single byte to the underlying stream. 
 WriteToWrites the underlying stream to another stream.