Table of Contents

Class MemoryStream

Namespace
System.IO
Assembly
System.IO.Streams.dll

Creates a stream whose backing store is memory.

public class MemoryStream : Stream, IDisposable
Inheritance
MemoryStream
Implements
Inherited Members
Extension Methods

Constructors

MemoryStream()

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

public MemoryStream()

Remarks

The CanRead, CanSeek, and CanWrite properties are all set to true.

The capacity of the current stream automatically increases when you use the SetLength(long) method to set the length to a value larger than the capacity of the current stream.

MemoryStream(byte[])

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

public MemoryStream(byte[] buffer)

Parameters

buffer byte[]

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

Remarks

The CanRead, CanSeek, and CanWrite properties are all set to true.

The capacity of the current stream automatically increases when you use the SetLength(long) method to set the length to a value larger than the capacity of the current stream.

Exceptions

ArgumentNullException

buffer is null.

Properties

CanRead

Gets a value indicating whether the current stream supports reading.

public override bool CanRead { get; }

Property Value

bool

/// true if the stream is open.///

Remarks

If a class derived from Stream does not support reading, calls to the Read(SpanByte) and ReadByte() methods throw a NotSupportedException.

If the stream is closed, this property returns false.

CanSeek

Gets a value indicating whether the current stream supports seeking.

public override bool CanSeek { get; }

Property Value

bool

true if the stream is open.

Remarks

If a class derived from Stream does not support reading, calls to the Length, SetLength(long), Position and Seek(long, SeekOrigin) throw a NotSupportedException.

If the stream is closed, this property returns false.

CanWrite

Gets a value indicating whether the current stream supports writing.

public override bool CanWrite { get; }

Property Value

bool

true if the stream supports writing; otherwise, false.

Remarks

If a class derived from Stream does not support reading, calls to the SetLength(long) and Write(byte[], int, int) or WriteByte(byte) methods throw a NotSupportedException.

If the stream is closed, this property returns false.

Length

When overridden in a derived class, gets the length in bytes of the stream.

public override long Length { get; }

Property Value

long

A long value representing the length of the stream in bytes.

Position

When overridden in a derived class, gets or sets the position within the current stream.

public override long Position { get; set; }

Property Value

long

The current position within the stream.

Exceptions

IOException

Can't adjust position out of the buffer size for fixed size buffer

ArgumentOutOfRangeException

Position can't be negative or higher than the stream allocated size.

Methods

Dispose(bool)

Releases the unmanaged resources used by the Stream and optionally releases the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

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

Flush()

When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.

public override void Flush()

GetBuffer()

Returns the array of unsigned bytes from which this stream was created.

public virtual byte[] GetBuffer()

Returns

byte[]

The byte array from which this stream was created, or the underlying array if a byte array was not provided to the MemoryStream constructor during construction of the current instance.

Remarks

Note that the buffer contains allocated bytes which might be unused. For example, if the string "test" is written into the MemoryStream object, the length of the buffer returned from GetBuffer() is 256, not 4, with 252 bytes unused. To obtain only the data in the buffer, use the ToArray() method; however, ToArray() creates a copy of the data in memory.

The buffer can also be null.

Read(byte[], int, int)

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

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

Parameters

buffer byte[]

An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

offset int

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

count int

The maximum number of bytes to be read from the current stream.

Returns

int

The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

Exceptions

ObjectDisposedException

The current stream instance is closed.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentException

offset subtracted from the buffer length is less than count.

Read(SpanByte)

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public override int Read(SpanByte buffer)

Parameters

buffer SpanByte

A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source.

Returns

int

The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

Exceptions

ObjectDisposedException

The current stream instance is closed.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentException

offset subtracted from the buffer length is less than count.

ReadByte()

Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.

public override int ReadByte()

Returns

int

The unsigned byte cast to an Int32, or -1 if at the end of the stream.

Exceptions

ObjectDisposedException

The current stream instance is closed.

Seek(long, SeekOrigin)

When overridden in a derived class, sets the position within the current stream.

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long

A byte offset relative to the origin parameter.

origin SeekOrigin

A value of type SeekOrigin indicating the reference point used to obtain the new position.

Returns

long

The new position within the current stream.

Exceptions

ObjectDisposedException
ArgumentOutOfRangeException

offset is greater than MaxValue.

IOException

Seeking is attempted before the beginning of the stream.

ArgumentException

There is an invalid SeekOrigin. -or- offset caused an arithmetic overflow.

SetLength(long)

When overridden in a derived class, sets the length of the current stream.

public override void SetLength(long value)

Parameters

value long

The desired length of the current stream in bytes.

ToArray()

public virtual byte[] ToArray()

Returns

byte[]

Write(byte[], int, int)

When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

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

Parameters

buffer byte[]

An array of bytes. This method copies count bytes from buffer to the current stream.

offset int

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

count int

The number of bytes to be written to the current stream.

WriteByte(byte)

Writes a byte to the current position in the stream and advances the position within the stream by one byte.

public override void WriteByte(byte value)

Parameters

value byte

The byte to write to the stream.

WriteTo(Stream)

Writes this MemoryStream to another stream.

public virtual void WriteTo(Stream stream)

Parameters

stream Stream

Stream to write into.

Exceptions

ArgumentNullException

If stream is null.