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.