In Java, data is stored in big-endian format (also called network order). That is, all data is represented sequentially starting from the most significant bit to the least significant. JDK versions prior to JDK 1.4 required definition of custom methods that manage reversing byte order to maintain compatibility with little-endian systems. Correct handling of byte order-related issues is critical when exchanging data in a networked environment that includes both big-endian and little-endian machines or when working with other languages using JNI. Failure to handle byte-ordering issues can cause unexpected program behavior.
...
This compliant solution uses methods provided by class ByteBuffer
(see [API 2006] ByteBuffer
) to correctly extract an int
from the original input value. It wraps the input byte array with a ByteBuffer
, sets the byte order to little-endian, and extracts the int
. The result is stored in the integer serialNumber
. Class ByteBuffer
provides analogous get and put methods for other numeric types.
...
Bibliography
[API 2006] | Class |
On Holy Wars and a Plea for Peace | |
Chapter 2, Primitive Data Types, Cross-Platform Issues |
...