Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Language
Expertise: Beginner
May 27, 1997

Byte order

How does one find out the byte order of the platform on which JVM is running? For example, for a Java application that has to read a C structure from a file, the structure would be stored in the local byte order and Java is big endian.

First a little background for new guys.

A memory module M can be viewed as an array of cells. Each cell has a unique address and holds one byte. Let M[i] denote the byte stored in the cell at address i.

Consider the following declaration:

short x = 1;
Since x occupies two bytes,
 x = 00000000 00000001  // binary representation of 1
we will have to use two consecutive cells, say M[2000] and M[2001], to store x. But which byte goes in which cell?

Great battles have been fought over this seemingly trivial detail, recalling the battle between the Lilliputians and their neighbors over which was the proper end to break on a soft boiled egg: the little end or the big end.

The Big Endians (Motorola, SPARC, Java VM) insist that the big (most significant) end ought to come first:

M[2000] = 00000000
   M[2001] = 00000001
The Little Endians (Intel) insist that the little end ought to come first:
M[2000] = 00000001
   M[2001] = 00000000
(MIPS processors can be configured either way!)

Actually, the choice is not as trivial as it first seems. Writing a number to a binary file preserves the byte order, so a Big Endian machine reading a file of numbers written by a Little Endian machine must perform complicated conversions, and vice versa.

The Java VM always writes data to files in Big Endian order, and always assumes the data files it reads are Big Endian.

So how do we tell if the host platform is Big or Little Endian? (Unfortunately, byte order isn't normally in System.getProperties().) I'm going to go out on a limb and say that there isn't a way to do this from within a Java program. If there was, it would break Java's platform independence. I tried doing a quasi-legal cast, the standard technique used in C, but Java was too smart for me:

class Parent {}
   class Child1 extends Parent{ short x = 1; }
   class Child2 extends Parent{ byte a, b; }
   Child1 c1;
   Parent p = c1;       // generalize
   Child2 c2 (Child2)p; // specialize
Notice that c1 and c2 both occupy two bytes. My plan was to peek at the first byte of c1.x:
boolean bigEndian = (c2.a == 0);
Fortunately (or unfortunately) the Java VM busted me on the specialization of p to c2. This happened because Java does both static and dynamic type checking. In other words, c1 had a type tag indicating that it was an instance of Child1, which the Java VM consulted before attempting the cast (Child2) p.

So how does one determine the byte order of the host platform? This would be of interest to people attempting to read binary files generated by native programs from their Java programs. The only solution is to go outside, or partially outside, of Java by writing 1 to a file from a C program:

int fd = creat("foo", 0777);
   fprintf(fd, "%d", 1);
Then read this file into a Java program:
 FileInputStream fin = new FileInputStream("foo");
   DataInputStream din = new DataInputStream(fin);
   int x = din.readInt();
   boolean bigEndian = (x == 1);
Of course this won't work in an applet because you can't read from or write to local files; but then, an applet shouldn't need to know the byte order of its host.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date