[Serializable] |
The current position of a stream is the position at which the next read or write operation could take place. The current position can be retrieved or set through the MemoryStream.Seek method. When a new instance of MemoryStream is created, the current position is set to zero.
Memory streams created with an unsigned byte array provide a non-resizable stream view of the data, and can only be written to. When using a byte array, you can neither append to nor shrink the stream, although you might be able to modify the existing contents depending on the parameters passed into the constructor. Empty memory streams are resizable, and can be written to and read from.
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero. |
ctor #2 | Overloaded:.ctor(byte[] buffer) Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array. |
ctor #3 | Overloaded:.ctor(int capacity) Initializes a new instance of the MemoryStream class with an expandable capacity initialized as specified. |
ctor #4 | Overloaded:.ctor(byte[] buffer, bool writable) Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array with the MemoryStream.CanWrite property set as specified. |
ctor #5 | Overloaded:.ctor(byte[] buffer, int index, int count) Initializes a new non-resizable instance of the MemoryStream class based on the specified region (index) of a byte array. |
ctor #6 | Overloaded:.ctor(byte[] buffer, int index, int count, bool writable) Initializes a new non-resizable instance of the MemoryStream class based on the specified region of a byte array, with the MemoryStream.CanWrite property set as specified. |
ctor #7 | Overloaded:.ctor(byte[] buffer, int index, int count, bool writable, bool publiclyVisible) Initializes a new instance of the MemoryStream class based on the specified region of a byte array, with the MemoryStream.CanWrite property set as specified, and the ability to call MemoryStream.GetBuffer set as specified. |
CanRead | Read-only Overridden: Gets a value indicating whether the current stream supports reading. |
CanSeek | Read-only Overridden: Gets a value indicating whether the current stream supports seeking. |
CanWrite | Read-only Overridden: Gets a value indicating whether the current stream supports writing. |
Capacity | Read-write Gets or sets the number of bytes allocated for this stream. |
Length | Read-only Overridden: Gets the length of the stream in bytes . |
Position | Read-write Overridden: Gets or sets the current position within the stream. |
BeginRead (inherited from System.IO.Stream) |
See base class member description: System.IO.Stream.BeginRead Begins an asynchronous read operation. |
BeginWrite (inherited from System.IO.Stream) |
See base class member description: System.IO.Stream.BeginWrite Begins an asynchronous write operation. |
Close | Overridden: Closes the stream for reading and writing. |
CreateObjRef (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. |
EndRead (inherited from System.IO.Stream) |
See base class member description: System.IO.Stream.EndRead Waits for the pending asynchronous read to complete. |
EndWrite (inherited from System.IO.Stream) |
See base class member description: System.IO.Stream.EndWrite Ends an asynchronous write operation. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
Flush | Overridden: Overrides Stream.Flush so that no action is performed. |
GetBuffer | Returns the array of unsigned bytes from which this stream was created. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
InitializeLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. |
Read | Overridden: Reads a block of bytes from the current stream and writes the data to buffer. |
ReadByte | Overridden: Reads a byte from the current stream. |
Seek | Overridden: Sets the position within the current stream to the specified value. |
SetLength | Overridden: Sets the length of the current stream to the specified value. |
ToArray | Writes the entire stream contents to a byte array, regardless of the MemoryStream.Position property. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Write | Overridden: Writes a block of bytes to the current stream using data read from buffer. |
WriteByte | Overridden: Writes a byte to the current stream at the current position. |
WriteTo | Writes the entire contents of this memory stream to another stream. |
CreateWaitHandle (inherited from System.IO.Stream) |
See base class member description: System.IO.Stream.CreateWaitHandle Allocates a WaitHandle object. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public MemoryStream(); |
The capacity of the current stream automatically increases when you use the MemoryStream.SetLength method to set the length to a value larger than the capacity of the current stream.
This constructor exposes the underlying stream, which MemoryStream.GetBuffer returns.
public MemoryStream( |
buffer
Exception Type | Condition |
---|---|
ArgumentNullException | buffer is null. |
The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see MemoryStream.SetLength).
This constructor does not expose the underlying stream. MemoryStream.GetBuffer throws UnauthorizedAccessException.
public MemoryStream( |
capacity
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | capacity is negative. |
The capacity automatically increases when you use the MemoryStream.SetLength method to set the length to a value larger than the capacity of the current stream. Except for a MemoryStream constructed with a byte[] parameter, write operations at the end of a MemoryStream expand the MemoryStream.
This constructor exposes the underlying stream that MemoryStream.GetBuffer returns.
buffer
writable
Exception Type | Condition |
---|---|
ArgumentNullException | buffer is null. |
The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see MemoryStream.SetLength).
This constructor does not expose the underlying stream. MemoryStream.GetBuffer throws UnauthorizedAccessException.
buffer
index
count
Exception Type | Condition |
---|---|
ArgumentNullException | buffer is null. |
ArgumentOutOfRangeException | index or count is less than zero. |
ArgumentException | The sum of index and count is greater than the length of buffer. |
The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see MemoryStream.SetLength).
This constructor does not expose the underlying stream. MemoryStream.GetBuffer throws UnauthorizedAccessException. However, you can write to the stream.
buffer
index
count
writable
Exception Type | Condition |
---|---|
ArgumentNullException | buffer is null. |
ArgumentOutOfRangeException | index or count are negative. |
ArgumentException | The sum of index and count is greater than the length of buffer. |
The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see MemoryStream.SetLength).
This constructor does not expose the underlying stream. MemoryStream.GetBuffer throws UnauthorizedAccessException. However, you can write to the stream.
public MemoryStream( |
buffer
index
count
writable
publiclyVisible
Exception Type | Condition |
---|---|
ArgumentNullException | buffer is null. |
ArgumentOutOfRangeException | index or count is negative. |
ArgumentException | The buffer length minus index is less than count. |
The new stream instance can be written to, but the MemoryStream.Capacity of the underlying byte array cannot be changed. The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see MemoryStream.SetLength).
public override bool CanRead {get;}
|
If the stream is closed, this property returns false.
public override bool CanSeek {get;}
|
If the stream is closed, this property returns false.
public override bool CanWrite {get;}
|
If the stream is closed, this property returns false.
public virtual int Capacity {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | A capacity is set that is negative or less than the current length of the stream. |
NotSupportedException | set is invoked on a stream that is closed or whose capacity cannot be modified. |
public override long Length {get;}
|
Exception Type | Condition |
---|---|
IOException | The stream is closed. |
ObjectDisposedException | Methods were called after the stream was closed. |
public override long Position {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The position is set to a negative value. |
IOException | The stream is closed. |
ObjectDisposedException | Methods were called after the stream was closed. |
public virtual IAsyncResult BeginRead( |
buffer
offset
count
callback
state
Exception Type | Condition |
---|---|
IOException | Attempted an asynchronous read past the end of the file, or a disk error occurs. |
ArgumentException | One or more or the arguments is invalid. |
ObjectDisposedException | Methods were called after the stream was closed. |
NotSupportedException | The current Stream implementation does not support the read operation. |
The current position in the stream is updated when the asynchronous read or write is issued, not when the I/O operation completes.
Multiple simultaneous asynchronous requests render the request completion order uncertain.
Use the Stream.CanRead property to determine whether the current instance supports reading.
If a stream is closed or you pass an invalid argument, exceptions are thrown immediately from BeginRead. Errors that occur during an asynchronous read request, such as a disk failure during the I/O request, occur on the threadpool thread and become visible upon a call to EndRead. Exceptions thrown by the threadpool thread will not be visible when calling Stream.EndWrite.
public virtual IAsyncResult BeginWrite( |
buffer
offset
count
callback
state
Exception Type | Condition |
---|---|
IOException | Attempted an asynchronous write past the end of the file, or a disk error occurs. |
ArgumentException | One or more or the arguments is invalid. |
ObjectDisposedException | Methods were called after the stream was closed. |
NotSupportedException | The current Stream implementation does not support the write operation. |
If a stream is writable, writing at the end of the stream expands the stream.
The current position in the stream is updated when you issue the asynchronous read or write, not when the I/O operation completes. Multiple simultaneous asynchronous requests render the request completion order uncertain.
Use the Stream.CanWrite property to determine whether the current instance supports writing.
If a stream is closed or you pass an invalid argument, exceptions are thrown immediately from BeginWrite. Errors that occur during an asynchronous write request, such as a disk failure during the I/O request, occur on the threadpool thread and become visible upon a call to EndWrite. Exceptions thrown by the threadpool thread will not be visible when calling EndWrite.
public override void Close(); |
The buffer is still available on a MemoryStream once the stream has been closed.
Flushing the stream will not flush its underlying encoder unless you explicitly call MemoryStream.Flush or Close. Setting StreamWriter.AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.
Attempting to manipulate a stream after it has been closed might throw an ObjectDisposedException.
requestedType
Exception Type | Condition |
---|---|
RemotingException | This instance is not a valid remoting object. |
protected virtual WaitHandle CreateWaitHandle(); |
Use this method if you implement the asynchronous methods and require a way of blocking in Stream.EndRead or Stream.EndWrite until the asynchronous operation is complete.
public virtual int EndRead( |
asyncResult
Exception Type | Condition |
---|---|
ArgumentNullException | asyncResult is null. |
ArgumentException | asyncResult did not originate from a Stream.BeginRead method on the current stream. |
EndRead can be called once on every IAsyncResult from Stream.BeginRead.
This method blocks until the I/O operation has completed.
public virtual void EndWrite( |
asyncResult
Exception Type | Condition |
---|---|
ArgumentNullException | asyncResult is null. |
ArgumentException | asyncResult did not originate from a Stream.BeginWrite method on the current stream. |
This method blocks until the I/O operation has completed. Errors that occur during an asynchronous write request, such as a disk failure during the I/O request, occur on the threadpool thread and become visible upon a call to EndWrite. Exceptions thrown by the threadpool thread will not be visible when calling EndWrite.
~MemoryStream(); |
public override void Flush(); |
Since any data written to a MemoryStream is written into RAM, this method is redundant.
public virtual byte[] GetBuffer(); |
Exception Type | Condition |
---|---|
UnauthorizedAccessException | The MemoryStream instance was not created with publicallyVisible set to true in its constructor. |
public virtual int GetHashCode(); |
public object GetLifetimeService(); |
public Type GetType(); |
public virtual object InitializeLifetimeService(); |
public class MyClass : MarshalByRefObject { public override Object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(1); lease.SponsorshipTimeout = TimeSpan.FromMinutes(2); lease.RenewOnCallTime = TimeSpan.FromSeconds(2); } return lease; } }
protected object MemberwiseClone(); |
buffer
offset
count
Exception Type | Condition |
---|---|
ArgumentNullException | buffer is null. |
ArgumentOutOfRangeException | offset or count is negative. |
ArgumentException | offset subtracted from the buffer length is less than count. |
ObjectDisposedException | The current stream instance is closed. |
The offset parameter gives the offset of the first byte in buffer to which data from the current stream is written. The count parameter gives the maximum number of bytes to read from the current stream. The returned value is the actual number of bytes read, or zero if the end of the stream is reached.
If the read operation is successful, the current position within the stream advances by the number of bytes read. If an exception occurs, the current position within the stream remains unchanged.
The Read method will return zero only if the end of the stream is reached. In all other cases, Read always reads at least one byte from the stream before returning. By definition, if no data is available from the stream upon a call to Read, the Read method returns zero (the end of the stream is reached automatically). An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.
Use BinaryReader for reading primitive data types.
If the byte array specified in the buffer parameter is the underlying buffer returned by the MemoryStream.GetBuffer method, the array contents are overwritten, and no exception is thrown.
public override int ReadByte(); |
Exception Type | Condition |
---|---|
ObjectDisposedException | The current stream instance is closed. |
If the read operation is successful, the current position within the stream is advanced by one byte. If an exception occurs, the current position within the stream is unchanged.
public override long Seek(long offset, Seek( |
offset
loc
Exception Type | Condition |
---|---|
IOException | Seeking is attempted before the beginning. |
ArgumentOutOfRangeException | offset is greater than the maximum length of the MemoryStream. |
ArgumentException | There is an invalid SeekOrigin. |
ObjectDisposedException | The current stream instance is closed. |
Seeking to any location beyond the length of the stream is supported.
public override void SetLength( |
value
Exception Type | Condition |
---|---|
NotSupportedException | The current stream is not resizable and value is larger than the current capacity. -or- The current stream does not support writing. |
ArgumentOutOfRangeException | value is negative or is greater than the maximum length of the MemoryStream, where the maximum length is( Int32.MaxValue - origin), and origin is the index into the underlying buffer at which the stream starts. |
If the specified value is less than the current length of the stream, the stream is truncated. If after the truncation the current position within the stream is past the end of the stream, the MemoryStream.ReadByte method returns -1, the MemoryStream.Read method reads zero bytes into the provided byte array, and MemoryStream.Write and MemoryStream.WriteByte methods append specified bytes at the end of the stream, increasing its length. If the specified value is larger than the current capacity and the stream is resizable, the capacity is increased, and the current position within the stream is unchanged. If the length is increased, the contents of the stream between the old and the new length are initialized to zeros.
public virtual byte[] ToArray(); |
public virtual string ToString(); |
buffer
offset
count
Exception Type | Condition |
---|---|
ArgumentNullException | buffer is null. |
NotSupportedException | The stream does not support writing. For additional information see Stream.CanWrite. -or- The current position is closer than count bytes to the end of the stream, and the capacity cannot be modified. |
ArgumentException | offset subtracted from the buffer length is less than count. |
ArgumentOutOfRangeException | offset or count are negative. |
IOException | An I/O error occurs. |
ObjectDisposedException | The current stream instance is closed. |
The offset parameter gives the offset of the first byte in buffer to write from, and the count parameter gives the number of bytes to write. If the write operation is successful, the current position within the stream is advanced by the number of bytes written. If an exception occurs, the current position within the stream is unchanged.
Except for a MemoryStream constructed with a byte[] parameter, write operations at the end of a MemoryStream expand the MemoryStream.
public override void WriteByte( |
value
Exception Type | Condition |
---|---|
NotSupportedException | The stream does not support writing. For additional information see Stream.CanWrite. -or- The current position is closer than count bytes to the end of the stream, and the capacity cannot be modified. |
ObjectDisposedException | The current stream is closed. |
Except for a MemoryStream constructed with a byte[] parameter, write operations at the end of a MemoryStream expand the MemoryStream.
public virtual void WriteTo( |
stream
Exception Type | Condition |
---|---|
ArgumentNullException | stream is null. |
ObjectDisposedException | The current or target stream is closed. |