Files
sharpcompress/src/SharpCompress/Polyfills/StreamExtensions.cs
Adam Hathcock 959bbdcf1b big clean up
2022-12-20 15:06:44 +00:00

47 lines
916 B
C#

#if NETFRAMEWORK || NETSTANDARD2_0
using System;
using System.Buffers;
using System.IO;
namespace SharpCompress;
internal static class StreamExtensions
{
internal static int Read(this Stream stream, Span<byte> buffer)
{
var temp = ArrayPool<byte>.Shared.Rent(buffer.Length);
try
{
var read = stream.Read(temp, 0, buffer.Length);
temp.AsSpan(0, read).CopyTo(buffer);
return read;
}
finally
{
ArrayPool<byte>.Shared.Return(temp);
}
}
internal static void Write(this Stream stream, ReadOnlySpan<byte> buffer)
{
var temp = ArrayPool<byte>.Shared.Rent(buffer.Length);
buffer.CopyTo(temp);
try
{
stream.Write(temp, 0, buffer.Length);
}
finally
{
ArrayPool<byte>.Shared.Return(temp);
}
}
}
#endif