简体   繁体   中英

Java: how to serialize object into byte array with as few output bytes as possible?

I tried code here ( Java Serializable Object to Byte Array ) and also SerializationUtils.serialize(), but when I serialize a object, I always get a lot more bytes then the number of bytes stored in the object.

For example, when I do SerializationUtils.serialize(new byte[1]), I can get a output byte array with length 28 while the input object has 1 byte. Similarly, if I have a class like

public class MyClass {
    public byte[] a;

    public MyClass(byte[] a) {
        this.a = a;
    }
}

And I create an object

MyClass obj = new MyClass(new byte[1]);

When I do SerializationUtils.serialize(obj), I will get a output byte array much longer than 1.

What I'm really doing in my program is that I'm sending array of objects between two parties, so I want to first serialize this array of objects into a single byte array and then send it. But SerializationUtils.serialize(MyClass[] obj) will output an array with a lot more bytes than the bytes obj really stores, which increases my bandwidth of communication.

I also tried creating the single byte array from the array of objects myself by doing the memory copying:

MyClass[] obj = new MyClass[5];
for (int i=0; i<5; i++)
    obj[i] = new MyClass(new byte[1]);

byte[] msg = new byte[5];
for (int i=0; i<obj.length; i++)
    System.arraycopy(obj[i].a, 0, msg, i, 1);

But this doesn't seem to be as efficient as SerializationUtils.serialize(MyClass[] obj) even through SerializationUtils.serialize(MyClass[] obj) will generate more bytes than the msg I have.

So is there some way in Java I can serialize object/array of objects efficiently and have the output byte array to be as short as possible?

Thanks in advance.

So is there some way in Java I can serialize object/array of objects efficiently and have the output byte array to be as short as possible?

Sure. What you need to do is write your own custom serialization code. For example, using a DataOutputStream you could serialize a byte[] like this:

   dos.writeInt(bytes.length);
   dos.write(bytes, 0, bytes.length);

and if the data was compressible then you could potentially do better.

For user-defined types, the serialization code will be more complex. But if you want "the output byte array to be as short as possible" ... then that is the price you have to pay !!


The reason that ObjectOututStream streams are large is that it is a requirement that serialization / deserialization is type-safe; ie that it can detect cases where representation types have changed between serializing and deserializing. That is done by including server UUIDs and type descriptors in the stream. That takes space.

Note you (the programmer) can improve on the size of a serialized object by other means:

  • by declaring unnecessary fields as transient ,
  • by writing a custom readObject / writeObject . or
  • by declaring the class to be Externalizable , and implementing space efficient externalization methods.

Please refer to the Java Object Serialization spec for the details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM