Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wip] Add an implementation of ReadAsync to PartialInputStream #589

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -4223,6 +4225,35 @@ public override int Read(byte[] buffer, int offset, int count)
}
}

///<inheritdoc/>
public override async Task<int> 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;
}
}

/// <summary>
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
/// </summary>
Expand Down