From 4c6097c969c57c46eae93d58678542c53e6e77e0 Mon Sep 17 00:00:00 2001 From: Richard Webb Date: Mon, 1 Mar 2021 15:40:35 +0000 Subject: [PATCH] Add an implementation of ReadAsync to PartialInputStream --- src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs index 704911b3f..c0bf53060 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs @@ -9,6 +9,8 @@ using System.IO; using System.Security.Cryptography; using System.Text; +using System.Threading; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Zip { @@ -4223,6 +4225,35 @@ public override int Read(byte[] buffer, int offset, int count) } } + /// + public override async Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + // lock (baseStream_) + { + if (count > end_ - readPos_) + { + count = (int)(end_ - readPos_); + if (count == 0) + { + return 0; + } + } + // Protect against Stream implementations that throw away their buffer on every Seek + // (for example, Mono FileStream) + if (baseStream_.Position != readPos_) + { + baseStream_.Seek(readPos_, SeekOrigin.Begin); + } + + int readCount = await baseStream_.ReadAsync(buffer, offset, count, cancellationToken); + if (readCount > 0) + { + readPos_ += readCount; + } + return readCount; + } + } + /// /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. ///