diff --git a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems index 0cf0f324..3d8e7be8 100644 --- a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems +++ b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems @@ -15,6 +15,7 @@ + diff --git a/src/ComputeSharp.NetStandard/System/IO/StreamExtensions.cs b/src/ComputeSharp.NetStandard/System/IO/StreamExtensions.cs new file mode 100644 index 00000000..42347133 --- /dev/null +++ b/src/ComputeSharp.NetStandard/System/IO/StreamExtensions.cs @@ -0,0 +1,54 @@ +using System.Buffers; + +namespace System.IO; + +/// +/// A polyfill type with extensions for . +/// +internal static class StreamExtensions +{ + /// + /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. + /// + /// The input instance. + /// A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source. + /// The total number of bytes read into the buffer. + public static int Read(this Stream stream, Span span) + { + if (span.IsEmpty) + { + return 0; + } + + byte[] array = ArrayPool.Shared.Rent(span.Length); + + int bytesRead = stream.Read(array, 0, span.Length); + + array.AsSpan(0, bytesRead).CopyTo(span); + + ArrayPool.Shared.Return(array); + + return bytesRead; + } + + /// + /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. + /// + /// The input instance. + /// A region of memory. This method copies the contents of this region to the current stream. + public static void Write(this Stream stream, Span span) + { + if (span.IsEmpty) + { + return; + } + + byte[] array = ArrayPool.Shared.Rent(span.Length); + + span.CopyTo(array); + + stream.Write(array, 0, span.Length); + + ArrayPool.Shared.Return(array); + } +}