Skip to content
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

Closing a chunked conversion causes an extra empty chunk to be emitted #52

Closed
abitofevrything opened this issue Sep 6, 2024 · 1 comment

Comments

@abitofevrything
Copy link

When the input stream to a chunked stream decompression is closed, an extra chunk with no data is emitted on the output stream.

As a simple example, an empty input stream should produce no output chunks (note that this is different from decompressing an empty list, which should yield an empty list):

await for (final chunk in Stream<List<int>>.empty().transform(zstd.decoder)) {
  print('Got chunk: $chunk'); // Prints "Got chunk: []" once
}

The root of the issue seems to be CodecFilter.processed, which always returns a non-null list if end is true, even if no input data is left to process:

// ...

final builder = BytesBuilder(copy: false);
if (_outputBuffer.unreadCount > 0) {
  final bufferedBytes = _outputBuffer.writtenBytes(reset: true);
  builder.add(bufferedBytes);
} else {
  if (_toProcess.isNotEmpty) _toProcess = _toProcess.drainTo(_inputBuffer);
  _codeOrDecode();
  final bufferedBytes = _outputBuffer.writtenBytes(reset: true);
  builder.add(bufferedBytes);
  if (flush == true) _flush(builder);
  if (end == true) _finalize(builder);
}
return builder.takeBytes();

This then causes CodecSink to emit the extra empty chunk in CodecSink.close.

This issue could be resolved by simply returning null if builder.isEmpty at the end of CodecFilter.processed. I'm not sure how correct this fix would be; I'll leave that to the experts on compression and whether empty chunks are possible.

sethloco added a commit that referenced this issue Sep 7, 2024
@sethloco
Copy link
Collaborator

sethloco commented Sep 7, 2024

Hello, thank you for reporting this and providing good information and analysis...very helpful.
I have published a new version to pub.dev.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants