Class StreamExtensions
- Namespace
- Garyon.Extensions
- Assembly
- Garyon.dll
Provides extensions for the Stream class.
public static class StreamExtensions
- Inheritance
-
StreamExtensions
- Inherited Members
Methods
ReachedEnd(Stream)
Determines whether a Stream has reached its end.
public static bool ReachedEnd(this Stream stream)
Parameters
Returns
ReadASCIIString(Stream, long)
Reads a string encoded in ASCII from the stream at the current position.
public static string ReadASCIIString(this Stream stream, long byteLength)
Parameters
streamStreamThe stream to read from.
byteLengthlongThe number of bytes to read. This does not necessarily equal the number of characters the resulting string contains.
Returns
ReadASCIIStringAt(Stream, long, long)
Reads a string encoded in ASCII from the stream at the specified position.
public static string ReadASCIIStringAt(this Stream stream, long position, long byteLength)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
byteLengthlongThe number of bytes to read. This does not necessarily equal the number of characters the resulting string contains.
Returns
ReadInt16(Stream)
Reads a short from the stream at the current position.
public static short ReadInt16(this Stream stream)
Parameters
streamStreamThe stream to read from.
Returns
- short
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadInt16At(Stream, long)
Reads a short from the stream at the specified position.
public static short ReadInt16At(this Stream stream, long position)
Parameters
Returns
- short
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadInt32(Stream)
Reads an int from the stream at the current position.
public static int ReadInt32(this Stream stream)
Parameters
streamStreamThe stream to read from.
Returns
- int
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadInt32At(Stream, long)
Reads an int from the stream at the specified position.
public static int ReadInt32At(this Stream stream, long position)
Parameters
Returns
- int
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadInt64(Stream)
Reads a long from the stream at the current position.
public static long ReadInt64(this Stream stream)
Parameters
streamStreamThe stream to read from.
Returns
- long
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadInt64At(Stream, long)
Reads a long from the stream at the specified position.
public static long ReadInt64At(this Stream stream, long position)
Parameters
Returns
- long
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadSByte(Stream)
Reads a sbyte from the stream at the current position.
public static sbyte ReadSByte(this Stream stream)
Parameters
streamStreamThe stream to read from.
Returns
- sbyte
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadSByteAt(Stream, long)
Reads a sbyte from the stream at the specified position.
public static sbyte ReadSByteAt(this Stream stream, long position)
Parameters
Returns
- sbyte
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadString(Stream, long, Encoding)
Reads a string encoded in a specified encoding from the stream at the current position.
public static string ReadString(this Stream stream, long byteLength, Encoding encoding)
Parameters
streamStreamThe stream to read from.
byteLengthlongThe number of bytes to read. This does not necessarily equal the number of characters the resulting string contains.
encodingEncodingThe encoding of the string.
Returns
ReadStringAt(Stream, long, long, Encoding)
Reads a string encoded in a specified encoding from the stream at the specified position.
public static string ReadStringAt(this Stream stream, long position, long byteLength, Encoding encoding)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
byteLengthlongThe number of bytes to read. This does not necessarily equal the number of characters the resulting string contains.
encodingEncodingThe encoding of the string.
Returns
ReadUInt16(Stream)
Reads a ushort from the stream at the current position.
public static ushort ReadUInt16(this Stream stream)
Parameters
streamStreamThe stream to read from.
Returns
- ushort
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadUInt16At(Stream, long)
Reads a ushort from the stream at the specified position.
public static ushort ReadUInt16At(this Stream stream, long position)
Parameters
Returns
- ushort
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadUInt32(Stream)
Reads a uint from the stream at the current position.
public static uint ReadUInt32(this Stream stream)
Parameters
streamStreamThe stream to read from.
Returns
- uint
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadUInt32At(Stream, long)
Reads a uint from the stream at the specified position.
public static uint ReadUInt32At(this Stream stream, long position)
Parameters
Returns
- uint
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadUInt64(Stream)
Reads a ulong from the stream at the current position.
public static ulong ReadUInt64(this Stream stream)
Parameters
streamStreamThe stream to read from.
Returns
- ulong
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadUInt64At(Stream, long)
Reads a ulong from the stream at the specified position.
public static ulong ReadUInt64At(this Stream stream, long position)
Parameters
Returns
- ulong
The read value from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadUTF32String(Stream, long)
Reads a string encoded in UTF-32 from the stream at the current position.
public static string ReadUTF32String(this Stream stream, long byteLength)
Parameters
streamStreamThe stream to read from.
byteLengthlongThe number of bytes to read. This does not necessarily equal the number of characters the resulting string contains.
Returns
ReadUTF32StringAt(Stream, long, long)
Reads a string encoded in UTF-32 from the stream at the specified position.
public static string ReadUTF32StringAt(this Stream stream, long position, long byteLength)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
byteLengthlongThe number of bytes to read. This does not necessarily equal the number of characters the resulting string contains.
Returns
ReadUTF8String(Stream, long)
Reads a string encoded in UTF-8 from the stream at the current position.
public static string ReadUTF8String(this Stream stream, long byteLength)
Parameters
streamStreamThe stream to read from.
byteLengthlongThe number of bytes to read. This does not necessarily equal the number of characters the resulting string contains.
Returns
ReadUTF8StringAt(Stream, long, long)
Reads a string encoded in UTF-8 from the stream at the specified position.
public static string ReadUTF8StringAt(this Stream stream, long position, long byteLength)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
byteLengthlongThe number of bytes to read. This does not necessarily equal the number of characters the resulting string contains.
Returns
ReadUnicodeString(Stream, long)
Reads a string encoded in Unicode (UTF-16) from the stream at the current position.
public static string ReadUnicodeString(this Stream stream, long byteLength)
Parameters
streamStreamThe stream to read from.
byteLengthlongThe number of bytes to read. This does not necessarily equal the number of characters the resulting string contains.
Returns
ReadUnicodeStringAt(Stream, long, long)
Reads a string encoded in Unicode (UTF-16) from the stream at the specified position.
public static string ReadUnicodeStringAt(this Stream stream, long position, long byteLength)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
byteLengthlongThe number of bytes to read. This does not necessarily equal the number of characters the resulting string contains.
Returns
ReadValueAt<T>(Stream, long)
Reads a value from the stream at the specified position.
public static T ReadValueAt<T>(this Stream stream, long position) where T : unmanaged
Parameters
Returns
- T
The read value from the stream.
Type Parameters
TThe type of the value to read from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
ReadValue<T>(Stream)
Reads a value from the stream at the current position.
public static T ReadValue<T>(this Stream stream) where T : unmanaged
Parameters
streamStreamThe stream to read from.
Returns
- T
The read value from the stream.
Type Parameters
TThe type of the value to read from the stream.
Exceptions
- EndOfStreamException
Thrown when the stream would not provide enough bytes to read.
RemainingBytes(Stream)
Calculates the remaining number of bytes that can be read from the Stream before reaching its end.
public static long RemainingBytes(this Stream stream)
Parameters
Returns
ResetPosition(Stream)
Resets a stream's position to 0.
public static void ResetPosition(this Stream stream)
Parameters
ToByteArray(Stream)
Reads all the contents of the stream and stores them to a newly initialized byte array.
public static byte[] ToByteArray(this Stream stream)
Parameters
streamStreamThe stream whose contents will be read and written into the target array.
Returns
Remarks
Be careful when using this method on very large streams, all the contents will be copied over to the memory, which is far more limited than storage.
TryReadByte(Stream, out byte)
Reads a byte from the stream at the current position.
public static bool TryReadByte(this Stream stream, out byte value)
Parameters
streamStreamThe stream to read from.
valuebyteThe value. Its value is always equal to the first byte of the int that ReadByte() returns. Only use the value if the method returns true.
Returns
- bool
true if the byte was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadByteAt(Stream, long, out byte)
Reads a byte from the stream at the specified position.
public static bool TryReadByteAt(this Stream stream, long position, out byte value)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
valuebyteThe value. Its value is always equal to the first byte of the int that ReadByte() returns. Only use the value if the method returns true.
Returns
- bool
true if the byte was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadInt16At(Stream, long, out short)
Reads a short from the stream at the specified position.
public static bool TryReadInt16At(this Stream stream, long position, out short value)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
valueshortThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the short was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadInt16<T>(Stream, out short)
Reads a short from the stream at the current position.
public static bool TryReadInt16<T>(this Stream stream, out short value)
Parameters
streamStreamThe stream to read from.
valueshortThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the short was successfully read from the stream, otherwise false, if the stream has reached its end.
Type Parameters
T
TryReadInt32(Stream, out int)
Reads an int from the stream at the current position.
public static bool TryReadInt32(this Stream stream, out int value)
Parameters
streamStreamThe stream to read from.
valueintThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the int was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadInt32At(Stream, long, out int)
Reads an int from the stream at the specified position.
public static bool TryReadInt32At(this Stream stream, long position, out int value)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
valueintThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the int was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadInt64(Stream, out long)
Reads a long from the stream at the current position.
public static bool TryReadInt64(this Stream stream, out long value)
Parameters
streamStreamThe stream to read from.
valuelongThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the long was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadInt64At(Stream, long, out long)
Reads a long from the stream at the specified position.
public static bool TryReadInt64At(this Stream stream, long position, out long value)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
valuelongThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the long was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadSByte(Stream, out sbyte)
Reads a sbyte from the stream at the current position.
public static bool TryReadSByte(this Stream stream, out sbyte value)
Parameters
streamStreamThe stream to read from.
valuesbyteThe value. Its value is always equal to the first byte of the int that ReadByte() returns. Only use the value if the method returns true.
Returns
- bool
true if the sbyte was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadSByteAt(Stream, long, out sbyte)
Reads a sbyte from the stream at the specified position.
public static bool TryReadSByteAt(this Stream stream, long position, out sbyte value)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
valuesbyteThe value. Its value is always equal to the first byte of the int that ReadByte() returns. Only use the value if the method returns true.
Returns
- bool
true if the sbyte was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadUInt16(Stream, out ushort)
Reads a ushort from the stream at the current position.
public static bool TryReadUInt16(this Stream stream, out ushort value)
Parameters
streamStreamThe stream to read from.
valueushortThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the ushort was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadUInt16At(Stream, long, out ushort)
Reads a ushort from the stream at the specified position.
public static bool TryReadUInt16At(this Stream stream, long position, out ushort value)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
valueushortThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the ushort was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadUInt32(Stream, out uint)
Reads a uint from the stream at the current position.
public static bool TryReadUInt32(this Stream stream, out uint value)
Parameters
streamStreamThe stream to read from.
valueuintThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the uint was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadUInt32At(Stream, long, out uint)
Reads a uint from the stream at the specified position.
public static bool TryReadUInt32At(this Stream stream, long position, out uint value)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
valueuintThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the uint was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadUInt64(Stream, out ulong)
Reads a ulong from the stream at the current position.
public static bool TryReadUInt64(this Stream stream, out ulong value)
Parameters
streamStreamThe stream to read from.
valueulongThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the ulong was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadUInt64At(Stream, long, out ulong)
Reads a ulong from the stream at the specified position.
public static bool TryReadUInt64At(this Stream stream, long position, out ulong value)
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
valueulongThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the ulong was successfully read from the stream, otherwise false, if the stream has reached its end.
TryReadValueAt<T>(Stream, long, out T)
Reads a T from the stream at the specified
position.
public static bool TryReadValueAt<T>(this Stream stream, long position, out T value) where T : unmanaged
Parameters
streamStreamThe stream to read from.
positionlongThe position in the stream to read from.
valueTThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the ushort was successfully read from the stream, otherwise false, if the stream has reached its end.
Type Parameters
TThe type of the value to read.
TryReadValue<T>(Stream, out T)
Reads a T from the stream at the current
position.
public static bool TryReadValue<T>(this Stream stream, out T value) where T : unmanaged
Parameters
streamStreamThe stream to read from.
valueTThe value. Its value is always equal to the buffer after calling the Read(Span<byte>) function on it. Only use the value if the method returns true.
Returns
- bool
true if the ushort was successfully read from the stream, otherwise false, if the stream has reached its end.
Type Parameters
TThe type of the value to read.
Write(Stream, byte)
Writes a byte to the stream at the current position.
public static void Write(this Stream stream, byte value)
Parameters
Write(Stream, byte[])
Writes all contents of a byte buffer to the stream at the current position.
public static void Write(this Stream stream, byte[] buffer)
Parameters
Write(Stream, short)
Writes a short to the stream at the current position.
public static void Write(this Stream stream, short value)
Parameters
Write(Stream, int)
Writes a int to the stream at the current position.
public static void Write(this Stream stream, int value)
Parameters
Write(Stream, long)
Writes a long to the stream at the current position.
public static void Write(this Stream stream, long value)
Parameters
Write(Stream, long, byte[])
Writes all contents of a byte buffer to the stream at the current position.
public static void Write(this Stream stream, long position, byte[] buffer)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
bufferbyte[]The buffer to write to the stream.
Write(Stream, sbyte)
Writes a sbyte to the stream at the current position.
public static void Write(this Stream stream, sbyte value)
Parameters
Write(Stream, ushort)
Writes a ushort to the stream at the current position.
public static void Write(this Stream stream, ushort value)
Parameters
Write(Stream, uint)
Writes a uint to the stream at the current position.
public static void Write(this Stream stream, uint value)
Parameters
Write(Stream, ulong)
Writes a ulong to the stream at the current position.
public static void Write(this Stream stream, ulong value)
Parameters
WriteASCIIString(Stream, string)
Writes a string encoded in ASCII to the stream at the current position.
public static void WriteASCIIString(this Stream stream, string s)
Parameters
WriteASCIIStringAt(Stream, long, string)
Writes a string encoded in ASCII to the stream at the specified position.
public static void WriteASCIIStringAt(this Stream stream, long position, string s)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
sstringThe string to write to the stream.
WriteAt(Stream, long, byte)
Writes a byte to the stream at the specified position.
public static void WriteAt(this Stream stream, long position, byte value)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
valuebyteThe value to write to the stream.
WriteAt(Stream, long, byte[])
Writes a byte[] to the stream at the specified position.
public static void WriteAt(this Stream stream, long position, byte[] bytes)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
bytesbyte[]The bytes to write to the stream.
WriteAt(Stream, long, short)
Writes a short to the stream at the specified position.
public static void WriteAt(this Stream stream, long position, short value)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
valueshortThe value to write to the stream.
WriteAt(Stream, long, int)
Writes an int to the stream at the specified position.
public static void WriteAt(this Stream stream, long position, int value)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
valueintThe value to write to the stream.
WriteAt(Stream, long, long)
Writes a long to the stream at the specified position.
public static void WriteAt(this Stream stream, long position, long value)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
valuelongThe value to write to the stream.
WriteAt(Stream, long, sbyte)
Writes a sbyte to the stream at the specified position.
public static void WriteAt(this Stream stream, long position, sbyte value)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
valuesbyteThe value to write to the stream.
WriteAt(Stream, long, ushort)
Writes a ushort to the stream at the specified position.
public static void WriteAt(this Stream stream, long position, ushort value)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
valueushortThe value to write to the stream.
WriteAt(Stream, long, uint)
Writes a uint to the stream at the specified position.
public static void WriteAt(this Stream stream, long position, uint value)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
valueuintThe value to write to the stream.
WriteAt(Stream, long, ulong)
Writes a ulong to the stream at the specified position.
public static void WriteAt(this Stream stream, long position, ulong value)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
valueulongThe value to write to the stream.
WriteAt<T>(Stream, long, T)
Writes a value to the stream at the specified position.
public static void WriteAt<T>(this Stream stream, long position, T value) where T : unmanaged
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
valueTThe value to write to the stream.
Type Parameters
TThe type of the value to write.
WriteString(Stream, string, Encoding)
Writes a string encoded in a specified encoding to the stream at the current position.
public static void WriteString(this Stream stream, string s, Encoding encoding)
Parameters
streamStreamThe stream to write to.
sstringThe string to write to the stream.
encodingEncodingThe encoding of the string.
WriteStringAt(Stream, long, string, Encoding)
Writes a string encoded in a specified encoding to the stream at the specified position.
public static void WriteStringAt(this Stream stream, long position, string s, Encoding encoding)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
sstringThe string to write to the stream.
encodingEncodingThe encoding of the string.
WriteUTF32String(Stream, string)
Writes a string encoded in UTF-32 to the stream at the current position.
public static void WriteUTF32String(this Stream stream, string s)
Parameters
WriteUTF32StringAt(Stream, long, string)
Writes a string encoded in UTF-32 to the stream at the specified position.
public static void WriteUTF32StringAt(this Stream stream, long position, string s)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
sstringThe string to write to the stream.
WriteUTF8String(Stream, string)
Writes a string encoded in UTF-8 to the stream at the current position.
public static void WriteUTF8String(this Stream stream, string s)
Parameters
WriteUTF8StringAt(Stream, long, string)
Writes a string encoded in UTF-8 to the stream at the specified position.
public static void WriteUTF8StringAt(this Stream stream, long position, string s)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
sstringThe string to write to the stream.
WriteUnicodeString(Stream, string)
Writes a string encoded in Unicode (UTF-16) to the stream at the current position.
public static void WriteUnicodeString(this Stream stream, string s)
Parameters
WriteUnicodeStringAt(Stream, long, string)
Writes a string encoded in Unicode (UTF-16) to the stream at the specified position.
public static void WriteUnicodeStringAt(this Stream stream, long position, string s)
Parameters
streamStreamThe stream to write to.
positionlongThe position in the stream to write at.
sstringThe string to write to the stream.
Write<T>(Stream, T)
Writes a value to the stream at the current position.
public static void Write<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 to the stream.