Skip to content

Commit c733ac9

Browse files
Merge pull request #66 from crispthinking/feature/buffer-stream
Support non-seekable streams in FromStream method
2 parents 81507b6 + f1b0684 commit c733ac9

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/PdqHash/PdqHasher.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ private static void ComputeDCTMatrix(Memory<float> memory)
6666
public HashResult? FromStream(Stream input, string source)
6767
{
6868
var stopwatch = Stopwatch.StartNew();
69-
69+
70+
using var bufferedStream = input.CanSeek ? null : new MemoryStream();
71+
if (bufferedStream != null)
72+
{
73+
input.CopyTo(bufferedStream);
74+
bufferedStream.Position = 0;
75+
input = bufferedStream;
76+
}
77+
7078
using var codec = SKCodec.Create(input, out var result);
7179

7280
if (codec == null)

test/PdqHash.Tests/Streams/StreamParsingTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public async Task EnsureHashingWithNonSeekableStreams()
6262
return hasher.FromStream(pipe.Reader.AsStream(), "");
6363
});
6464

65-
var writeTask = Task.Run(() => pipe.Writer.WriteAsync(bytes));
65+
var writeTask = Task.Run(async () =>
66+
{
67+
await pipe.Writer.WriteAsync(bytes);
68+
await pipe.Writer.CompleteAsync();
69+
});
6670

6771
await Task.WhenAll(hashTask, writeTask);
6872

0 commit comments

Comments
 (0)