2
2
//License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
3
3
4
4
using System ;
5
+ using System . Buffers . Text ;
5
6
using System . Collections . Generic ;
6
7
using System . IO ;
7
8
using System . Linq ;
10
11
using System . Threading ;
11
12
using System . Threading . Tasks ;
12
13
using ServiceStack . Text ;
14
+ using ServiceStack . Text . Pools ;
13
15
14
16
namespace ServiceStack
15
17
{
@@ -58,13 +60,9 @@ public static IEnumerable<string> ReadLines(this Stream stream)
58
60
public const int DefaultBufferSize = 8 * 1024 ;
59
61
60
62
/// <summary>
61
- /// Reads the given stream up to the end, returning the data as a byte
62
- /// array.
63
+ /// Reads the given stream up to the end, returning the data as a byte array.
63
64
/// </summary>
64
- public static byte [ ] ReadFully ( this Stream input )
65
- {
66
- return ReadFully ( input , DefaultBufferSize ) ;
67
- }
65
+ public static byte [ ] ReadFully ( this Stream input ) => ReadFully ( input , DefaultBufferSize ) ;
68
66
69
67
/// <summary>
70
68
/// Reads the given stream up to the end, returning the data as a byte
@@ -75,7 +73,15 @@ public static byte[] ReadFully(this Stream input, int bufferSize)
75
73
if ( bufferSize < 1 )
76
74
throw new ArgumentOutOfRangeException ( nameof ( bufferSize ) ) ;
77
75
78
- return ReadFully ( input , new byte [ bufferSize ] ) ;
76
+ byte [ ] buffer = BufferPool . GetBuffer ( bufferSize ) ;
77
+ try
78
+ {
79
+ return ReadFully ( input , buffer ) ;
80
+ }
81
+ finally
82
+ {
83
+ BufferPool . ReleaseBufferToPool ( ref buffer ) ;
84
+ }
79
85
}
80
86
81
87
/// <summary>
@@ -110,6 +116,45 @@ public static byte[] ReadFully(this Stream input, byte[] buffer)
110
116
}
111
117
}
112
118
119
+ /// <summary>
120
+ /// Reads the given stream up to the end, returning the MemoryStream Buffer as ReadOnlyMemory<byte>.
121
+ /// </summary>
122
+ public static ReadOnlyMemory < byte > ReadFullyAsMemory ( this Stream input ) =>
123
+ ReadFullyAsMemory ( input , DefaultBufferSize ) ;
124
+
125
+ /// <summary>
126
+ /// Reads the given stream up to the end, returning the MemoryStream Buffer as ReadOnlyMemory<byte>.
127
+ /// </summary>
128
+ public static ReadOnlyMemory < byte > ReadFullyAsMemory ( this Stream input , int bufferSize )
129
+ {
130
+ byte [ ] buffer = BufferPool . GetBuffer ( bufferSize ) ;
131
+ try
132
+ {
133
+ return ReadFullyAsMemory ( input , buffer ) ;
134
+ }
135
+ finally
136
+ {
137
+ BufferPool . ReleaseBufferToPool ( ref buffer ) ;
138
+ }
139
+ }
140
+
141
+ public static ReadOnlyMemory < byte > ReadFullyAsMemory ( this Stream input , byte [ ] buffer )
142
+ {
143
+ if ( buffer == null )
144
+ throw new ArgumentNullException ( nameof ( buffer ) ) ;
145
+
146
+ if ( input == null )
147
+ throw new ArgumentNullException ( nameof ( input ) ) ;
148
+
149
+ if ( buffer . Length == 0 )
150
+ throw new ArgumentException ( "Buffer has length of 0" ) ;
151
+
152
+ var ms = new MemoryStream ( ) ;
153
+ CopyTo ( input , ms , buffer ) ;
154
+ return ms . GetBufferAsMemory ( ) ;
155
+ }
156
+
157
+
113
158
/// <summary>
114
159
/// Copies all the data from one stream into another.
115
160
/// </summary>
0 commit comments