13.07.2015 Views

I/O Fundamentals

I/O Fundamentals

I/O Fundamentals

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Java Input and OutputJava is Big-EndianFactoid: Where did the terms "Big-Endian" and "Little-Endian"originate?Jonathan Swift used these terms when writing about a "holy war" in "Gulliver'sTravels" (1726). The Lilliputian ruler declared that all Lilliputians must eat theireggs starting at the "Little End" (because he clumsily cut himself whileattempting to eat the egg from the big end). Many Lilliputians rebelled, and tookrefuge in nearby Blefuscu, where all were required to eat their eggs starting at the"Big End". The "Little-Endians" and "Big-Endians" went to war over the entiresilly issue.Danny Cohen wrote an essay on byte-ordering in 1980, "On Holy Wars and aPlea for Peace". In that essay, he likens the entire issue to the egg war in"Gulliver's Travels", giving us the terms "Big-Endian" and "Little-Endian" forbyte ordering. You can read his essay at http://www.op.net/docs/RFCs/ien-137The problem Endianness causes is that a translation must be performed wheneversomeone writes a binary file on a Big-Endian platform, and tries to read it on aLittle-Endian platform. Currently, most types of platforms, such as the variousUNIX machines, are Big-Endian. Some, like the PC, are Little-Endian.The Java Virtual Machine is a platform, and as such, it needed to choose how itwould interpret data.The choice seemed rather obvious, as Sun, a UNIX machine producer, created theplatform.Java is a Big-Endian platform.This means if you run a Java program under Solaris, and write several ints to thefile (using DataOutputStream, for example), native Solaris programs can read theints from the file. If you run the same Java program on a PC, native PCprograms cannot directly use the data!Dealing With Java I/O on Little-Endian PlatformsThere are several ways to deal with this issue:• Only have Java programs process the data in question. If the binary data is notneeded by any external native programs, this is the simplest solution.For example, if you are writing a binary configuration file that will be read© 1996-2003 jGuru.com. All Rights Reserved. Java Input and Output -55

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!