22//License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
33
44using System ;
5+ using System . Buffers . Text ;
56using System . Collections . Generic ;
67using System . IO ;
78using System . Linq ;
1011using System . Threading ;
1112using System . Threading . Tasks ;
1213using ServiceStack . Text ;
14+ using ServiceStack . Text . Pools ;
1315
1416namespace ServiceStack
1517{
@@ -58,13 +60,9 @@ public static IEnumerable<string> ReadLines(this Stream stream)
5860 public const int DefaultBufferSize = 8 * 1024 ;
5961
6062 /// <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.
6364 /// </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 ) ;
6866
6967 /// <summary>
7068 /// 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)
7573 if ( bufferSize < 1 )
7674 throw new ArgumentOutOfRangeException ( nameof ( bufferSize ) ) ;
7775
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+ }
7985 }
8086
8187 /// <summary>
@@ -110,6 +116,45 @@ public static byte[] ReadFully(this Stream input, byte[] buffer)
110116 }
111117 }
112118
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+
113158 /// <summary>
114159 /// Copies all the data from one stream into another.
115160 /// </summary>
0 commit comments