Reading and Writing Little-Endian Numbers
It’s likely that at some point in time you’ll need to
read a file full of little-endian data, especially if you’re
working on Intel hardware or with data written by native code on such
a platform. Java has essentially no support
for little-endian numbers. The
LittleEndianOutputStream
class in Example 7.8 and
the
LittleEndianInputStream
class in Example 7.9
provide the support you need to do this. These classes are closely
modeled on the java.io.DataInputStream and
java.io.DataOutputStream classes. Some of the
methods in these classes do exactly the same thing as the same
methods in the DataInputStream and
DataOutputStream classes. After all, a big-endian
byte is no different from a little-endian byte. In fact, these two
classes come very close to implementing the
java.io.DataInput and
java.io.DataOutput interfaces. Actually doing so
would have been a bad idea, however, because client programmers will
expect objects implementing DataInput and
DataOutput to use big-endian numbers, and
it’s best not to go against such common assumptions.
I also considered making the little-endian classes subclasses of
DataInputStream and
DataOutputStream. While this would have eliminated
some duplicated methods like readBoolean() and
writeBoolean(), it would also have required the
new, little-endian methods to have unwieldy names like
readLittleEndianInt() and
writeLittleEndianInt(). Furthermore, it’s unlikely you’ll need to read or write both little-endian ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access