Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikus1993 committed Mar 2, 2024
1 parent b2e7d6d commit ba75e6d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/BuildingBlocks/Common/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
using System.Buffers;

namespace Common.Extensions;


internal sealed class BytesStreamReader : IDisposable
{
private readonly bool _disposeStreamAfterRead;
private static readonly ArrayPool<byte> ArrayPool = ArrayPool<byte>.Shared;
private readonly byte[] _bytes;
private readonly Stream _stream;

public BytesStreamReader(Stream stream, int bufferSize = 8, bool disposeStreamAfterRead = false)
{
_stream = stream;
_disposeStreamAfterRead = disposeStreamAfterRead;
_bytes = ArrayPool.Rent(bufferSize);
}

public bool SequenceEqual(byte[] arr)
{
var readBytes = _stream.ReadAtLeast(_bytes, _bytes.Length, false);
var arrSpan = arr.AsSpan();
var bytesSpan = _bytes.AsSpan(0, readBytes);
return arrSpan.SequenceEqual(bytesSpan);
}

public void Dispose()
{
if (_disposeStreamAfterRead)
{
_stream.Dispose();
}
ArrayPool.Return(_bytes);
}

}

public static class StreamExtensions
{
public static byte[] ReadBytes(this Stream stream, int count)
Expand Down

0 comments on commit ba75e6d

Please sign in to comment.