aspose file tools*
The moose likes Java in General and the fly likes what is a lightweight way to convert double or integers to byte arrays in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "what is a lightweight way to convert double or integers to byte arrays in Java" Watch "what is a lightweight way to convert double or integers to byte arrays in Java" New topic
Author

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

zheng da
Greenhorn

Joined: Dec 09, 2004
Posts: 4
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. 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?

Thanks,
Da
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40053
    
  28
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.

Why can't you use the ByteArrayOutputStream class to convert the entire object to a byte[]?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

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.

And you've also declared two variables "i"


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
zheng da
Greenhorn

Joined: Dec 09, 2004
Posts: 4
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Except longs are usually stored on the stack, and byte arrays on the heap - in different memory regions.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40053
    
  28
You're right Rob; it should have been (byte)-1.

Sorry for my mistake.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40053
    
  28
... or, as stated previously, 0xff would have been better.
zheng da
Greenhorn

Joined: Dec 09, 2004
Posts: 4
Rob Spoor wrote:Except longs are usually stored on the stack, and byte arrays on the heap - in different memory regions.

I know. So one memory copy is inevitable, but I hope we can avoid all instructions for converting types.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is a lightweight way to convert double or integers to byte arrays in Java