Skip to content

Conversation

@kundadebdatta
Copy link
Member

Pull Request Template

Description

Please include a summary of the change and which issue is fixed. Include samples if adding new API, and include relevant motivation and context. List any dependencies that are required for this change.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

Closing issues

To automatically close an issue: closes #IssueNumber

public byte[] ReadAll()
{
ArraySegment<byte> byteSegment = this.innerStream.GetBuffer();
int count, totalBytes = 0, offset = (int)this.Position, length = (int)this.Length;
Copy link
Member

Choose a reason for hiding this comment

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

NIT - I admit personal opinion - but I find it unreadable when multiple variables are declared in one line.

int count, totalBytes = 0, offset = (int)this.Position, length = (int)this.Length;
byte[] bytes = new byte[length];

while ((count = this.innerStream.Read(bytes, offset, length - offset)) > 0)
Copy link
Member

Choose a reason for hiding this comment

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

The implementation here is functionally incorrect when you first use Stream.Read and subsequently Stream.ReadAll - for example you would copy the first byte again in the buffer for ReadAll - while it has already been processed in that case.

public byte[] ReadAll()
{
ArraySegment<byte> byteSegment = this.innerStream.GetBuffer();
int count, totalBytes = 0, offset = (int)this.Position, length = (int)this.Length;
Copy link
Member

Choose a reason for hiding this comment

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

this.Length is not save when the inner stream is not buffered. And the API contract does not ensure that - it would thorw an exception if the inner stream is not buffered.

/// </returns>
public async Task<byte[]> ReadAllAsync(CancellationToken cancellationToken = default)
{
int count, totalBytes = 0, offset = (int)this.Position, length = (int)this.Length;
Copy link
Member

Choose a reason for hiding this comment

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

See comments in sync PAI. In general I think Stream implementations should implement the async variation and just await it in the sync overload. The perf overhead of doing it even if the asnyc overload completes synchronously is relatively small.

if (offset < 0
|| count < 0
|| (buffer.Length - offset) < count
|| this.innerStream.Position == this.innerStream.Length)
Copy link
Member

Choose a reason for hiding this comment

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

You can't do this for any Stream. If this.innerStream.CanSeek==false then Position or Lenght will throw NotSupportedException

@kundadebdatta
Copy link
Member Author

Follow Up PR created to track this work.

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.

3 participants