Skip to content

Streaming API - #64

Merged
oleg-st merged 7 commits into
masterfrom
streaming
Jan 2, 2026
Merged

oleg-st merged 7 commits into
masterfrom
streaming

Conversation

@oleg-st

@oleg-st oleg-st commented Dec 25, 2025

Copy link
Copy Markdown
Owner

Fix for #63

@deniszykov

Copy link
Copy Markdown

The basic idea behind these interfaces (gzip, brotli, zstd) is that you push the input and grab the output while it's available. That is, they should first check the output and ask for more until it's completely flushed.

  1. So the first check is: are we done? Yes -> Done
  2. Then, is there enough space for the output? No -> DestinationTooSmall
  3. Then, has all the input been processed? Yes -> NeedMoreInput
  4. Do encode/decode -> go to 1)

@deniszykov

Copy link
Copy Markdown

And check for destination is Span.Empty because pinning empty span gives NULL ptr. If it is empty -> DestinationTooSmall

@deniszykov

Copy link
Copy Markdown

I hope I'm not being too annoying with my comments :)

@oleg-st

oleg-st commented Dec 25, 2025

Copy link
Copy Markdown
Owner Author

The basic idea behind these interfaces (gzip, brotli, zstd) is that you push the input and grab the output while it's available. That is, they should first check the output and ask for more until it's completely flushed.

  1. So the first check is: are we done? Yes -> Done

  2. Then, is there enough space for the output? No -> DestinationTooSmall

  3. Then, has all the input been processed? Yes -> NeedMoreInput

  4. Do encode/decode -> go to 1)

I think current version covers this cases, 2 and 3 swapped, DestinationTooSmall if other options are excluded

@oleg-st

oleg-st commented Dec 25, 2025

Copy link
Copy Markdown
Owner Author

I hope I'm not being too annoying with my comments :)

No, it's ok :)

@oleg-st

oleg-st commented Dec 25, 2025

Copy link
Copy Markdown
Owner Author

And check for destination is Span.Empty because pinning empty span gives NULL ptr. If it is empty -> DestinationTooSmall

I'll add this checks later

@oleg-st

oleg-st commented Dec 25, 2025

Copy link
Copy Markdown
Owner Author

I'm not sure if 1-3 can be false, so we need cycle here

@deniszykov

deniszykov commented Dec 25, 2025 •

Copy link
Copy Markdown

I'm not sure if 1-3 can be false, so we need cycle here

Yes, first compression/decompression is done, then everything is checked according to the list, and if the output is not full, and the input is not fully used, compression/decompression is done again.

BrotliDecoder is done via while ((int)availableOutput > 0)

@oleg-st

oleg-st commented Dec 25, 2025 •

Copy link
Copy Markdown
Owner Author

I'm not sure if 1-3 can be false, so we need cycle here

Yes, first compression/decompression is done, then everything is checked according to the list, and if the output is not full, and the input is not fully used, compression/decompression is done again.

BrotliDecoder is done via while ((int)availableOutput > 0)

But why can't compression/decompression continue on its own if there is enough data in the input and space in the output?
I thought compression/decompression could only end for three reasons: done/not enough space in the output/input finished (prematurely).
Why do we need to pass the same data a second time?

@deniszykov

deniszykov commented Dec 25, 2025 •

Copy link
Copy Markdown

gzip and brotli, for example, allow you to concatenate files together; when one of these blocks ends, decompress() exits without finishing whole input and using up whole output. And caller should call decompress() again.
This is just one of the cases that I know of, there may be more.

If zstd has the same feature, it might be worth writing a test.

For compression, I don’t know why they did BrotliEncoder through a while ((int)availableOutput > 0), maybe it’s some kind of quirk of library.

@oleg-st

oleg-st commented Dec 25, 2025

Copy link
Copy Markdown
Owner Author

If zstd has the same feature

Yes, it has, so it's real case.

@oleg-st

oleg-st commented Dec 26, 2025

Copy link
Copy Markdown
Owner Author

Checked two concatenated streams.
It works for Zstd (current version without a cycle): both streams are decompressed.
It does not work for Brotli (with a cycle): only the first stream is decompressed, then it returns Done.

@deniszykov

deniszykov commented Dec 26, 2025 •

Copy link
Copy Markdown

GzipStream's Inflater handles concatenated streams explicitly. GzipStream behavior is to continue decompression. Zlib's is to stop at the end of the block.
https://github.com/dotnet/runtime/blob/6aecb65a7eff3e91dd6b8856401854e03c30a839/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateZLib/Inflater.cs#L138

So it's up to you.

@deniszykov

deniszykov commented Dec 26, 2025 •

Copy link
Copy Markdown

I think if current passing tests, there's nothing more to do. As soon as you release the next version, I'll check it with my tests.

…ll; otherwise, continue compression/decompression
@oleg-st

oleg-st commented Dec 26, 2025

Copy link
Copy Markdown
Owner Author

In the case of two concatenated streams, DestinationTooSmall was returned even if there was still space available. Added a loop to fix this.

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

Successfully merging this pull request may close these issues.

2 participants