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

Adding non compressed data at the end of the stream #95

Closed
julienlesur opened this issue Jan 24, 2024 · 2 comments
Closed

Adding non compressed data at the end of the stream #95

julienlesur opened this issue Jan 24, 2024 · 2 comments
Labels
question Further information is requested

Comments

@julienlesur
Copy link

Hi,
First, I'd like to congrat you and thank you for this great library.

In a current project, I need to create a stream with non compressed data at the beginning, and the end but compressed data in the middle (several blocks if possible).
I easily could write non compressed data followed by compressed data and read it. But I can't find any solution to add non compressed data at the end.

Here is one of the many failing ways I tried, could you give me a hint?

`
//First stream
using var streamHeader = WriteMindVersionSection();
//Second stream
using var streamModelInformations = WriteModelInformations(globalModel, modelIndex, instanceIndex, isInterlinkExtraction, out var gridIndicesToSave);
streamModelInformations.Seek(0, SeekOrigin.Begin);

        using var compressor = LZ4Stream.Encode(streamHeader);
        //Compressing the stream and adding it to streamHeader
        streamModelInformations.CopyTo(compressor);

        //Then one the many ways I try to add non compressed at the end, Fails like the rest
        using var streamPositionSection = new MemoryTributary();
        using var bwPosition = new BinaryWriter(streamHeader);
        bwPosition.Write((short)14);
        bwPosition.Write(21l);
        bwPosition.Write(100000l);

        return streamHeader;`

I can after read values that were at the beginning of streamHeader and the compressed values in streamModelInformations but if I try to go directly before the short and the two long at the end of the stream, the values I obtain are different than the one I initially wrote.
Thanks for advance if you can help me (and even if you can't ;) )

@MiloszKrajewski MiloszKrajewski added the question Further information is requested label Jan 24, 2024
@julienlesur
Copy link
Author

The problem was finally resolved, in each method that are building a part stream, I "force" the compressor to be disposed at the end of the method instead of letting the garbage collector do it by its own.

Sorry for the stupid question and thanks once again for your great library

@MiloszKrajewski
Copy link
Owner

MiloszKrajewski commented Jan 26, 2024

Not sure if what you did is right.

What you need, is to Dispose LZ4 stream, but not underlying Stream. You can use leaveOpen argument for that:

Stream stream, LZ4EncoderSettings? settings = null, bool leaveOpen = false)

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

No branches or pull requests

2 participants