public static void DangerousWriteStructs <T>(this Stream output, T[] input, Int32 count) where T : struct { if (count < 1) { return; } if (count > input.Length) { throw new ArgumentOutOfRangeException(nameof(count)); } Int32 entrySize = UnsafeTypeCache <T> .UnsafeSize; Int32 sizeToWrite = entrySize * count; using (UnsafeTypeCache <Byte> .ChangeArrayTypes(input, entrySize)) output.Write((Byte[])(Array)input, 0, sizeToWrite); }
public static void DangerousReadStructs <T>(this Stream input, T[] output, Int32 count) where T : struct { if (count < 1) { return; } if (count < output.Length) { throw new ArgumentOutOfRangeException(nameof(count)); } Int32 entrySize = UnsafeTypeCache <T> .UnsafeSize; Int32 sizeToRead = entrySize * count; using (UnsafeTypeCache <Byte> .ChangeArrayTypes(output, entrySize)) input.EnsureRead((Byte[])(Array)output, 0, sizeToRead); }