Converting a class to byte array without any values already contains 220 bytes of my 256 available bytes

StackOverflow https://stackoverflow.com/questions/12932485

سؤال

I'm currently working on saving the class UsageData to a byte array and storing it on a hardware key. This hardware key has reserved a maximum 256 number of bytes for user data. Currently my filled class with properties has around a size of 640 bytes, already compressed. Now I went over to create a temporarily class UsageDataTemp without any properties/fiels. I run a test to see how large the byte array is without any values, and it already has a size of 220 bytes, only leaving me with 36 more bytes to fill.

    [Serializable]
    public class UsageDataTemp
    {
        public byte[] ToByteArray()
        {
            var formatter = new BinaryFormatter();
            using (var stream = new MemoryStream())
            {
                //original serialization is 684 bytes long, compress it with the gzipstream
                using (var compressionStream = new GZipStream(stream, CompressionMode.Compress))
                {
                    formatter.Serialize(compressionStream, this);
                    compressionStream.Flush();
                    return stream.ToArray();
                }
            }
        }
    }

Why is this the case that an empty class without any values already needs 220 bytes for saving itself. And is there a way to compress the byte array down further. Or do I need to start making my own BinaryFormatter.

هل كانت مفيدة؟

المحلول

You don't tell the serializer/deserializer what type of objects they're dealing with - you can pass them anything and they "should" work.

That implies that some type information must be stored in the binary stream for it to work out what type of objects are being deserialized. That could easily (I think) account for 220 bytes of overhead.


If every byte counts, then yes, you ought to be writing your own code to transform the data that you're interested in down to a minimal representation, rather than using a generic mechanism such as the binary serializer.

نصائح أخرى

Why is this the case that an empty class without any values already needs 220 bytes for saving itself?

Why ask what you can find out? This code already yields 33 bytes:

FileStream fs = new FileStream("Serialized.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, "FooString");

Because there's more being serialized than just the string itself. If you serialize an object, its class definition (and the assembly it's in) is also being serialized. Serializing an instance of this class:

[Serializable]
class Foo
{
    public String FooString { get; set; }
}

Costs 166 bytes, while the FooString property contains the same data as the raw string serialized earlier.

So: write your own serialization (or: load/save) logic. You are the one that reads and writes that data, so you can for example assign specific bytes in the space you have to certain properties of your class.

You don't need to serialize your structure. All you need is to store the bytes in the hardware key. You can use the Object Factory pattern to implement something that, given a 256 bytes array builds a UsageData object, and something to obtain your 256 bytes array from an UsageData object.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top