Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Avoid copying with BytesBuilder #186

Merged
merged 2 commits into from
Jul 1, 2021
Merged

Avoid copying with BytesBuilder #186

merged 2 commits into from
Jul 1, 2021

Conversation

natebosch
Copy link
Contributor

@natebosch natebosch commented Jun 30, 2021

The previous implementation of collectBytes would return a Uint8List
where the lengthInBytes and buffer.lengthInBytes matched. This
property isn't an intended guarantee, however since some callers may
rely on it for correct behavior it is safer to retain it. Use copy: false
so that the buffer used in takeBytes has the same length as the data.

The previous implementation of `collectBytes` would return a `Uint8List`
where the `lengthInBytes` and `buffer.lengthInBytes` matched. This
property isn't an intended guarantee, however since some callers may
rely on it for correct behavior it is safer to retain it. Use `toBytes`
to create a copy with the correct length.
@@ -46,7 +46,7 @@ T _collectBytes<T>(Stream<List<int>> source,
var completer = Completer<Uint8List>.sync();
var subscription =
source.listen(bytes.add, onError: completer.completeError, onDone: () {
completer.complete(bytes.takeBytes());
completer.complete(bytes.toBytes());
Copy link
Contributor

Choose a reason for hiding this comment

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

Try using a BytesBuilder(copy: false) instead, then use takeBytes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using copy: false seems to fix it. I'll use that. Thanks!

Copy link
Contributor

@lrhn lrhn left a comment

Choose a reason for hiding this comment

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

Try the non-copying approach. If that also fails, using toBytes LGTM.

@natebosch natebosch changed the title Use BytesBuffer.toBytes over takeBytes Avoid copying with BytesBuilder Jul 1, 2021
@natebosch natebosch merged commit 8193eac into master Jul 1, 2021
@natebosch natebosch deleted the to-bytes branch July 1, 2021 21:13
mosuem pushed a commit to dart-lang/core that referenced this pull request Oct 14, 2024
The previous implementation of `collectBytes` would return a `Uint8List`
where the `lengthInBytes` and `buffer.lengthInBytes` matched. This
property isn't an intended guarantee, however since some callers may
rely on it for correct behavior it is safer to retain it. Use `copy: false`
so that the buffer used in `takeBytes` has the same length as the data.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging this pull request may close these issues.

2 participants