In Java, data is stored in big-endian format (also called network order), that . That is, all data is represented sequentially starting from the most significant bit (MSB) to the least significant. Prior to JDK 1.4, custom methods had to be defined to be compatible with little-endian systems that use the reverse byte order. Handling byte order related issues is critical when data is to be exchanged in a networked environment that consists of machines varying in endianness. Failure to handle such cases can cause misinterpretations and unexpected program behavior.
...
Assuming that an integer value is to be read from the file, read and write methods can be defined for handling little-endian data. The readLittleEndianInteger
method reads data into a byte buffer and then pieces together the integer in the right order. Long
values can be handled by defining a byte buffer of size 8
eight. The writeLittleEndianInteger
method obtains bytes by repeatedly casting the integer so that the most significant byte is extracted on successive right shifts.
...
Compliant Solution
In JDK 1.5+, the Integer.reverseBytes()
method methods defined in the classes Character
, Short
, Integer
and Long
can be used to reverse the order of the bytes constituting the integer. Note that there is no such method for float
and double
values.
...