Class StreamExtensions
Provides extension methods for Stream
public static class StreamExtensions
- Inheritance
-
StreamExtensions
- Inherited Members
Methods
ToClrStream(Stream, bool, bool?, bool?, bool?)
public static SdlToClrStream ToClrStream(this Stream sdlStream, bool leaveOpen = false, bool? canRead = null, bool? canSeek = null, bool? canWrite = null)
Parameters
sdlStreamStreamThe SDL Stream to wrap
leaveOpenboolA value indicating whether to leave the underlying SDL stream open when the wrapper is disposed
canReadbool?An overwrite flag indicating whether the stream can be read from
canSeekbool?An overwrite flag indicating whether the stream can be seeked
canWritebool?An overwrite flag indicating whether the stream can be written to
Returns
- SdlToClrStream
A SdlToClrStream wrapper around the SDL Stream
Remarks
The SdlToClrStream constructor does its best to detect the capabilities of the provided SDL Stream.
But in some cases it might not be possible to determine whether the stream can be read from, seeked or written to, or the detection might be wrong.
In those cases, you can use the optional canRead, canSeek and canWrite parameters to explicitly specify the capabilities of the stream.
Note that, for example specifying true for canWrite for an inherently read-only stream will not magically make it writable, but it will make this wrapper report that it is writable.
Please remember to dispose the returned SdlToClrStream instance when you are done using it to free up any resources.
Exceptions
- ArgumentNullException
sdlStreamisnull
ToSdlStream(Stream, bool)
public static Stream ToSdlStream(this Stream clrStream, bool leaveOpen = false)
Parameters
clrStreamStreamThe CLR Stream to wrap
leaveOpenboolA value indicating whether to leave the underlying CLR stream open when the wrapper is disposed
Returns
- Stream
A ClrToSdlStream wrapper around the CLR Stream
Remarks
Please remember to dispose the returned ClrToSdlStream instance when you are done using it to free up any resources.
Exceptions
- ArgumentNullException
clrStreamisnull
TryCopyTo(Stream, Stream, out nuint, out nuint, TimeSpan?, nuint)
Tries to copy all data from the source stream into a specified destination stream
public static bool TryCopyTo(this Stream source, Stream destination, out nuint bytesRead, out nuint bytesWritten, TimeSpan? timeout, nuint bufferSize = 4096)
Parameters
sourceStreamThe source stream to copy data into
destinationStreamThe destination stream to copy data into
bytesReadnuintThe number of bytes read from the
sourcestreambytesWrittennuintThe number of bytes written to the
destinationstreamtimeoutTimeSpan?The time to wait for the
destinationstream to become ready or fail if exceeded. Fractional nanoseconds will get rounded to next greater whole nanosecond value. Must not be negative and must represent a rounded nanoseconds value that is less or equal to MaxValue. Ifnull, waits indefinitely. IfZero, fails immediately.bufferSizenuintThe size of the buffer to use for copying. Defaults to
4096.
Returns
- bool
true, if the data from thesourcestream was successfully copied to thedestinationstream; otherwise,false(check TryGet(out string?) for more information)
Remarks
This method tries to copy as many bytes as possible before returning.
bytesRead will be equal to the actual number of bytes read from the stream, while bytesWritten will be equal to the actual number of bytes written to the destination stream, even in the case when not all data could be copied.
You should check the stream's Sdl3Sharp.Ffi.Status property as well as the destination's Sdl3Sharp.Ffi.Status property to determine whether a shortened copy operation is recoverable or not.
You may want to specify a timeout value for the time to wait for the destination stream to become ready in asynchronous I/O scenarios.
You can specify 0 to fail immediately, if the destination stream is not ready yet.
You can also specify null to wait indefinitely for the destination stream to become ready.
If the destination stream's Status property is any other value than Ready or NotReady, this methods still fails and returns false.
Please notice that the specified timeout value is only for waiting on the destination stream; if the source stream cannot read in time, this method still fails and returns false
(this behavior is identical to any other of the TryRead* methods of a Stream, except this method still returns true when the source stream's Status property is Eof and all data was successfully written).
This method is not threadsafe.
Exceptions
- ArgumentOutOfRangeException
timeoutis negative or the rounded nanosecond valuetimeoutrepresents is greater than MaxValue- InvalidOperationException
The internal timer SDL uses may have been reset or overflowed during the execution. Only happens when
timeoutis neithernullnorZero.- ArgumentNullException
destinationisnull- ArgumentOutOfRangeException
bufferSizeis not greater than0- InvalidOperationException
The internal timer SDL uses may have been reset or overflowed during the execution. Only happens when
nanosecondsTimeoutis neithernullnor0.
TryCopyTo(Stream, Stream, out nuint, out nuint, uint?, nuint)
Tries to copy all data from the source stream into a specified destination stream
public static bool TryCopyTo(this Stream source, Stream destination, out nuint bytesRead, out nuint bytesWritten, uint? millisecondsTimeout, nuint bufferSize = 4096)
Parameters
sourceStreamThe source stream to copy data into
destinationStreamThe destination stream to copy data into
bytesReadnuintThe number of bytes read from the
sourcestreambytesWrittennuintThe number of bytes written to the
destinationstreammillisecondsTimeoutuint?The number of milliseconds to wait for the
destinationstream to become ready or fail if exceeded. Ifnull, waits indefinitely. If0, fails immediately.bufferSizenuintThe size of the buffer to use for copying. Defaults to
4096.
Returns
- bool
true, if the data from thesourcestream was successfully copied to thedestinationstream; otherwise,false(check TryGet(out string?) for more information)
Remarks
This method tries to copy as many bytes as possible before returning.
bytesRead will be equal to the actual number of bytes read from the stream, while bytesWritten will be equal to the actual number of bytes written to the destination stream, even in the case when not all data could be copied.
You should check the stream's Sdl3Sharp.Ffi.Status property as well as the destination's Sdl3Sharp.Ffi.Status property to determine whether a shortened copy operation is recoverable or not.
You may want to specify a timeout value for the time to wait for the destination stream to become ready in asynchronous I/O scenarios.
You can specify 0 to fail immediately, if the destination stream is not ready yet.
You can also specify null to wait indefinitely for the destination stream to become ready.
If the destination stream's Status property is any other value than Ready or NotReady, this methods still fails and returns false.
Please notice that the specified timeout value is only for waiting on the destination stream; if the source stream cannot read in time, this method still fails and returns false
(this behavior is identical to any other of the TryRead* methods of a Stream, except this method still returns true when the source stream's Status property is Eof and all data was successfully written).
This method is not threadsafe.
Exceptions
- InvalidOperationException
The internal timer SDL uses may have been reset or overflowed during the execution. Only happens when
millisecondsTimeoutis neithernullnor0.- ArgumentNullException
destinationisnull- ArgumentOutOfRangeException
bufferSizeis not greater than0- InvalidOperationException
The internal timer SDL uses may have been reset or overflowed during the execution. Only happens when
nanosecondsTimeoutis neithernullnor0.
TryCopyTo(Stream, Stream, out nuint, out nuint, ulong?, nuint)
Tries to copy all data from the source stream into a specified destination stream
public static bool TryCopyTo(this Stream source, Stream destination, out nuint bytesRead, out nuint bytesWritten, ulong? nanosecondsTimeout = 0, nuint bufferSize = 4096)
Parameters
sourceStreamThe source stream to copy data into
destinationStreamThe destination stream to copy data into
bytesReadnuintThe number of bytes read from the
sourcestreambytesWrittennuintThe number of bytes written to the
destinationstreamnanosecondsTimeoutulong?The number of nanoseconds to wait for the
destinationstream to become ready or fail if exceeded. Ifnull, waits indefinitely. If0, fails immediately. Defaults to0.bufferSizenuintThe size of the buffer to use for copying. Defaults to
4096.
Returns
- bool
true, if the data from thesourcestream was successfully copied to thedestinationstream; otherwise,false(check TryGet(out string?) for more information)
Remarks
This method tries to copy as many bytes as possible before returning.
bytesRead will be equal to the actual number of bytes read from the stream, while bytesWritten will be equal to the actual number of bytes written to the destination stream, even in the case when not all data could be copied.
You should check the stream's Sdl3Sharp.Ffi.Status property as well as the destination's Sdl3Sharp.Ffi.Status property to determine whether a shortened copy operation is recoverable or not.
You may want to specify a timeout value for the time to wait for the destination stream to become ready in asynchronous I/O scenarios.
You can specify 0 to fail immediately, if the destination stream is not ready yet.
You can also specify null to wait indefinitely for the destination stream to become ready.
If the destination stream's Status property is any other value than Ready or NotReady, this methods still fails and returns false.
Please notice that the specified timeout value is only for waiting on the destination stream; if the source stream cannot read in time, this method still fails and returns false
(this behavior is identical to any other of the TryRead* methods of a Stream, except this method still returns true when the source stream's Status property is Eof and all data was successfully written).
This method is not threadsafe.
Exceptions
- ArgumentNullException
destinationisnull- ArgumentOutOfRangeException
bufferSizeis not greater than0- InvalidOperationException
The internal timer SDL uses may have been reset or overflowed during the execution. Only happens when
nanosecondsTimeoutis neithernullnor0.