@@ -518,8 +518,7 @@ class _MediaUploadStreamSink implements StreamSink<List<int>> {
518
518
final storage_api.Object _object;
519
519
final String ? _predefinedAcl;
520
520
final int ? _length;
521
- int _bufferLength = 0 ;
522
- final List <List <int >> buffer = < List <int >> [];
521
+ final BytesBuilder _buffer = BytesBuilder ();
523
522
final _controller = StreamController <List <int >>(sync : true );
524
523
late StreamSubscription _subscription;
525
524
late StreamController <List <int >> _resumableController;
@@ -577,15 +576,23 @@ class _MediaUploadStreamSink implements StreamSink<List<int>> {
577
576
void _onData (List <int > data) {
578
577
assert (_state != _stateLengthKnown);
579
578
if (_state == _stateProbingLength) {
580
- buffer.add (data);
581
- _bufferLength += data.length;
582
- if (_bufferLength > _maxNormalUploadLength) {
579
+ _buffer.add (data);
580
+ if (_buffer.length > _maxNormalUploadLength) {
583
581
// Start resumable upload.
584
582
// TODO: Avoid using another stream-controller.
585
583
_resumableController = StreamController <List <int >>(sync : true );
586
- buffer. forEach ( _resumableController.add);
584
+ _resumableController.add (_buffer. takeBytes () );
587
585
_startResumableUpload (_resumableController.stream, _length);
588
586
_state = _stateDecidedResumable;
587
+
588
+ // At this point, we're forwarding events to the synchronous controller,
589
+ // so let's also forward pause and resume requests.
590
+ _resumableController
591
+ ..onPause = _subscription.pause
592
+ ..onResume = _subscription.resume;
593
+ // We don't have to handle `onCancel`: The upload will only cancel the
594
+ // stream in case of errors, which we already handle by closing the
595
+ // subscription.
589
596
}
590
597
} else {
591
598
assert (_state == _stateDecidedResumable);
@@ -597,7 +604,7 @@ class _MediaUploadStreamSink implements StreamSink<List<int>> {
597
604
if (_state == _stateProbingLength) {
598
605
// As the data is already cached don't bother to wait on somebody
599
606
// listening on the stream before adding the data.
600
- _startNormalUpload (Stream < List < int >>. fromIterable (buffer), _bufferLength );
607
+ _startNormalUpload (Stream . value (_buffer. takeBytes ()), _buffer.length );
601
608
} else {
602
609
_resumableController.close ();
603
610
}
0 commit comments