44using System ;
55using System . Buffers ;
66using System . IO ;
7-
87using SixLabors . ImageSharp . Memory ;
98using SixLabors . Memory ;
109
@@ -15,7 +14,6 @@ namespace SixLabors.ImageSharp
1514 /// </summary>
1615 internal static class StreamExtensions
1716 {
18- #if NETCOREAPP2_1
1917 /// <summary>
2018 /// Writes data from a stream into the provided buffer.
2119 /// </summary>
@@ -24,23 +22,18 @@ internal static class StreamExtensions
2422 /// <param name="offset">The offset within the buffer to begin writing.</param>
2523 /// <param name="count">The number of bytes to write to the stream.</param>
2624 public static void Write ( this Stream stream , Span < byte > buffer , int offset , int count )
27- {
28- stream . Write ( buffer . Slice ( offset , count ) ) ;
29- }
25+ => stream . Write ( buffer . Slice ( offset , count ) ) ;
3026
3127 /// <summary>
3228 /// Reads data from a stream into the provided buffer.
3329 /// </summary>
3430 /// <param name="stream">The stream.</param>
35- /// <param name="buffer">The buffer.. </param>
31+ /// <param name="buffer">The buffer.</param>
3632 /// <param name="offset">The offset within the buffer where the bytes are read into.</param>
3733 /// <param name="count">The number of bytes, if available, to read.</param>
3834 /// <returns>The actual number of bytes read.</returns>
3935 public static int Read ( this Stream stream , Span < byte > buffer , int offset , int count )
40- {
41- return stream . Read ( buffer . Slice ( offset , count ) ) ;
42- }
43- #endif
36+ => stream . Read ( buffer . Slice ( offset , count ) ) ;
4437
4538 /// <summary>
4639 /// Skips the number of bytes in the given stream.
@@ -75,17 +68,39 @@ public static void Skip(this Stream stream, int count)
7568 }
7669
7770 public static void Read ( this Stream stream , IManagedByteBuffer buffer )
78- {
79- stream . Read ( buffer . Array , 0 , buffer . Length ( ) ) ;
80- }
71+ => stream . Read ( buffer . Array , 0 , buffer . Length ( ) ) ;
8172
8273 public static void Write ( this Stream stream , IManagedByteBuffer buffer )
74+ => stream . Write ( buffer . Array , 0 , buffer . Length ( ) ) ;
75+
76+ #if ! SUPPORTS_SPAN_STREAM
77+ // This is a port of the CoreFX implementation and is MIT Licensed:
78+ // https://github.com/dotnet/corefx/blob/17300169760c61a90cab8d913636c1058a30a8c1/src/Common/src/CoreLib/System/IO/Stream.cs#L742
79+ public static int Read ( this Stream stream , Span < byte > buffer )
8380 {
84- stream . Write ( buffer . Array , 0 , buffer . Length ( ) ) ;
81+ // This uses ArrayPool<byte>.Shared, rather than taking a MemoryAllocator,
82+ // in order to match the signature of the framework method that exists in
83+ // .NET Core.
84+ byte [ ] sharedBuffer = ArrayPool < byte > . Shared . Rent ( buffer . Length ) ;
85+ try
86+ {
87+ int numRead = stream . Read ( sharedBuffer , 0 , buffer . Length ) ;
88+ if ( ( uint ) numRead > ( uint ) buffer . Length )
89+ {
90+ throw new IOException ( "Stream was too long." ) ;
91+ }
92+
93+ new Span < byte > ( sharedBuffer , 0 , numRead ) . CopyTo ( buffer ) ;
94+ return numRead ;
95+ }
96+ finally
97+ {
98+ ArrayPool < byte > . Shared . Return ( sharedBuffer ) ;
99+ }
85100 }
86101
87- #if NET472 || NETSTANDARD1_3 || NETSTANDARD2_0
88- // This is a port of the CoreFX implementation and is MIT Licensed: https://github.com/dotnet/coreclr /blob/c4dca1072d15bdda64c754ad1ea474b1580fa554 /src/System.Private.CoreLib/shared/ System/IO/Stream.cs#L770
102+ // This is a port of the CoreFX implementation and is MIT Licensed:
103+ // https://github.com/dotnet/corefx /blob/17300169760c61a90cab8d913636c1058a30a8c1 /src/Common/src/CoreLib/ System/IO/Stream.cs#L775
89104 public static void Write ( this Stream stream , ReadOnlySpan < byte > buffer )
90105 {
91106 // This uses ArrayPool<byte>.Shared, rather than taking a MemoryAllocator,
0 commit comments