-
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
virtual Task CopyToAsync(byte[] destination, CancellationToken cancellationToken) (and its sync version) for abstract class Stream #42109
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Why do you think your method is more useful than |
Thanks. |
My bad, you always want to have it started at offset 0, I missed that. |
var ms = new MemoryStream();
var copiedByteCount = await sourceStream.CopyToByteArrayAsync(ms, CancellationToken.None);
var buffer = ms.ToArray(); You can even get the internal array using |
@davidfowl thanks for comment. If need to implement that API, can we use similar way as implementation of 'Stream.CopyToAsync(Stream)'? (by calling ReadAsync in loop and manage current buffer offset and so on) I think that is efficient way for this requirement ? I can try to contribute these implementation if approved API proposal :) |
Your proposed behavior has no difference with |
Thanks.
I have not used ReadAsync(Memory) but I feel All ReadAsync methods have following common behavior based on doc:
What I mentioned is behavior about Fully Read, not only "currently available", which I think it is CopyTo , rather than ReadAsync Please check source code of existing Stream.CopyToAsync(Stream) and that is my thought about Fully copy. |
There's no "fully" concept of a stream. You cannot know the end and length of a network stream until the remote endpoint says to disconnect. |
We can know stream is to end by checking stream.readasync =0 |
A similar proposal, looking at this from a different point of view: |
Thanks for the suggestion. Given all the feedback, I'm going to close this. I do want to see some variation of #16598 implemented, hopefully in .NET 7. |
Background and Motivation
Sometimes we would like to have a stream CopyTo method which can try to fully copies stream data to a byte array, not only to another stream.
If stream is to end, copy returns with actual copied bytes count;
if byte array is written to its length - 1, return byte array length.
Proposed API
Usage Examples
Risks
As previous Stream.CopyTo (Stream) function, it is also a virtual one in abstract class Stream, so that just implement in abstract Stream and no breaking change for user.
The text was updated successfully, but these errors were encountered: