Exception: 'Unexpected end of stream. Data might be corrupted.'

80 Views Asked by At
static MemoryStream CompressData(MyMessage data)
    {
        // Serialize to a MemoryStream
        var serializedStream = new MemoryStream();
        Serializer.Serialize(serializedStream, data);
        serializedStream.Position = 0;

        // Compress the serialized data
        var compressedStream = new MemoryStream();
        var lz4Stream = LZ4Stream.Encode(compressedStream, LZ4Level.L00_FAST);
        {
            serializedStream.CopyTo(lz4Stream);
        }
        compressedStream.Position = 0;
        lz4Stream.Flush();
        // Dispose the serializedStream
        serializedStream.Dispose();
        
        return compressedStream; 
    }

    static MemoryStream DecompressStream(Stream input)
    {
        // Decompress the data
        var decompressedStream = new MemoryStream();
        using (var lz4Stream = LZ4Stream.Decode(input))
        {
         // System.IO.EndOfStreamException: 'Unexpected end of stream. Data might be corrupted.'

            lz4Stream.CopyTo(decompressedStream); 
        }
        decompressedStream.Position = 0;

        return decompressedStream;
    }

What am I doing wrong here? I tried disposing and changing the stream position and still it wouldn't work. Any help would be highly appreciated as I'm struggling with this for a whole day.

0

There are 0 best solutions below