This discussion is archived
9 Replies Latest reply: Jul 21, 2011 7:28 PM by 866753 RSS

what is a lightweight way to convert double or integers to byte arrays

866753 Newbie
Currently Being Moderated
Hello,

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.

It's also desired to convert a byte array to corresponding typed data, and I do something as follows:
for getting a long integer, I do Long.reverseBytes(in.readLong()),
and for gettting a double float point, I do Double.longBitsToDouble(Long.reverseBytes(in.readLong()));
For my case, I need to reverse the byte order, but it is irrelevant to my question.

These operations requires some ALU instructions. They might be nothing for powerful servers, but for Atom processors, it's still quite heavy when working on several hundred GB data.

I hope I can avoid all extra instructions if possible. I'm thinking whether Java VM provides some primitive methods to allow us to convert data types directly. If so, a simple memory copy will be enough.

Thanks,
Da

Edited by: 863750 on Jul 19, 2011 11:14 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points