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
valueTA reference to the unmanaged structure to be converted.
Returns
Type Parameters
TThe 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
Tis 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
sourceStreamThe source stream to read from.
destinationStreamThe destination stream to write to.
offsetlongThe offset in the source stream to start reading from.
lengthlongThe number of bytes to copy.
bufferbyte[]An optional buffer to use for copying. If null, a temporary buffer will be allocated.
tokenCancellationTokenA 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
streamStreamThe stream to read from.
Returns
- T
The value read from the stream.
Type Parameters
TThe 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
streamStreamThe stream to read from.
Returns
- T
The value read from the stream.
Type Parameters
TThe 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
valueTThe value whose endianness is to be reversed.
Returns
- T
The value with reversed endianness.
Type Parameters
TThe 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
messagestringThe error message that explains the reason for the exception.
innerExceptionExceptionThe 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
messagestringThe error message that explains the reason for the exception.
innerExceptionExceptionThe 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
TThe 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
messagestringThe error message that explains the reason for the exception.
fileNamestringThe name of the file that could not be found.
innerExceptionExceptionThe 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
messagestringThe error message that explains the reason for the exception.
fileNamestringThe name of the file that could not be found.
innerExceptionExceptionThe 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
TThe 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
streamStreamThe stream to write to.
valueTThe value to write to the stream.
Type Parameters
TThe 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
streamStreamThe stream to write to.
valueTThe value to write to the stream.
Type Parameters
TThe type of the value to write. Must be unmanaged.
Exceptions
- ArgumentNullException
Thrown when the stream is null.