Byte order

Byte order

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

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

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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as