dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.23k stars 5.87k forks source link

Stream.Read mistake #42496

Open bravequickcleverfibreyarn opened 2 weeks ago

bravequickcleverfibreyarn commented 2 weeks ago

Type of issue

Other (describe below)

Description

From https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-7#net-libraries

Stream.Read may return less data than what's available in the stream.

In fact, Read can return less bytes than buffer size is. Meaning all stream bytes are read as long as buffer is capacitated for it.

See https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.read?view=net-8.0#system-io-stream-read(system-span((system-byte))).

The total number of bytes read into the buffer. This can be less than the size of the buffer if that many bytes are not currently available, or zero (0) if the buffer's length is zero or the end of the stream has been reached.

Page URL

https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-7

Content source URL

https://github.com/dotnet/docs/blob/main/docs/core/whats-new/dotnet-7.md

Document Version Independent Id

0d5a2ef0-b8a5-181f-db39-5a0ad4c182ff

Article author

@gewarren

Metadata

adegeo commented 1 week ago

I'm unsure what you're implying here. Are you saying this "what's new in .NET" post is wrong? According to the linked blog post about what's new, Stream can return less than the buffer yet still contain more data.

bravequickcleverfibreyarn commented 1 week ago

Yes, you understood. I suggest that both linked post and news article are wrong. As far as I remember Stream.Read always returned all data up to buffer size. How it would be even possible to read it if it randomly returned not-all bytes?

I also briefly checked with code. I saw nothing what should convey behavior mentioned.

Check also:

Simply, I take text from https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.read?view=net-8.0#system-io-stream-read(system-span((system-byte))) as gauge.

I guess somebody can be consulted.