MemoryStream Class

Creates a stream whose backing store is memory.

Syntax

Declaration

[Serializable]
[ComVisible(true)]
public class MemoryStream : Stream, IDisposable

Inheritance Hierarchy

Constructors summary

Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.

Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array.

Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array with the CanWrite property set as specified.

Initializes a new non-resizable instance of the MemoryStream class based on the specified region (index) of a byte array.

Initializes a new non-resizable instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified.

Initializes a new instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified, and the ability to call GetBuffer() set as specified.

Initializes a new instance of the MemoryStream class with an expandable capacity initialized as specified.

Properties summary

Gets a value indicating whether the current stream supports reading.

Gets a value indicating whether the current stream supports seeking.

Gets a value indicating whether the current stream supports writing.

Gets or sets the number of bytes allocated for this stream.

Gets the length of the stream in bytes.

Gets or sets the current position within the stream.

Methods summary

Asynchronously reads all the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token.

Releases the unmanaged resources used by the MemoryStream class and optionally releases the managed resources.

Overrides the Flush() method so that no action is performed.

Asynchronously clears all buffers for this stream, and monitors cancellation requests.

Reads a block of bytes from the current stream and writes the data to a buffer.

Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

Reads a byte from the current stream.

Sets the position within the current stream to the specified value.

Sets the length of the current stream to the specified value.

Writes the stream contents to a byte array, regardless of the Position property.

Returns the array of unsigned bytes from which this stream was created. The return value indicates whether the conversion succeeded.

Writes a block of bytes to the current stream using data read from a buffer.

Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

Writes a byte to the current stream at the current position.

Writes the entire contents of this memory stream to another stream.

Constructors

  • MemoryStream()

    Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.

    public MemoryStream()
  • MemoryStream(Byte[])

    Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array.

    public MemoryStream(byte[] buffer)

    Parameters

    • buffer

      The array of unsigned bytes from which to create the current stream.

    Exceptions

  • MemoryStream(Byte[], Boolean)

    Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array with the CanWrite property set as specified.

    public MemoryStream(byte[] buffer, bool writable)

    Parameters

    • buffer

      The array of unsigned bytes from which to create this stream.

    • writable

      The setting of the CanWrite property, which determines whether the stream supports writing.

    Exceptions

  • MemoryStream(Byte[], Int32, Int32)

    Initializes a new non-resizable instance of the MemoryStream class based on the specified region (index) of a byte array.

    public MemoryStream(byte[] buffer, int index, int count)

    Parameters

    • buffer

      The array of unsigned bytes from which to create this stream.

    • index

      The index into buffer at which the stream begins.

    • count

      The length of the stream in bytes.

    Exceptions

  • MemoryStream(Byte[], Int32, Int32, Boolean)

    Initializes a new non-resizable instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified.

    public MemoryStream(byte[] buffer, int index, int count, bool writable)

    Parameters

    • buffer

      The array of unsigned bytes from which to create this stream.

    • index

      The index in buffer at which the stream begins.

    • count

      The length of the stream in bytes.

    • writable

      The setting of the CanWrite property, which determines whether the stream supports writing.

    Exceptions

  • MemoryStream(Byte[], Int32, Int32, Boolean, Boolean)

    Initializes a new instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified, and the ability to call GetBuffer() set as specified.

    public MemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible)

    Parameters

    • buffer

      The array of unsigned bytes from which to create this stream.

    • index

      The index into buffer at which the stream begins.

    • count

      The length of the stream in bytes.

    • writable

      The setting of the CanWrite property, which determines whether the stream supports writing.

    • publiclyVisible

      true to enable GetBuffer(), which returns the unsigned byte array from which the stream was created; otherwise, false.

    Exceptions

  • MemoryStream(Int32)

    Initializes a new instance of the MemoryStream class with an expandable capacity initialized as specified.

    public MemoryStream(int capacity)

    Parameters

    • capacity

      The initial size of the internal array in bytes.

    Exceptions

Properties

  • CanRead

    Gets a value indicating whether the current stream supports reading.

    public override bool CanRead { get; }

    Property Value

    • true if the stream is open.

    Overrides

  • CanSeek

    Gets a value indicating whether the current stream supports seeking.

    public override bool CanSeek { get; }

    Property Value

    • true if the stream is open.

    Overrides

  • CanWrite

    Gets a value indicating whether the current stream supports writing.

    public override bool CanWrite { get; }

    Property Value

    • true if the stream supports writing; otherwise, false.

    Overrides

  • Capacity

    Gets or sets the number of bytes allocated for this stream.

    public virtual int Capacity { get; set; }

    Property Value

    • The length of the usable portion of the buffer for the stream.

    Exceptions

  • Length

    Gets the length of the stream in bytes.

    public override long Length { get; }

    Property Value

    • The length of the stream in bytes.

    Exceptions

    Overrides

  • Position

    Gets or sets the current position within the stream.

    public override long Position { get; set; }

    Property Value

    • The current position within the stream.

    Exceptions

    Overrides

Methods

  • CopyToAsync(Stream, Int32, CancellationToken)

    Asynchronously reads all the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token.

    public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)

    Parameters

    • destination

      The stream to which the contents of the current stream will be copied.

    • bufferSize

      The size, in bytes, of the buffer. This value must be greater than zero.

    • cancellationToken

      The token to monitor for cancellation requests.

    Returns

    • A task that represents the asynchronous copy operation.

    Exceptions

    Overrides

  • Dispose(Boolean)

    Releases the unmanaged resources used by the MemoryStream class and optionally releases the managed resources.

    protected override void Dispose(bool disposing)

    Parameters

    • disposing

      true to release both managed and unmanaged resources; false to release only unmanaged resources.

    Overrides

  • Flush()

    Overrides the Flush() method so that no action is performed.

    public override void Flush()

    Overrides

  • FlushAsync(CancellationToken)

    Asynchronously clears all buffers for this stream, and monitors cancellation requests.

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task FlushAsync(CancellationToken cancellationToken)

    Parameters

    • cancellationToken

      The token to monitor for cancellation requests.

    Returns

    • A task that represents the asynchronous flush operation.

    Exceptions

    Overrides

  • Read(Byte[], Int32, Int32)

    Reads a block of bytes from the current stream and writes the data to a buffer.

    public override int Read(byte[] buffer, int offset, int count)

    Parameters

    • buffer

      When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the characters read from the current stream.

    • offset

      The zero-based byte offset in buffer at which to begin storing data from the current stream.

    • count

      The maximum number of bytes to read.

    Returns

    • The total number of bytes written into the buffer. This can be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached before any bytes are read.

    Exceptions

    Overrides

  • ReadAsync(Byte[], Int32, Int32, CancellationToken)

    Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

    Parameters

    • buffer

      The buffer to write the data into.

    • offset

      The byte offset in buffer at which to begin writing data from the stream.

    • count

      The maximum number of bytes to read.

    • cancellationToken

      The token to monitor for cancellation requests. The default value is None.

    Returns

    • A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.

    Exceptions

    Overrides

  • ReadByte()

    Reads a byte from the current stream.

    public override int ReadByte()

    Returns

    • The byte cast to a Int32, or -1 if the end of the stream has been reached.

    Exceptions

    Overrides

  • Seek(Int64, SeekOrigin)

    Sets the position within the current stream to the specified value.

    public override long Seek(long offset, SeekOrigin loc)

    Parameters

    • offset

      The new position within the stream. This is relative to the loc parameter, and can be positive or negative.

    • loc

      A value of type SeekOrigin, which acts as the seek reference point.

    Returns

    • The new position within the stream, calculated by combining the initial reference point and the offset.

    Exceptions

    Overrides

  • SetLength(Int64)

    Sets the length of the current stream to the specified value.

    public override void SetLength(long value)

    Parameters

    • value

      The value at which to set the length.

    Exceptions

    • The current stream is not resizable and value is larger than the current capacity.

      -or-

      The current stream does not support writing.

    • value is negative or is greater than the maximum length of the MemoryStream, where the maximum length is(MaxValue - origin), and origin is the index into the underlying buffer at which the stream starts.

    Overrides

  • ToArray()

    Writes the stream contents to a byte array, regardless of the Position property.

    public virtual byte[] ToArray()

    Returns

    • A new byte array.

  • TryGetBuffer(out ArraySegment<Byte>)

    Returns the array of unsigned bytes from which this stream was created. The return value indicates whether the conversion succeeded.

    public virtual bool TryGetBuffer(out ArraySegment<byte> buffer)

    Parameters

    • buffer

      The byte array segment from which this stream was created.

    Returns

    • true if the conversion was successful; otherwise, false.

  • Write(Byte[], Int32, Int32)

    Writes a block of bytes to the current stream using data read from a buffer.

    public override void Write(byte[] buffer, int offset, int count)

    Parameters

    • buffer

      The buffer to write data from.

    • offset

      The zero-based byte offset in buffer at which to begin copying bytes to the current stream.

    • count

      The maximum number of bytes to write.

    Exceptions

    Overrides

  • WriteAsync(Byte[], Int32, Int32, CancellationToken)

    Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

    Parameters

    • buffer

      The buffer to write data from.

    • offset

      The zero-based byte offset in buffer from which to begin copying bytes to the stream.

    • count

      The maximum number of bytes to write.

    • cancellationToken

      The token to monitor for cancellation requests. The default value is None.

    Returns

    • A task that represents the asynchronous write operation.

    Exceptions

    Overrides

  • WriteByte(Byte)

    Writes a byte to the current stream at the current position.

    public override void WriteByte(byte value)

    Parameters

    • value

      The byte to write.

    Exceptions

    Overrides

  • WriteTo(Stream)

    Writes the entire contents of this memory stream to another stream.

    public virtual void WriteTo(Stream stream)

    Parameters

    • stream

      The stream to write this memory stream to.

    Exceptions

Details

Namespace

System.IO

Assembly

System.IO.dll