Skip to content

Flesh out ByteRangeStream methods to make it always limit reads #213

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

Merged
merged 2 commits into from
Nov 28, 2018

Conversation

dougbu
Copy link
Contributor

@dougbu dougbu commented Nov 25, 2018

  • ByteRangeStream does not limit CopyToAsync(...) range as it should #206
  • Seek(...) was a no-op, Position was incorrect if _lowerbounds != 0, ReadAsync(...) read entire inner Stream
    • rewrite ByteRangeStreamTest to cover new ByteRangeStream members and hold fewer resources
  • remove DelegatingStream.CopyToAsync(...) override because it copied entire inner Stream and was not needed
    • base implementation invokes ReadAsync(...)

@dougbu
Copy link
Contributor Author

dougbu commented Nov 25, 2018

FYI @stephentoub

/// </summary>
/// <param name="count">The count requested to be read by the caller.</param>
/// <returns>The remaining bytes to read within the range defined for this stream.</returns>
private int PrepareStreamForRangeRead(int count)
{
long effectiveCount = Math.Min(count, _totalCount - _currentCount);
if (effectiveCount > 0)
// A negative count causes base.Raad* methods to throw an ArgumentOutOfRangeException.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrote method because _currentCount may now exceed _totalCount and we need to distinguish that case (a no-op) from count < 0 (an error).

_currentCount = offset;
break;
case SeekOrigin.Current:
_currentCount = unchecked(_currentCount + offset);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/unchecked:

Expressions that contain non-constant terms are unchecked by default at compile time and run time.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, seems you don't need the unchecked context here.

Copy link

@mkArtakMSFT mkArtakMSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment regarding the unchecked context. The rest looks good.

- #206
- `Seek(...)` was a no-op, `Position` was incorrect if `_lowerbounds != 0`, `ReadAsync(...)` read entire inner `Stream`
  - rewrite `ByteRangeStreamTest` to cover new `ByteRangeStream` members and hold fewer resources
- remove `DelegatingStream.CopyToAsync(...)` override because it copied entire inner `Stream` and was not needed
  - base implementation invokes `ReadAsync(...)`
@dougbu dougbu force-pushed the dougbu/remove.copytoasync.206 branch from 2d675b0 to ca41472 Compare November 28, 2018 00:46
@dougbu
Copy link
Contributor Author

dougbu commented Nov 28, 2018

🆙📅 to rebase and address @mkArtakMSFT's comment.
Will merge once the CI check passes.

@dougbu dougbu merged commit 39d3064 into master Nov 28, 2018
@dougbu dougbu deleted the dougbu/remove.copytoasync.206 branch November 28, 2018 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants