• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[]?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
zheng da
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except longs are usually stored on the stack, and byte arrays on the heap - in different memory regions.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right Rob; it should have been (byte)-1.

Sorry for my mistake.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... or, as stated previously, 0xff would have been better.
 
zheng da
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic