0

I keep getting this recursive error when using my plugin. Because it is an addon to something written in mono I can't see the full stack trace, just the text version. I'm trying to serialize a GZipStream from a MemoryStream to a object and back. My code:

    public byte[] Bytes
    {
        set
        {
            using (var ms = new MemoryStream())
            {
                ms.Write(value, 0, value.Length);
                ms.Seek(0, SeekOrigin.Begin);
                using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
                {
                    Value = (T)new BinaryFormatter().Deserialize(gzip);
                    gzip.Close();
                }
                ms.Close();
            }
            valChanged = true;
        }
        get
        {
            if (!valChanged) return bytes;
            if (Value == null) return null;
            if (!Value.GetType().IsSerializable) return null;
            using (var ms = new MemoryStream())
            using (var gzip = new GZipStream(ms, CompressionMode.Compress))
            {
                new BinaryFormatter().Serialize(gzip, Value);
                bytes = ms.ToArray();
            }
            valChanged = false;
            return bytes;
        }
    }

The exception I get when executing this and serializing is System.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed. I've tried ms.Position = 0; and ms.Seek(0, SeekOrigin.Begin); but they both have the same output. I've looked at a bunch of other Stackoverflow questions about this but they don't seem to help.

Bwookzy
  • 3
  • 2

0 Answers0