Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit b07bd02

Browse files
committed
Update ReadFully to use BufferPool + add more efficient ReadFullyAsMemory
1 parent c75de69 commit b07bd02

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

src/ServiceStack.Text/StreamExtensions.cs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
33

44
using System;
5+
using System.Buffers.Text;
56
using System.Collections.Generic;
67
using System.IO;
78
using System.Linq;
@@ -10,6 +11,7 @@
1011
using System.Threading;
1112
using System.Threading.Tasks;
1213
using ServiceStack.Text;
14+
using ServiceStack.Text.Pools;
1315

1416
namespace 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&lt;byte&gt;.
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&lt;byte&gt;.
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

Comments
 (0)