Class StreamExtensions
- Namespace
- Plugin.BaseTypeExtensions
- Assembly
- Plugin.BaseTypeExtensions.dll
Provides extension methods for Stream operations and utility functions.
public static class StreamExtensions
- Inheritance
-
StreamExtensions
- Inherited Members
Methods
CanReset(Stream)
Determines whether the stream can be reset to the beginning.
public static bool CanReset(this Stream stream)
Parameters
stream
StreamThe stream to check.
Returns
- bool
True if the stream can be reset; otherwise, false.
Exceptions
- ArgumentNullException
Thrown when stream is null.
CopyToWithProgress(Stream, Stream, IProgress<long>?)
Copies the stream to another stream with progress reporting.
public static void CopyToWithProgress(this Stream source, Stream destination, IProgress<long>? progress = null)
Parameters
source
StreamThe source stream to copy from.
destination
StreamThe destination stream to copy to.
progress
IProgress<long>An optional progress reporter for tracking copy progress.
Exceptions
- ArgumentNullException
Thrown when source or destination is null.
CopyToWithProgressAsync(Stream, Stream, IProgress<long>?, CancellationToken)
Copies the stream to another stream asynchronously with progress reporting.
public static Task CopyToWithProgressAsync(this Stream source, Stream destination, IProgress<long>? progress = null, CancellationToken cancellationToken = default)
Parameters
source
StreamThe source stream to copy from.
destination
StreamThe destination stream to copy to.
progress
IProgress<long>An optional progress reporter for tracking copy progress.
cancellationToken
CancellationTokenA token to cancel the operation.
Returns
- Task
A task that represents the asynchronous copy operation.
Exceptions
- ArgumentNullException
Thrown when source or destination is null.
- OperationCanceledException
Thrown when the operation is cancelled.
GetLength(Stream)
Gets the length of the stream safely, returning -1 if not available.
public static long GetLength(this Stream stream)
Parameters
stream
StreamThe stream to measure.
Returns
- long
The length of the stream, or -1 if not available.
Exceptions
- ArgumentNullException
Thrown when stream is null.
Reset(Stream)
Resets the stream position to the beginning if possible.
public static void Reset(this Stream stream)
Parameters
stream
StreamThe stream to reset.
Exceptions
- ArgumentNullException
Thrown when stream is null.
- NotSupportedException
Thrown when the stream does not support seeking.
SaveToFile(Stream, string)
Saves the stream content to a file.
public static void SaveToFile(this Stream stream, string filePath)
Parameters
Exceptions
- ArgumentNullException
Thrown when stream or filePath is null.
- ArgumentException
Thrown when filePath is empty or whitespace.
- IOException
Thrown when an I/O error occurs.
- UnauthorizedAccessException
Thrown when access to the file is denied.
SaveToFileAsync(Stream, string, CancellationToken)
Saves the stream content to a file asynchronously.
public static Task SaveToFileAsync(this Stream stream, string filePath, CancellationToken cancellationToken = default)
Parameters
stream
StreamThe stream to save.
filePath
stringThe file path where to save the stream.
cancellationToken
CancellationTokenA token to cancel the operation.
Returns
- Task
A task that represents the asynchronous save operation.
Exceptions
- ArgumentNullException
Thrown when stream or filePath is null.
- ArgumentException
Thrown when filePath is empty or whitespace.
- IOException
Thrown when an I/O error occurs.
- UnauthorizedAccessException
Thrown when access to the file is denied.
- OperationCanceledException
Thrown when the operation is cancelled.
ToByteArray(Stream)
Converts the stream to a byte array.
public static byte[] ToByteArray(this Stream stream)
Parameters
stream
StreamThe stream to convert.
Returns
- byte[]
A byte array containing the stream data.
Exceptions
- ArgumentNullException
Thrown when stream is null.
ToByteArrayAsync(Stream, CancellationToken)
Converts the stream to a byte array asynchronously.
public static Task<byte[]> ToByteArrayAsync(this Stream stream, CancellationToken cancellationToken = default)
Parameters
stream
StreamThe stream to convert.
cancellationToken
CancellationTokenA token to cancel the operation.
Returns
- Task<byte[]>
A task that represents the asynchronous operation, containing a byte array with the stream data.
Exceptions
- ArgumentNullException
Thrown when stream is null.
- OperationCanceledException
Thrown when the operation is cancelled.
ToMemoryStream(Stream)
Converts the stream to a MemoryStream.
public static MemoryStream ToMemoryStream(this Stream stream)
Parameters
stream
StreamThe stream to convert.
Returns
- MemoryStream
A MemoryStream containing the stream data.
Exceptions
- ArgumentNullException
Thrown when stream is null.
ToStringContent(Stream, Encoding?)
Converts the stream to a string using the specified encoding.
public static string ToStringContent(this Stream stream, Encoding? encoding = null)
Parameters
Returns
- string
A string containing the stream content.
Exceptions
- ArgumentNullException
Thrown when stream is null.
ToStringContentAsync(Stream, Encoding?, CancellationToken)
Converts the stream to a string asynchronously using the specified encoding.
public static Task<string> ToStringContentAsync(this Stream stream, Encoding? encoding = null, CancellationToken cancellationToken = default)
Parameters
stream
StreamThe stream to convert.
encoding
EncodingThe encoding to use (default is UTF-8).
cancellationToken
CancellationTokenA token to cancel the operation.
Returns
- Task<string>
A task that represents the asynchronous operation, containing a string with the stream content.
Exceptions
- ArgumentNullException
Thrown when stream is null.
- OperationCanceledException
Thrown when the operation is cancelled.