Reputation: 1065
I wrote a small test program to show how many bytes we need to serialize Integer
object:
ByteArrayOutputStream data = new ByteArrayOutputStream();
try {
ObjectOutputStream output = new ObjectOutputStream(data);
output.writeObject(1);
output.flush();
System.out.println(data.toByteArray().length);
} catch (IOException e) {
e.printStackTrace();
}
However, the result is surprising, it takes 81 bytes. If I serialize String
"1", it only takes 8 bytes instead. I know java has optimization for String serialization, but why not do the same thing for Integer? I think it shouldn't be very difficult.
Or does anyone has some workaround? I need a method which can serialize everything include objects and basic types. Thanks for your answers!
Upvotes: 2
Views: 696
Reputation: 1504122
It's a balancing act, between making the serialization protocol more complicated by having direct support for lots of types, and between keeping it simple.
In my experience, Integer
values are relatively rare compared with int
values - and the latter does have built-in support, along with all the other primitive types. It's also worth noting that although serializing a single Integer
object is expensive, the incremental cost is much smaller, because there's already a reference to the class in the stream. So after the first Integer
has been written, a new Integer
only takes 10 bytes - and a reference to an Integer
which has already been written to the stream (common if you're boxing small values) is only 5 bytes.
Personally I would try to avoid native Java binary serialization anyway - it's platform specific and very brittle, as well as not being terribly compact. I like Protocol Buffers but there are lots of other alternatives available too.
Upvotes: 4