O'Reilly logo

Java I/O by Elliotte Rusty Harold

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required