Class
ExpandableMemoryStream

A high-performance memory stream implementation optimized for large data. Uses segmented buffers to reduce large contiguous memory allocations.

Definition

Namespace:Telerik.Documents.Core.Data

Assembly:Telerik.Windows.Documents.Core.dll

Syntax:

cs-api-definition
public class ExpandableMemoryStream : Stream, IDisposable, IAsyncDisposable

Inheritance: objectMarshalByRefObjectStreamExpandableMemoryStream

Implements: IAsyncDisposableIDisposable

Inherited Members Stream.NullStream.CopyTo(Stream)Stream.CopyTo(Stream, int)Stream.CopyToAsync(Stream)Stream.CopyToAsync(Stream, int)Stream.CopyToAsync(Stream, CancellationToken)Stream.CopyToAsync(Stream, int, CancellationToken)Stream.Dispose()Stream.Close()Stream.DisposeAsync()Stream.FlushAsync()Stream.FlushAsync(CancellationToken)Stream.CreateWaitHandle()Stream.BeginRead(byte[], int, int, AsyncCallback, object)Stream.EndRead(IAsyncResult)Stream.ReadAsync(byte[], int, int)Stream.ReadAsync(byte[], int, int, CancellationToken)Stream.ReadAsync(Memory<byte>, CancellationToken)Stream.ReadExactlyAsync(Memory<byte>, CancellationToken)Stream.ReadExactlyAsync(byte[], int, int, CancellationToken)Stream.ReadAtLeastAsync(Memory<byte>, int, bool, CancellationToken)Stream.BeginWrite(byte[], int, int, AsyncCallback, object)Stream.EndWrite(IAsyncResult)Stream.WriteAsync(byte[], int, int)Stream.WriteAsync(byte[], int, int, CancellationToken)Stream.WriteAsync(ReadOnlyMemory<byte>, CancellationToken)Stream.Read(Span<byte>)Stream.ReadByte()Stream.ReadExactly(Span<byte>)Stream.ReadExactly(byte[], int, int)Stream.ReadAtLeast(Span<byte>, int, bool)Stream.Write(ReadOnlySpan<byte>)Stream.Synchronized(Stream)Stream.ObjectInvariant()Stream.ValidateBufferArguments(byte[], int, int)Stream.ValidateCopyToArguments(Stream, int)Stream.CanTimeoutStream.ReadTimeoutStream.WriteTimeoutMarshalByRefObject.GetLifetimeService()MarshalByRefObject.InitializeLifetimeService()MarshalByRefObject.MemberwiseClone(bool)

Constructors

ExpandableMemoryStream(byte[], int)

Initializes a new instance of ExpandableMemoryStream with the specified initial data.

Declaration

cs-api-definition
public ExpandableMemoryStream(byte[] data, int bufferSize = 1000000)

Parameters

data

byte[]

The initial content to copy into the stream.

bufferSize

int

Optional custom buffer size in bytes for each segment. Default is 1 MB.

Exceptions

ArgumentNullException

Thrown when data is null.

ArgumentOutOfRangeException

Thrown when bufferSize is less than or equal to zero.

ExpandableMemoryStream(int)

Initializes a new instance of ExpandableMemoryStream.

Declaration

cs-api-definition
public ExpandableMemoryStream(int bufferSize = 1000000)

Parameters

bufferSize

int

Optional custom buffer size in bytes for each segment. Default is 1 MB.

Exceptions

ArgumentOutOfRangeException

Thrown when bufferSize is less than or equal to zero.

Properties

CanRead

Gets a value indicating whether the current stream supports reading.

Declaration

cs-api-definition
public override bool CanRead { get; }

Property Value

bool

true if the stream supports reading and is not disposed; otherwise, false.

Overrides Stream.CanRead

CanSeek

Gets a value indicating whether the current stream supports seeking.

Declaration

cs-api-definition
public override bool CanSeek { get; }

Property Value

bool

true if the stream supports seeking and is not disposed; otherwise, false.

Overrides Stream.CanSeek

CanWrite

Gets a value indicating whether the current stream supports writing.

Declaration

cs-api-definition
public override bool CanWrite { get; }

Property Value

bool

true if the stream supports writing and is not disposed; otherwise, false.

Overrides Stream.CanWrite

Length

Gets the length in bytes of the stream.

Declaration

cs-api-definition
public override long Length { get; }

Property Value

long

The number of bytes written to the stream.

Overrides Stream.Length

Position

Gets or sets the current position within the stream.

Declaration

cs-api-definition
public override long Position { get; set; }

Property Value

long

The current position within the stream.

Exceptions

ObjectDisposedException

The stream has been disposed.

ArgumentOutOfRangeException

The position is set to a value less than zero or greater than Length.

Overrides Stream.Position

Methods

Dispose(bool)

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

Declaration

cs-api-definition
protected override void Dispose(bool disposing)

Parameters

disposing

bool

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

Overrides Stream.Dispose(bool)

Remarks

When disposing is true, the internal buffers list is cleared and the stream becomes unusable.

Flush()

Clears all buffers for this stream and causes any buffered data to be written to the underlying device.

Declaration

cs-api-definition
public override void Flush()

Exceptions

ObjectDisposedException

The stream has been disposed.

Overrides Stream.Flush()

Remarks

This implementation stores data entirely in memory; therefore, this method is a no-op.

Read(byte[], int, int)

Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

Declaration

cs-api-definition
public override int Read(byte[] buffer, int offset, int count)

Parameters

buffer

byte[]

The buffer to write the data into.

offset

int

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

count

int

The maximum number of bytes to read.

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 if the end of the stream is reached.

Exceptions

ObjectDisposedException

The stream has been disposed.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative, or the sum of offset and count is greater than the buffer length.

Overrides Stream.Read(byte[], int, int)

Seek(long, SeekOrigin)

Sets the position within the current stream.

Declaration

cs-api-definition
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 stream.

Exceptions

ObjectDisposedException

The stream has been disposed.

ArgumentOutOfRangeException

origin is not a valid SeekOrigin value.

IOException

Seeking results in a negative position.

Overrides Stream.Seek(long, SeekOrigin)

SetLength(long)

Sets the length of the current stream.

Declaration

cs-api-definition
public override void SetLength(long value)

Parameters

value

long

The desired length of the current stream in bytes.

Exceptions

ObjectDisposedException

The stream has been disposed.

ArgumentOutOfRangeException

value is negative.

Overrides Stream.SetLength(long)

Remarks

If the new length is shorter than the current length, the stream is truncated and data beyond the new length is discarded. If the new length is longer, the stream is extended and the added region is initialized to zeros.

Write(byte[], int, int)

Writes a sequence of bytes to the current stream and advances the position within the stream by the number of bytes written.

Declaration

cs-api-definition
public override void Write(byte[] buffer, int offset, int count)

Parameters

buffer

byte[]

The buffer that contains the data to write to the stream.

offset

int

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

count

int

The number of bytes to write to the stream.

Exceptions

ObjectDisposedException

The stream has been disposed.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative, or the sum of offset and count is greater than the buffer length.

Overrides Stream.Write(byte[], int, int)

Remarks

The stream automatically grows to accommodate the written data. Data may span multiple underlying segments.

WriteByte(byte)

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

Declaration

cs-api-definition
public override void WriteByte(byte value)

Parameters

value

byte

The byte to write to the stream.

Exceptions

ObjectDisposedException

The stream has been disposed.

Overrides Stream.WriteByte(byte)