Table of Contents

Class Stream

Namespace
Sdl3Sharp.IO
Assembly
Sdl3Sharp.dll

A base stream for reading and writing data

public abstract class Stream : IDisposable
Inheritance
Stream
Implements
Derived
Inherited Members
Extension Methods

Constructors

Stream()

Creates a new Stream instance that uses a custom IO implementation provided by the derived class

protected Stream()

Exceptions

SdlException

The custom IO stream could not be created (check TryGet(out string?) for more information)

Properties

IsValid

Gets a value indicating whether the Stream is valid

public bool IsValid { get; }

Property Value

bool

A value indicating whether the Stream is valid

Remarks

A Stream becomes invalid after it has been closed or disposed.

Length

Gets the length of the stream, in bytes

public long Length { get; }

Property Value

long

The length of the stream, in bytes

Remarks

This property is not threadsafe.

LengthCore

Gets the total length, in bytes, of the stream

protected abstract long LengthCore { get; }

Property Value

long

The total length, in bytes, of the stream, or -1 on failure

See Also

Position

Gets the current read/write offset in the stream, in bytes

public long Position { get; }

Property Value

long

The current read/write offset in the stream, in bytes

Remarks

This property is actually a wrapper around TrySeek(long, StreamWhence, out long) with an offset of 0 bytes from Current.

This property is not threadsafe.

Properties

Gets the properties associated with the stream

public Properties? Properties { get; }

Property Value

Properties

The properties associated with the stream, or null if the properties could not be retrieved successfully (check TryGet(out string?) for more information)

Remarks

This property is not threadsafe.

SdlIOStream

Gets a pointer to the underlying SDL SDL_IOStream that the Stream represents

public nint SdlIOStream { get; }

Property Value

nint

A pointer to the underlying SDL SDL_IOStream that the Stream represents. This can be cast to a SDL SDL_IOStream*.

Status

Gets the current status of the stream

public StreamStatus Status { get; }

Property Value

StreamStatus

The current status of the stream

Remarks

Inspecting the status of the stream can be useful to determine if a short read or write operation was due to an error, due to reaching the end of the stream, or due to a non-blocking operation that hasn't finished.

The value of this property is only expected to change after read or write operations on the stream. Don't expect it to change spontaneously.

This property is not threadsafe.

Methods

CloseCore()

Closes the stream and release associated resources

protected abstract bool CloseCore()

Returns

bool

true if the stream was closed successfully; otherwise, false

See Also

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Dispose(bool, bool)

Disposes the stream

protected virtual void Dispose(bool disposing, bool close)

Parameters

disposing bool

A value indicating whether the call came from a call to Dispose() or from the finalizer

close bool

A value indicating whether the stream should be closed

Remarks

This method is not threadsafe.

See Also

~Stream()

protected ~Stream()

FlushCore(ref StreamStatus)

Flushes any buffered data in the stream

protected abstract bool FlushCore(ref StreamStatus status)

Parameters

status StreamStatus

The StreamStatus of the stream. The value of the referenced StreamStatus could change on failure and should not necessarily change on success.

Returns

bool

true if the flush was successful; otherwise, false

See Also

ReadCore(NativeMemory, ref StreamStatus)

Reads into specified data from the stream

protected abstract nuint ReadCore(NativeMemory data, ref StreamStatus status)

Parameters

data NativeMemory

The memory buffer to read data into

status StreamStatus

The StreamStatus of the stream. The value of the referenced StreamStatus could change on failure and should not necessarily change on success.

Returns

nuint

The number of bytes read from the stream

See Also

SeekCore(long, StreamWhence)

Seeks within the stream

protected abstract long SeekCore(long offset, StreamWhence whence)

Parameters

offset long

The offset to seek to

whence StreamWhence

The reference point for the seek operation

Returns

long

The absolute offset from the start of the stream after seeking, or -1 on failure

See Also

TryClose()

Tries to close the stream and releases associated resources

public bool TryClose()

Returns

bool

true if the stream was closed without failure; otherwise, false (check TryGet(out string?) for more information)

Remarks

Note: this method returns false if the stream couldn't be flushed successfully before closing. The stream is still invalid when this method returns.

A call to this method flushes any buffered writes to the operating system, but there are no guarantees that those writes have gone to physical media; they might be in the OS's file cache, waiting to go to disk later. If it's absolutely crucial that writes go to disk immediately, so they are definitely stored even if the power fails before the file cache would have caught up, one should call TryFlush() before closing. Note that flushing takes time and makes the system and your app operate less efficiently, so do so sparingly.

This method is not threadsafe.

TryFlush()

Tries to flush any buffered data in the stream

public bool TryFlush()

Returns

bool

true if the flush was successful; otherwise, false (check TryGet(out string?) for more information)

Remarks

A call to this method makes sure that any buffered data is written to the stream. Normally this isn't necessary but if the stream is a pipe or socket it guarantees that any pending data is sent.

This method is not threadsafe.

TryLoad(out NativeMemoryManager?, bool)

Tries to load all the data from the stream

public bool TryLoad(out NativeMemoryManager? data, bool closeAfterwards = false)

Parameters

data NativeMemoryManager

A NativeMemoryManager managing native memory containing all available data from the stream, when this method returns true; otherwise, null

closeAfterwards bool

A value indicating whether the stream should be closed before this method returns (even in case of an error)

Returns

bool

true, if all available data from the stream was succesfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The resulting NativeMemoryManager should be disposed if the memory it's managing is no longer needed. That also frees the allocated memory.

This method is not threadsafe.

TryRead(NativeMemory, out nuint)

Tries to read data from the stream

public bool TryRead(NativeMemory data, out nuint bytesRead)

Parameters

data NativeMemory

The memory buffer to read data into

bytesRead nuint

The number of bytes read from the stream, when this method returns true; otherwise, 0

Returns

bool

true if the data was successfully read into the specified memory buffer; otherwise, false (check TryGet(out string?) for more information)

Remarks

This method might read less bytes than requested by data's Length property.

This method will return false (bytesRead will be 0), if the end of the stream was reached before reading any data (Status is Eof). If this method returns false (bytesRead is 0) and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryRead(Span<byte>, out int)

Tries to read data from the stream

public bool TryRead(Span<byte> data, out int bytesRead)

Parameters

data Span<byte>

The Span<T> to read data into

bytesRead int

The number of bytes read from the stream, when this method returns true; otherwise, 0

Returns

bool

true if the data was successfully read into the specified Span<T>; otherwise, false (check TryGet(out string?) for more information)

Remarks

This method might read less bytes than requested by data's Length property.

This method will return false (bytesRead will be 0), if the end of the stream was reached before reading any data (Status is Eof). If this method returns false (bytesRead is 0) and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryRead(void*, nuint, out nuint)

Tries to read data from the stream

public bool TryRead(void* data, nuint size, out nuint bytesRead)

Parameters

data void*

A pointer to the unmananged memory to read data into

size nuint

The number of bytes to read

bytesRead nuint

The number of bytes read from the stream, when this method returns true; otherwise, 0

Returns

bool

true if the data was successfully read into the specified unmanaged memory; otherwise, false (check TryGet(out string?) for more information)

Remarks

This method might read less bytes than requested by size.

This method will return false (bytesRead will be 0), if the end of the stream was reached before reading any data (Status is Eof). If this method returns false (bytesRead is 0) and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadDoubleBE(out double)

Tries to read a big-endian double-precision floating point value from the stream

public bool TryReadDoubleBE(out double value)

Parameters

value double

The double-precision floating point value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the double-precision floating point value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the double-precision floating point value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadDoubleLE(out double)

Tries to read a little-endian double-precision floating point value from the stream

public bool TryReadDoubleLE(out double value)

Parameters

value double

The double-precision floating point value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the double-precision floating point value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the double-precision floating point value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadHalfBE(out Half)

Tries to read a big-endian half-precision floating point value from the stream

public bool TryReadHalfBE(out Half value)

Parameters

value Half

The half-precision floating point value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the half-precision floating point value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the half-precision floating point value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadHalfLE(out Half)

Tries to read a little-endian half-precision floating point value from the stream

public bool TryReadHalfLE(out Half value)

Parameters

value Half

The half-precision floating point value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the half-precision floating point value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the half-precision floating point value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadInt128BE(out Int128)

Tries to read a big-endian 128-bit signed integer value from the stream

public bool TryReadInt128BE(out Int128 value)

Parameters

value Int128

The 128-bit signed integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 128-bit signed integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 128-bit signed integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadInt16BE(out short)

Tries to read a big-endian 16-bit signed integer value from the stream

public bool TryReadInt16BE(out short value)

Parameters

value short

The 16-bit signed integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 16-bit signed integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 16-bit signed integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadInt16LE(out short)

Tries to read a little-endian 16-bit signed integer value from the stream

public bool TryReadInt16LE(out short value)

Parameters

value short

The 16-bit signed integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 16-bit signed integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 16-bit signed integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadInt32BE(out int)

Tries to read a big-endian 32-bit signed integer value from the stream

public bool TryReadInt32BE(out int value)

Parameters

value int

The 32-bit signed integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 32-bit signed integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 32-bit signed integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadInt32LE(out int)

Tries to read a little-endian 32-bit signed integer value from the stream

public bool TryReadInt32LE(out int value)

Parameters

value int

The 32-bit signed integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 32-bit signed integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 32-bit signed integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadInt64BE(out long)

Tries to read a big-endian 64-bit signed integer value from the stream

public bool TryReadInt64BE(out long value)

Parameters

value long

The 64-bit signed integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 64-bit signed integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 64-bit signed integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadInt64LE(out long)

Tries to read a little-endian 64-bit signed integer value from the stream

public bool TryReadInt64LE(out long value)

Parameters

value long

The 64-bit signed integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 64-bit signed integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 64-bit signed integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadInt8(out sbyte)

Tries to read an 8-bit signed integer value from the stream

public bool TryReadInt8(out sbyte value)

Parameters

value sbyte

The 8-bit signed integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 8-bit signed integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

This method will return false, if the end of the stream was reached before completely reading the 8-bit signed integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadIntPtrBE(out nint)

Tries to read a big-endian pointer-sized signed integer value from the stream

public bool TryReadIntPtrBE(out nint value)

Parameters

value nint

The pointer-sized signed integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the pointer-sized signed integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the pointer-sized signed integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadIntPtrLE(out nint)

Tries to read a little-endian pointer-sized signed integer value from the stream

public bool TryReadIntPtrLE(out nint value)

Parameters

value nint

The pointer-sized signed integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the pointer-sized signed integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the pointer-sized signed integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadSingleBE(out float)

Tries to read a big-endian single-precision floating point value from the stream

public bool TryReadSingleBE(out float value)

Parameters

value float

The single-precision floating point value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the single-precision floating point value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the single-precision floating point value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadSingleLE(out float)

Tries to read a little-endian single-precision floating point value from the stream

public bool TryReadSingleLE(out float value)

Parameters

value float

The single-precision floating point value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the single-precision floating point value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the single-precision floating point value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadUInt128BE(out UInt128)

Tries to read a big-endian 128-bit unsigned integer value from the stream

public bool TryReadUInt128BE(out UInt128 value)

Parameters

value UInt128

The 128-bit unsigned integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 128-bit unsigned integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 128-bit unsigned integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadUInt128LE(out Int128)

Tries to read a little-endian 128-bit signed integer value from the stream

public bool TryReadUInt128LE(out Int128 value)

Parameters

value Int128

The 128-bit signed integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 128-bit signed integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 128-bit signed integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadUInt128LE(out UInt128)

Tries to read a little-endian 128-bit unsigned integer value from the stream

public bool TryReadUInt128LE(out UInt128 value)

Parameters

value UInt128

The 128-bit unsigned integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 128-bit unsigned integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 128-bit unsigned integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadUInt16BE(out ushort)

Tries to read a big-endian 16-bit unsigned integer value from the stream

public bool TryReadUInt16BE(out ushort value)

Parameters

value ushort

The 16-bit unsigned integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 16-bit unsigned integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 16-bit unsigned integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadUInt16LE(out ushort)

Tries to read a little-endian 16-bit unsigned integer value from the stream

public bool TryReadUInt16LE(out ushort value)

Parameters

value ushort

The 16-bit unsigned integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 16-bit unsigned integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 16-bit unsigned integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadUInt32BE(out uint)

Tries to read a big-endian 32-bit unsigned integer value from the stream

public bool TryReadUInt32BE(out uint value)

Parameters

value uint

The 32-bit unsigned integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 32-bit unsigned integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 32-bit unsigned integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadUInt32LE(out uint)

Tries to read a little-endian 32-bit unsigned integer value from the stream

public bool TryReadUInt32LE(out uint value)

Parameters

value uint

The 32-bit unsigned integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 32-bit unsigned integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 32-bit unsigned integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadUInt64BE(out ulong)

Tries to read a big-endian 64-bit unsigned integer value from the stream

public bool TryReadUInt64BE(out ulong value)

Parameters

value ulong

The 64-bit unsigned integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 64-bit unsigned integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 64-bit unsigned integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadUInt64LE(out ulong)

Tries to read a little-endian 64-bit unsigned integer value from the stream

public bool TryReadUInt64LE(out ulong value)

Parameters

value ulong

The 64-bit unsigned integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 64-bit unsigned integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the 64-bit unsigned integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadUInt8(out byte)

Tries to read an 8-bit unsigned integer value from the stream

public bool TryReadUInt8(out byte value)

Parameters

value byte

The 8-bit unsigned integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the 8-bit unsigned integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

This method will return false, if the end of the stream was reached before completely reading the 8-bit unsigned integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadUIntPtrBE(out nuint)

Tries to read a big-endian pointer-sized unsigned integer value from the stream

public bool TryReadUIntPtrBE(out nuint value)

Parameters

value nuint

The pointer-sized unsigned integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the pointer-sized unsigned integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in big-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the pointer-sized unsigned integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryReadUIntPtrLE(out nuint)

Tries to read a little-endian pointer-sized unsigned integer value from the stream

public bool TryReadUIntPtrLE(out nuint value)

Parameters

value nuint

The pointer-sized unsigned integer value that was read from the stream, when this method returns true; otherwise, undefined

Returns

bool

true if the pointer-sized unsigned integer value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value is expected to be stored in the stream in little-endian byte order. The resulting value will be in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the pointer-sized unsigned integer value (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TryRead<T>(out T, out nuint)

Tries to read a value of type T from the stream

public bool TryRead<T>(out T value, out nuint bytesRead) where T : unmanaged, allows ref struct

Parameters

value T

The value of type T that was read from the stream, when this method returns true; otherwise, uninitialized

bytesRead nuint

The number of bytes read from the stream

Returns

bool

true if the value was successfully read; otherwise, false (check TryGet(out string?) for more information)

Type Parameters

T

The type of the value to read

Remarks

Values will be read bitwise (structure-wise) and in the endianness of the current platform.

This method will return false, if the end of the stream was reached before completely reading the value of type T and bytesRead be equal to the number of potentially partially read bytes (Status will be Eof). If this method returns false and the Status is not Eof, an error occured and you should check TryGet(out string?) for more information.

This method is not threadsafe.

TrySave(ReadOnlyNativeMemory, bool)

Tries to save specified data into the stream

public bool TrySave(ReadOnlyNativeMemory data, bool closeAfterwards = false)

Parameters

data ReadOnlyNativeMemory

The memory buffer containing all the data to be saved into the stream

closeAfterwards bool

A value indicating whether the stream should be closed before this method returns (even in case of an error)

Returns

bool

true if the data from the specified memory buffer was successfully saved into the stream; otherwise, false (check TryGet(out string?) for more information)

Remarks

This method is not threadsafe.

TrySave(ReadOnlySpan<byte>, bool)

Tries to save specified data into the stream

public bool TrySave(ReadOnlySpan<byte> data, bool closeAfterwards = false)

Parameters

data ReadOnlySpan<byte>

The Span<T> containing all the data to be saved into the stream

closeAfterwards bool

A value indicating whether the stream should be closed before this method returns (even in case of an error)

Returns

bool

true if the data from the specified Span<T> was successfully saved into the stream; otherwise, false (check TryGet(out string?) for more information)

Remarks

This method is not threadsafe.

TrySave(void*, nuint, bool)

Tries to save specified data into the stream

public bool TrySave(void* data, nuint size, bool closeAfterwards = false)

Parameters

data void*

A pointer to the unmanaged memory containing all the data to be saved into the stream

size nuint

The size, in bytes, of the data to be saved

closeAfterwards bool

A value indicating whether the stream should be closed before this method returns (even in case of an error)

Returns

bool

true if the data from the specified unmanaged memory was successfully saved into the stream; otherwise, false (check TryGet(out string?) for more information)

Remarks

The unmanaged memory pointed to by data must be safely dereferencable for at least size bytes.

This method is not threadsafe.

TrySeek(long, StreamWhence, out long)

Tries to seek within the stream

public bool TrySeek(long offset, StreamWhence whence, out long absoluteOffset)

Parameters

offset long

The offset to seek to

whence StreamWhence

The reference point for the seek operation

absoluteOffset long

The absolute offset from the start of the stream after seeking, when this method returns true; otherwise, -1

Returns

bool

true if the seek operation was successful; otherwise, false (check TryGet(out string?) for more information)

Remarks

This method will return false, if the stream cannot be seeked.

This method is not threadsafe.

TryWrite(ReadOnlyNativeMemory, out nuint)

Tries to write specified data into the stream

public bool TryWrite(ReadOnlyNativeMemory data, out nuint bytesWritten)

Parameters

data ReadOnlyNativeMemory

The memory buffer containing all the data to be written into the stream

bytesWritten nuint

The number of bytes written to the stream

Returns

bool

true if the data from the specified memory buffer was successfully written into the stream; otherwise, false (check TryGet(out string?) for more information)

Remarks

In case of an error, this method still attempts to write as many bytes as possible before returning. bytesWritten will be equal to the actual number of bytes written to the stream, even in the case when not all data could be written. You should check the stream's Status property to determine whether a shorted write operation is recoverable or not.

This method is not threadsafe.

TryWrite(ReadOnlySpan<byte>, out int)

Tries to write specified data into the stream

public bool TryWrite(ReadOnlySpan<byte> data, out int bytesWritten)

Parameters

data ReadOnlySpan<byte>

The Span<T> containing all the data to be written into the stream

bytesWritten int

The number of bytes written to the stream

Returns

bool

true if the data from the specified Span<T> was successfully written into the stream; otherwise, false (check TryGet(out string?) for more information)

Remarks

In case of an error, this method still attempts to write as many bytes as possible before returning. bytesWritten will be equal to the actual number of bytes written to the stream, even in the case when not all data could be written. You should check the stream's Status property to determine whether a shorted write operation is recoverable or not.

This method is not threadsafe.

TryWrite(string, out nuint)

Tries to write a string into the stream

public bool TryWrite(string text, out nuint bytesWritten)

Parameters

text string

The string to be written into the stream

bytesWritten nuint

The number of bytes written to the stream

Returns

bool

true if the string was successfully written into the stream; otherwise, false (check TryGet(out string?) for more information)

Remarks

The text parameter is treated as a C-style printf format string. This means that any % characters are interpreted as format specifiers. To include a literal % character in the output, you must escape it by writing %%.

This method is not threadsafe.

TryWrite(string, out nuint, params ReadOnlySpan<object>)

Tries to write a formatted string into the stream

public bool TryWrite(string format, out nuint bytesWritten, params ReadOnlySpan<object> args)

Parameters

format string

The C-style printf format string to be written into the stream

bytesWritten nuint

The number of bytes written to the stream

args ReadOnlySpan<object>

The arguments corresponding to the format specifiers in format

Returns

bool

true if the formatted string was successfully written into the stream; otherwise, false (check TryGet(out string?) for more information)

Remarks

The format parameter is interpreted as a C-style printf format string, and the args parameter supplies the values for its format specifiers. Supported argument types include all integral types up to 64-bit (including bool and char), floating point types (float and double), pointer types (nint and nuint), and string.

For a detailed explanation of C-style printf format strings and their specifiers, see https://en.wikipedia.org/wiki/Printf#Format_specifier.

Consider using TryWrite(string, out nuint) instead when possible, as it may be more efficient. In many cases, you can use C# string interpolation to construct the string before writing.

This method is not threadsafe.

TryWrite(void*, nuint, out nuint)

Tries to write specified data into the stream

public bool TryWrite(void* data, nuint size, out nuint bytesWritten)

Parameters

data void*

A pointer to the unmanaged memory containing all the data to be written into the stream

size nuint

The size, in bytes, of the data to be written

bytesWritten nuint

The number of bytes written to the stream

Returns

bool

true if the data from the specified unmanaged memory was successfully written into the stream; otherwise, false (check TryGet(out string?) for more information)

Remarks

The unmanaged memory pointed to by data must be safely dereferencable for at least size bytes.

In case of an error, this method still attempts to write as many bytes as possible before returning. bytesWritten will be equal to the actual number of bytes written to the stream, even in the case when not all data could be written. You should check the stream's Status property to determine whether a shorted write operation is recoverable or not.

This method is not threadsafe.

TryWriteDoubleBE(double)

Tries to write a big-endian double-precision floating point value to the stream

public bool TryWriteDoubleBE(double value)

Parameters

value double

The double-precision floating point value to write into the stream

Returns

bool

true if the double-precision floating point value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteDoubleLE(double)

Tries to write a little-endian double-precision floating point value to the stream

public bool TryWriteDoubleLE(double value)

Parameters

value double

The double-precision floating point value to write into the stream

Returns

bool

true if the double-precision floating point value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteHalfBE(Half)

Tries to write a big-endian half-precision floating point value to the stream

public bool TryWriteHalfBE(Half value)

Parameters

value Half

The half-precision floating point value to write into the stream

Returns

bool

true if the half-precision floating point value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteHalfLE(Half)

Tries to write a little-endian half-precision floating point value to the stream

public bool TryWriteHalfLE(Half value)

Parameters

value Half

The half-precision floating point value to write into the stream

Returns

bool

true if the half-precision floating point value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteInt128BE(Int128)

Tries to write a big-endian 128-bit signed integer value to the stream

public bool TryWriteInt128BE(Int128 value)

Parameters

value Int128

The 128-bit signed integer value to write into the stream

Returns

bool

true if the 128-bit signed integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteInt128LE(Int128)

Tries to write a little-endian 128-bit signed integer value to the stream

public bool TryWriteInt128LE(Int128 value)

Parameters

value Int128

The 128-bit signed integer value to write into the stream

Returns

bool

true if the 128-bit signed integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteInt16BE(short)

Tries to write a big-endian 16-bit signed integer value to the stream

public bool TryWriteInt16BE(short value)

Parameters

value short

The 16-bit signed integer value to write into the stream

Returns

bool

true if the 16-bit signed integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteInt16LE(short)

Tries to write a little-endian 16-bit signed integer value to the stream

public bool TryWriteInt16LE(short value)

Parameters

value short

The 16-bit signed integer value to write into the stream

Returns

bool

true if the 16-bit signed integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteInt32BE(int)

Tries to write a big-endian 32-bit signed integer value to the stream

public bool TryWriteInt32BE(int value)

Parameters

value int

The 32-bit signed integer value to write into the stream

Returns

bool

true if the 32-bit signed integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteInt32LE(int)

Tries to write a little-endian 32-bit signed integer value to the stream

public bool TryWriteInt32LE(int value)

Parameters

value int

The 32-bit signed integer value to write into the stream

Returns

bool

true if the 32-bit signed integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteInt64BE(long)

Tries to write a big-endian 64-bit signed integer value to the stream

public bool TryWriteInt64BE(long value)

Parameters

value long

The 64-bit signed integer value to write into the stream

Returns

bool

true if the 64-bit signed integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteInt64LE(long)

Tries to write a little-endian 64-bit signed integer value to the stream

public bool TryWriteInt64LE(long value)

Parameters

value long

The 64-bit signed integer value to write into the stream

Returns

bool

true if the 64-bit signed integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteInt8(sbyte)

Tries to write an 8-bit signed integer value to the stream

public bool TryWriteInt8(sbyte value)

Parameters

value sbyte

The 8-bit signed integer value to write into the stream

Returns

bool

true if the 8-bit signed integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteIntPtrBE(nint)

Tries to write a big-endian pointer-sized signed integer value to the stream

public bool TryWriteIntPtrBE(nint value)

Parameters

value nint

The pointer-sized signed integer value to write into the stream

Returns

bool

true if the pointer-sized signed integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteIntPtrLE(nint)

Tries to write a little-endian pointer-sized signed integer value to the stream

public bool TryWriteIntPtrLE(nint value)

Parameters

value nint

The pointer-sized signed integer value to write into the stream

Returns

bool

true if the pointer-sized signed integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteSingleBE(float)

Tries to write a big-endian single-precision floating point value to the stream

public bool TryWriteSingleBE(float value)

Parameters

value float

The single-precision floating point value to write into the stream

Returns

bool

true if the single-precision floating point value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteSingleLE(float)

Tries to write a little-endian single-precision floating point value to the stream

public bool TryWriteSingleLE(float value)

Parameters

value float

The single-precision floating point value to write into the stream

Returns

bool

true if the single-precision floating point value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteUInt128BE(UInt128)

Tries to write a big-endian 128-bit unsigned integer value to the stream

public bool TryWriteUInt128BE(UInt128 value)

Parameters

value UInt128

The 128-bit unsigned integer value to write into the stream

Returns

bool

true if the 128-bit unsigned integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteUInt128LE(UInt128)

Tries to write a little-endian 128-bit unsigned integer value to the stream

public bool TryWriteUInt128LE(UInt128 value)

Parameters

value UInt128

The 128-bit unsigned integer value to write into the stream

Returns

bool

true if the 128-bit unsigned integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteUInt128LE(nuint)

Tries to write a little-endian pointer-sized unsigned integer value to the stream

public bool TryWriteUInt128LE(nuint value)

Parameters

value nuint

The pointer-sized unsigned integer value to write into the stream

Returns

bool

true if the pointer-sized unsigned integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteUInt16BE(ushort)

Tries to write a big-endian 16-bit unsigned integer value to the stream

public bool TryWriteUInt16BE(ushort value)

Parameters

value ushort

The 16-bit unsigned integer value to write into the stream

Returns

bool

true if the 16-bit unsigned integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteUInt16LE(ushort)

Tries to write a little-endian 16-bit unsigned integer value to the stream

public bool TryWriteUInt16LE(ushort value)

Parameters

value ushort

The 16-bit unsigned integer value to write into the stream

Returns

bool

true if the 16-bit unsigned integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteUInt32BE(uint)

Tries to write a big-endian 32-bit unsigned integer value to the stream

public bool TryWriteUInt32BE(uint value)

Parameters

value uint

The 32-bit unsigned integer value to write into the stream

Returns

bool

true if the 32-bit unsigned integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteUInt32LE(uint)

Tries to write a little-endian 32-bit unsigned integer value to the stream

public bool TryWriteUInt32LE(uint value)

Parameters

value uint

The 32-bit unsigned integer value to write into the stream

Returns

bool

true if the 32-bit unsigned integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteUInt64BE(ulong)

Tries to write a big-endian 64-bit unsigned integer value to the stream

public bool TryWriteUInt64BE(ulong value)

Parameters

value ulong

The 64-bit unsigned integer value to write into the stream

Returns

bool

true if the 64-bit unsigned integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteUInt64LE(ulong)

Tries to write a little-endian 64-bit unsigned integer value to the stream

public bool TryWriteUInt64LE(ulong value)

Parameters

value ulong

The 64-bit unsigned integer value to write into the stream

Returns

bool

true if the 64-bit unsigned integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in little-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWriteUInt8(byte)

Tries to write an 8-bit unsigned integer value to the stream

public bool TryWriteUInt8(byte value)

Parameters

value byte

The 8-bit unsigned integer value to write into the stream

Returns

bool

true if the 8-bit unsigned integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

This method is not threadsafe.

TryWriteUIntPtrBE(nuint)

Tries to write a big-endian pointer-sized unsigned integer value to the stream

public bool TryWriteUIntPtrBE(nuint value)

Parameters

value nuint

The pointer-sized unsigned integer value to write into the stream

Returns

bool

true if the pointer-sized unsigned integer value was successfully written; otherwise, false (check TryGet(out string?) for more information)

Remarks

The value will be stored in the stream in big-endian byte order regardless of the endianness of the current platform.

This method is not threadsafe.

TryWrite<T>(in T, out nuint)

Tries to write a value of type T into the stream

public bool TryWrite<T>(in T value, out nuint bytesWritten) where T : unmanaged, allows ref struct

Parameters

value T

The value of type T to be written into the stream

bytesWritten nuint

The number of bytes written to the stream

Returns

bool

true if the value was successfully written into the stream; otherwise, false (check TryGet(out string?) for more information)

Type Parameters

T

The type of the value to write

Remarks

Values will be written bitwise (structure-wise) and in the endianness of the current platform.

In case of an error, this method still attempts to write as many bytes as possible before returning. bytesWritten will be equal to the actual number of bytes written to the stream, even in the case when value was only partially written into stream. You should check the stream's Status property to determine whether a shorted write operation is recoverable or not.

This method is not threadsafe.

WriteCore(ReadOnlyNativeMemory, ref StreamStatus)

Writes all specified data into the stream

protected abstract nuint WriteCore(ReadOnlyNativeMemory data, ref StreamStatus status)

Parameters

data ReadOnlyNativeMemory

The memory buffer containing all the data to be written into the stream

status StreamStatus

The StreamStatus of the stream. The value of the referenced StreamStatus could change on failure and should not necessarily change on success.

Returns

nuint

The number of bytes read from the stream

See Also