From dd710ec3080183eb7cc2c0ddc32e3b2b5f03790f Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sun, 21 Feb 2021 13:37:58 +0000 Subject: [PATCH] fixed read only sub stream --- src/SharpCompress/IO/ReadOnlySubStream.cs | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/SharpCompress/IO/ReadOnlySubStream.cs b/src/SharpCompress/IO/ReadOnlySubStream.cs index 7b39a45b..fb576c09 100644 --- a/src/SharpCompress/IO/ReadOnlySubStream.cs +++ b/src/SharpCompress/IO/ReadOnlySubStream.cs @@ -34,12 +34,37 @@ namespace SharpCompress.IO public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); } + public override async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) + { + var count = buffer.Length; + if (BytesLeftToRead < count) + { + count = (int)BytesLeftToRead; + } + + if (count == 0) + { + return 0; + } + int read = await Stream.ReadAsync(buffer.Slice(0, count), cancellationToken); + if (read > 0) + { + BytesLeftToRead -= read; + } + return read; + } + public override async Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { if (BytesLeftToRead < count) { count = (int)BytesLeftToRead; } + + if (count == 0) + { + return 0; + } int read = await Stream.ReadAsync(buffer, offset, count, cancellationToken); if (read > 0) {