Table of Contents

Class StreamExtensions

Namespace
Icy.Data
Assembly
IcyUI.dll

Provides extension methods for the Stream class.

public static class StreamExtensions
Inheritance
StreamExtensions
Inherited Members

Methods

AsByteSpan<T>(ref T)

Converts a reference to an unmanaged structure into a Span<T> of bytes, allowing for direct manipulation of the underlying memory representation.

public static Span<byte> AsByteSpan<T>(this ref T value) where T : unmanaged

Parameters

value T

A reference to the unmanaged structure to be converted.

Returns

Span<byte>

A Span<T> of bytes representing the memory of the specified structure.

Type Parameters

T

The type of the unmanaged structure.

Remarks

This method uses CreateSpan<T>(scoped ref T, int) and As<TFrom, TTo>(ref TFrom) to create a span that points to the same memory as the original structure. It is important to ensure that the type T is unmanaged, as using this method with managed types may lead to undefined behavior.

Exceptions

ArgumentException

Thrown if T is not an unmanaged type.

CopyBytesAsync(Stream, Stream, long, long, byte[]?, CancellationToken)

Asynchronously copies a specified number of bytes from the source stream to the destination stream.

public static Task CopyBytesAsync(this Stream source, Stream destination, long offset, long length, byte[]? buffer = null, CancellationToken token = default)

Parameters

source Stream

The source stream to read from.

destination Stream

The destination stream to write to.

offset long

The offset in the source stream to start reading from.

length long

The number of bytes to copy.

buffer byte[]

An optional buffer to use for copying. If null, a temporary buffer will be allocated.

token CancellationToken

A cancellation token to monitor for cancellation requests.

Returns

Task

A task that represents the asynchronous copy operation.

Exceptions

ArgumentNullException

Thrown when the source or destination stream is null.

ReadBigEndian<T>(Stream)

Reads a value from the stream in big-endian byte order.

public static T ReadBigEndian<T>(this Stream stream) where T : unmanaged

Parameters

stream Stream

The stream to read from.

Returns

T

The value read from the stream.

Type Parameters

T

The type of the value to read. Must be unmanaged.

Exceptions

ArgumentNullException

Thrown when the stream is null.

ReadLittleEndian<T>(Stream)

Reads a value from the stream in little-endian byte order.

public static T ReadLittleEndian<T>(this Stream stream) where T : unmanaged

Parameters

stream Stream

The stream to read from.

Returns

T

The value read from the stream.

Type Parameters

T

The type of the value to read. Must be unmanaged.

Exceptions

ArgumentNullException

Thrown when the stream is null.

ReverseEndianness<T>(T)

Reverses the endianness of a value.

public static T ReverseEndianness<T>(this T value) where T : unmanaged

Parameters

value T

The value whose endianness is to be reversed.

Returns

T

The value with reversed endianness.

Type Parameters

T

The type of the value. Must be unmanaged.

ThrowEndOfStreamException(string?, Exception?)

Throws an EndOfStreamException with the specified message and inner exception.

[DoesNotReturn]
public static void ThrowEndOfStreamException(string? message = null, Exception? innerException = null)

Parameters

message string

The error message that explains the reason for the exception.

innerException Exception

The exception that is the cause of the current exception, or null if no inner exception is specified.

Exceptions

EndOfStreamException

Always thrown.

ThrowEndOfStreamException<T>(string?, Exception?)

Throws an EndOfStreamException with the specified message and inner exception, returning a default value of type T.

public static T ThrowEndOfStreamException<T>(string? message = null, Exception? innerException = null)

Parameters

message string

The error message that explains the reason for the exception.

innerException Exception

The exception that is the cause of the current exception, or null if no inner exception is specified.

Returns

T

Never returns a value, always throws an exception.

Type Parameters

T

The type to return if the exception is not thrown.

Exceptions

EndOfStreamException

Always thrown.

ThrowFileNotFoundException(string?, string?, Exception?)

Throws a FileNotFoundException with the specified message, file name, and inner exception.

[DoesNotReturn]
public static void ThrowFileNotFoundException(string? message = null, string? fileName = null, Exception? innerException = null)

Parameters

message string

The error message that explains the reason for the exception.

fileName string

The name of the file that could not be found.

innerException Exception

The exception that is the cause of the current exception, or null if no inner exception is specified.

Exceptions

FileNotFoundException

Always thrown.

ThrowFileNotFoundException<T>(string?, string?, Exception?)

Throws a FileNotFoundException with the specified message, file name, and inner exception, returning a default value of type T.

public static T ThrowFileNotFoundException<T>(string? message = null, string? fileName = null, Exception? innerException = null)

Parameters

message string

The error message that explains the reason for the exception.

fileName string

The name of the file that could not be found.

innerException Exception

The exception that is the cause of the current exception, or null if no inner exception is specified.

Returns

T

Never returns a value, always throws an exception.

Type Parameters

T

The type to return if the exception is not thrown.

Exceptions

FileNotFoundException

Always thrown.

WriteBigEndian<T>(Stream, T)

Writes a value to the stream in big-endian byte order.

public static void WriteBigEndian<T>(this Stream stream, T value) where T : unmanaged

Parameters

stream Stream

The stream to write to.

value T

The value to write to the stream.

Type Parameters

T

The type of the value to write. Must be unmanaged.

Exceptions

ArgumentNullException

Thrown when the stream is null.

WriteLittleEndian<T>(Stream, T)

Writes a value to the stream in little-endian byte order.

public static void WriteLittleEndian<T>(this Stream stream, T value) where T : unmanaged

Parameters

stream Stream

The stream to write to.

value T

The value to write to the stream.

Type Parameters

T

The type of the value to write. Must be unmanaged.

Exceptions

ArgumentNullException

Thrown when the stream is null.