Serialization in Java is an old topic, but here I'm asking a very lightweight serialization, hopefully as lightweight as memory copy. In C/C++, if I want to convert a double or integer into a byte array, I can simply convert the type and do memory copy (or even memory copy can be saved). I wonder if Java provides some method to do the similar thing.
Right now the best I can get is use DataOutputStream.writeDouble() or .writeInt() to convert doubles or integers to byte array. To convert a byte array to corresponding typed data, I do something as follows:
ra = Double.longBitsToDouble(Long.reverseBytes(in.readLong()));
dec = Double.longBitsToDouble(Long.reverseBytes(in.readLong()));
objID = Long.reverseBytes(in.readLong());
x = Double.longBitsToDouble(Long.reverseBytes(in.readLong()));
y = Double.longBitsToDouble(Long.reverseBytes(in.readLong()));
z = Double.longBitsToDouble(Long.reverseBytes(in.readLong()));
These operations might be nothing for powerful servers. But for Atom processors, it's still quite heavy when working on several hundred GB data.
So any ideas how to convert between typed data and byte arrays more lightweightly?
Try something like thisFor a double, try the Double#doubleToLongBits(double) method. Use the Long or Double class values instead of Integer. You can probably use the >> operator instead of >>>. You will have to check whether I have got the names of the constant fields correct.
Line 5 needs a cast to byte. I'm also not too sure if that works; is the "& Byte.MIN_VALUE" really needed? Byte.MIN_VALUE is -128 or 1000 000. In other words, you're only taking the most significant byte. Here's how I've done this in the past:
I think the "& 0xFF" is unnecessary, as 0xFF is the same as 1111 1111 if you only take the last 8 bits. The "& 0xFF" cuts off anything but the last 8 bits, but so does the cast to byte.
Guys, what you said is how DataOutputStream.writeDouble() and .writeLong() are implemented. Please check the code.
I don't know the performance difference between >>> and >>. Maybe it's worth having a closer look.
I'm thinking, however, whether Java VM provides some primitive methods to allow us to convert data types directly. Think about it, a double float-point and 8-byte array occupy the same size of memory. A simple memory copy will be enough if JVM has a primitive to do so.