Here's the situation. I'm tasked with "porting" and composing device communication drivers in JAVA. By communication drivers, I mean I'm going to need to both parse raw byte streamed messages coming from various IEDs (Intelligent Electronic Devices, not explosives!), and format similar byte streams to be transmitted back. At the very least I'm going to have to convert arrays of bytes (or portiona of such arrays) to JAVA primitives (such as shorts, ints, longs, and IEEE floats), and reverse the same process for transmitting data. Note that due to the many and varied IEDs I'll be dealing with, I can not even count on multibyte types being in big or small endian format.

Well its no big deal for me to swap around byte order in Java, and really its not that big a deal to convert byte streams to integral types such as shorts or ints, using various shift operators (and being careful about sign extension). But 32 and 64 bit real numbers (floats and doubles) are another matter. I suppose I could write routines to parse bit fields to interpret mantissa, exponent, sign bits, etc, and "rebuild" floating point numbers from scratch. I suppose I could also do the reverse for converting floats to bytes too. But my gut feeling is that doing so will result in horribly CPU hogging code. Clearly, some simple way to serialize and de-serialize primitives is the best approach. I've seen an article about "deep copying" of Java objects using such serialization, but it seemed geared toward working with entire Java objects at both ends of the conversion, so I don't think that will help. Obviously a C style memcpy() would be best, but I don't see any "legal" way to do such things in JAVA.

Any ideas?