using System; using System.IO; namespace ADOFAIRenderer.Util { public static class StreamUtil { public static void WriteNativePtrToStream(IntPtr nativePtr, int length, Stream destinationStream) { if (nativePtr == IntPtr.Zero) throw new ArgumentNullException(nameof(nativePtr)); if (length <= 0) return; unsafe { // Create an unmanaged memory stream that wraps the native pointer // (This does not allocate new memory; it references the existing pointer) using var unmanagedStream = new UnmanagedMemoryStream((byte*)nativePtr, length); // Copy the data directly into your target stream unmanagedStream.CopyTo(destinationStream); } } } }