views:

52

answers:

1

I want to use Preon for project that communicates with a server written in C. The protocol depends on the native endianess of the machine (you can solve with thisjava.nio.ByteOrder.getNative() under the assumption that the JVM has the same endianess as the server) and uses uint64_t for data lenghts and int32_t for status codes (a negative value indicates an error).

I couldn't find information about signedness in the Preon documentation. I had a quick look at the source code and found that nl.flotsam.preon.buffer.DefaultBitBuffer uses signed shifts (<< and >>) and the javadoc comments of several methods in nl.flotsam.preon.buffer.BitBuffer also indicate that it uses signed integers, but the javadoc comment nl.flotsam.preon.buffer.BitBuffer says that BitBuffer uses unsigned integers. This confused me.

What is the default integer format of Preon? How do I represent uint64_t and int32_t?

+1  A: 

For data marked with @BoundNumber, the default is LittleEndian. You can override that like this:

@BoundNumber(byteOrder=BigEndian)

… but platform specific currently is not an option. That is a limitation. You might want to consider filing an issue for it. (Otherwise, I will, somewhere in the future.)

The way I could see it work is to just have another enum constant, called Platform.

Update:

For byte order, Preon now (currently only available in the head) has three options: BigEndian, LittleEndian and Native. Native will resolve in the byte order indicated by java.nio.ByteOrder.getNative().

As a consequence, it is now possible to write code like this:

@BoundNumber(byteOrder=Native) int value;

… which translates into a signed 32 bit integer, with byte order based on the architecture.

Wilfred Springer
See http://jira.codehaus.org/browse/PREON-15
Wilfred Springer
O, and because of some issues with Codehaus, I am currently pushing to Github, rather than to the Codehaus repository. Sorry for the inconvenience. Hope that will be resolved soon.
Wilfred Springer
Thanks. As far as I understood it now that default format for integers in Preon is signed. `@BoundNumber(size = "32") long value` could represent a `uint32_t` then. But what about `uint64_t`? Can it be represented in `BigInteger`?
ott
That would be possible, however it would require registering another CodecFactory that would (in its create(...) method) be triggered by the combination of a BigInteger type and the presence of a BigNumber annotation.
Wilfred Springer