add ReadToEnd function

This commit is contained in:
Michael Becker 2024-07-21 22:36:57 -04:00
parent c275c017c1
commit 9df55b9c08
Signed by: beckermj
GPG Key ID: 24F8DAA73DCB2C8F

View File

@ -96,4 +96,33 @@ public static class StreamExtensions
} }
_streamPositions[st].Push(st.Position); _streamPositions[st].Push(st.Position);
} }
private const long BUFFER_SIZE=4096;
public static byte[] ReadToEnd(this Stream st)
{
byte[] buffer = new byte[BUFFER_SIZE];
byte[] output = new byte[0];
int i = 0;
bool done = false;
while (!done)
{
int length = st.Read(buffer, 0, buffer.Length);
if (length == 0)
{
done = true;
break;
}
if (length < BUFFER_SIZE)
{
Array.Resize<byte>(ref buffer, length);
done = true;
}
Array.Resize<byte>(ref output, output.Length + buffer.Length);
Array.Copy(buffer, 0, output, i, buffer.Length);
i += buffer.Length;
}
return output;
}
} }