StreamExtensions
Utility class providing high performance extension methods for working with Stream
objects.
StreamExtensions is a utility class that provides extension methods for the Stream
type, including padding, writing, and reading unmanaged and marshalled structures.
Methods
AddPadding
public static void AddPadding<TStream>(this TStream stream, int alignment)
Stream.SetLength
.
AddPadding
public static void AddPadding<TStream>(this TStream stream, byte value, int alignment = 2048)
value
bytes until it is aligned to the specified alignment.
Write
public static void Write<TStream, T>(this TStream stream, Span<T> structure) where T : unmanaged
Write
public static void Write<TStream, T>(this TStream stream, in T structure) where T : unmanaged
WriteMarshalled
public static void WriteMarshalled<TStream, T>(this TStream stream, T item)
WriteMarshalled
public static void WriteMarshalled<TStream, T>(this TStream stream, T[] item)
WriteMarshalled
public static void WriteMarshalled<TStream, T>(this TStream stream, Span<T> item)
Read
public static void Read<TStream, T>(this TStream stream, out T result) where T : unmanaged
Read
public static void Read<TStream, T>(this TStream stream, T[] output) where T : unmanaged
Read
public static void Read<TStream, T>(this TStream stream, Span<T> output) where T : unmanaged
ReadMarshalled
public static void ReadMarshalled<TStream, T>(this TStream stream, out T result)
ReadMarshalled
public static void ReadMarshalled<TStream, T>(this TStream stream, T[] output)
ReadMarshalled
public static void ReadMarshalled<TStream, T>(this TStream stream, Span<T> output)
Usage
Pad Stream with Random Bytes
using var stream = new MemoryStream();
stream.AddPadding(4096);
Pad Stream with Specific Byte Value
using var stream = new MemoryStream();
stream.AddPadding(0xFF, 4096);
Write Unmanaged Structure
using var stream = new MemoryStream();
Vector2 structure = new Vector2(1, 2);
stream.Write(structure);
Write Marshalled Structure
using var stream = new MemoryStream();
MyStruct item = new MyStruct { Value1 = 1, Value2 = "Hello" };
stream.WriteMarshalled(item);
Read Unmanaged Structure
using var stream = new MemoryStream();
stream.Read(out Vector2 result);
Read Marshalled Structure
using var stream = new MemoryStream();
stream.ReadMarshalled(out MyStruct result);
Read Span of Unmanaged Structures
using var stream = new MemoryStream();
Vector2[] output = new Vector2[10];
stream.Read(output);
Read Span of Marshalled Structures
using var stream = new MemoryStream();
MyStruct[] output = new MyStruct[10];
stream.ReadMarshalled(output);