-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Proposal: Stream.HasLength property #43448
Comments
Tagging subscribers to this area: @dotnet/ncl |
Instead of |
Thank you for clarifying, I'll go open a PR in the docs repo then! 😄 Do you reckon a separate |
It's certainly fine to have a discussion about, and this issue can be used for that. We do want to be very careful about any new virtuals we add to Stream and that they really add enough value to be worth their weight, and need to think through what the default implementations would be. For example, Stream.HasLength's base implementation would probably need to be |
Right, yeah that'd be inconvenient 🤔 |
It could also do a |
Related: If you run this on Linux today:
The output is:
Would this API account for this? |
@jkotas the main description is accounting for the case you shared:
Not only |
Right, it means that there is no good way to provide base implementation that works well. How are people calling this API going to figure out whether they are calling base implementation that may return bogus result; or actual implementation that will be correct? |
FWIW, I do not think that the proposal in its current form is ready for review. |
I agree. As it stands, we shouldn't add this. |
i hit this today. so a http stream that is not seekable and length throws. went to work around it by copying it to a memory stream. and then CopyToAsync throws since internally it tries to use Length to calculate the buffer size |
I agree. Since we have not found a way to do that for a few years I am going to close the issue. |
Description
There currently is no reliable way to know whether
Stream.Length
can be used, as checkingCanSeek
only guarantees thatLength
will work if it returnstrue
, but gives no guarantees if it returnsfalse
: there can be cases whereCanSeek
isfalse
butLength
works just fine. This means that the only way to check theLength
is to use atry/catch
block, which is not ideal. Being able to access the length allows for a number of optimizations, such as renting a buffer the size of the whole stream, to read its contents in a single pass without wasting cycles expanding the buffer to write to, and making more copies and reads.Proposed API:
Original description
The
Stream
returned byHttpContent.ReadAsStreamAsync
sometimes hasCanSeek
set tofalse
, but accessing theLength
property works just fine. This seems to directly contradict the docs (here):This remark implies that
!CanSeek
==>Length
throws, but this is not always true in practice. To work around this, I ended up using an uglytry/catch
block in theSystem.Text.Json
-based JSON serializer in therefit
library: here. As mentioned above, not really sure how to classify this issue between a bug report and a feature proposal, as it depends on whether or not this is intended behavior. As in, I would say there are two possibilities here:!CanSeek
==>Length
throws, then it seems like there is a bug inHttpContent
.Stream.HasLength
property (this was actually @tannergooding's idea), that will allow developers to just check whetherLength
can be accessed, regardless of whether or not the whole stream is actually seekable. This is particularly useful whenever you'd like to just allocate a buffer to read the whole stream in memory in one go, without having to use a buffer writer which will likely result in less efficient code due to buffer expansions and copy operations.Related issues/PRs
Configuration
The text was updated successfully, but these errors were encountered: