Byte order

Byte order

How does one find out the byte order of the platformon which JVM is running? For example, for a Java application that has to read a C structure from a file, the structure wouldbe 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 hasa unique address and holds one byte. Let M[i] denote the byte storedin 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 theirneighbors over which was the proper end to break on a soft boiledegg: 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 tocome 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 BigEndian 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.

See also  Does It Make Sense to Splurge on a Laptop?

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 dothis from within a Java program. If there was, it would breakJava’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 staticand dynamic type checking. In other words, c1 had a typetag indicating that it was an instance of Child1, which theJava 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 binaryfiles generated by native programs from their Java programs. The only solution is to go outside, or partially outside, ofJava 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’tread from or write to local files; but then, an applet shouldn’tneed to know the byte order of its host.

See also  5 Tips to Manage a Remote Workforce

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist