-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve download bytes performance while tracking progress
- Loading branch information
Showing
4 changed files
with
49 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'dart:typed_data'; | ||
|
||
extension Uint8ListExt on Uint8List { | ||
Stream<Uint8List> toStream({required int chunkSize}) async* { | ||
final byteArray = this; | ||
|
||
int offset = 0; | ||
while (offset < byteArray.length) { | ||
int end = offset + chunkSize; | ||
if (end > byteArray.length) end = byteArray.length; | ||
|
||
// Create a view of the data without copying | ||
var chunk = Uint8List.sublistView(byteArray, offset, end); | ||
yield chunk; | ||
|
||
offset = end; | ||
} | ||
} | ||
} | ||
|
||
extension ByteStreamExt on Stream<List<int>> { | ||
Future<Uint8List> toUint8List() async { | ||
// Using BytesBuilder to efficiently concatenate all bytes | ||
final bytesBuilder = BytesBuilder(copy: false); | ||
await for (final chunk in this) { | ||
bytesBuilder.add(chunk); | ||
} | ||
|
||
return bytesBuilder.takeBytes(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters