1
- #if NET45_OR_GREATER || TARGETS_NETSTANDARD || TARGETS_NETCOREAPP // PUBLIC_API_CHANGES
1
+
2
+ #if NET45_OR_GREATER || TARGETS_NETSTANDARD || TARGETS_NETCOREAPP // PUBLIC_API_CHANGES
2
3
using System ;
3
4
using System . IO ;
4
5
using System . Text ;
@@ -83,9 +84,7 @@ public static string ReadAsString([NotNull] this Stream stream, [CanBeNull] Enco
83
84
{
84
85
// DO NOT dispose the reader
85
86
using ( var reader = stream . ToStreamReader ( encoding , true ) )
86
- {
87
87
return reader . ReadToEnd ( ) ;
88
- }
89
88
}
90
89
91
90
/// <summary>
@@ -100,9 +99,7 @@ public static async Task<string> ReadAsStringAsync(
100
99
{
101
100
// DO NOT dispose the reader
102
101
using ( var reader = stream . ToStreamReader ( encoding , true ) )
103
- {
104
102
return await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
105
- }
106
103
}
107
104
108
105
/// <summary>
@@ -112,13 +109,20 @@ public static async Task<string> ReadAsStringAsync(
112
109
[ NotNull ]
113
110
public static byte [ ] ReadAsByteArray ( [ NotNull ] this Stream stream )
114
111
{
115
- // DO NOT dispose the reader
116
- using ( var reader = stream . ToBinaryReader ( null , true ) )
112
+ if ( stream . CanSeek )
113
+ // DO NOT dispose underlying stream
114
+ using ( var reader = stream . ToBinaryReader ( null , true ) )
115
+ {
116
+ var readCount = checked ( ( int ) ( stream . Length - stream . Position ) ) ;
117
+ return reader . ReadBytes ( readCount ) ;
118
+ }
119
+ using ( var tempStream = new MemoryStream ( ) )
117
120
{
118
- var readCount = checked ( ( int ) ( stream . Length - stream . Position ) ) ;
119
- return reader . ReadBytes ( readCount ) ;
121
+ stream . CopyTo ( tempStream ) ;
122
+ return tempStream . ToArray ( ) ;
120
123
}
121
124
}
122
125
}
123
126
}
124
- #endif
127
+
128
+ #endif
0 commit comments