Non-portable Data Problem

Non-portable Data Problem

I’ve read, in several places, that Java sets bit-size and format for numeric representation internally. I can’t find anything that says Java also dictates how the data will be PHYSICALLY written to devices, such as disk. I’ve seen so many ‘portable’ languages, which do not take care of the problem of non-portable data: High-byte, low-byte stuff across multiple platforms. Does Java?

Indeed Java does a thorough job in this regard. To see the problems, let us look at the way a C programmer would save and read an integer.

        int x = 0xCAFE;        . . .        fwrite(&x, sizeof(x), 1, fp); /* C code */        . . .        fread(&x, sizeof(x), 1, fp);
This has two problems. First, it depends on the size of the int data type. On some machines, int is a 16-bit quantity, on others a 32-bit quantity. And it depends on the byte ordering. On an Intel processor, the least significant byte comes first in memory. The fwrite call would write the following sequence of bytes:
        0xFE 0xCA
        0xFE 0xCA 0x00 0x00
This is called the “little-endian” storage. The Sparc processor uses big-endian storage, thus the same code would produce the output
        0x00 0x00 0xCA 0xFE
As a result, a data file written on a PC cannot be read by the same program running on a Sparc. C code is portable, but it isn’t that portable.

In Java, this problem is solved completely.

  1. When writing a multi-byte quantity to a stream (which may be a file or a network connection, all quantities are written in the “big-endian” fashion, with the most significant byte first. For example, if
            int x = 0xCAFEBABE;
    then the bytes are written in the order
            0xCA 0xFE 0xBA 0xBE
    This is independent of the way the bytes are stored in memory–that of course depends on the processor (big-endian on Sparc, little-endian on Intel).

  2. The sizes and formats of all types are fixed. For example, int is a 4-byte 2’s complement signed number on all platforms.

    Naturally, it is very important that reading and writing numbers is processor-independent. Suppose you write an applet that needs to read a file containing some numbers from the server. You don’t know whether that applet runs on a machine with big-endian or little-endian storage. The same applet must work on both architectures, and it will.

    By the way, it is easy to find out exactly how Java writes and reads integers. If you have the JDK from Sun Microsystems, you get a file that contains the Java source of all classes in the standard libraries. (You don’t get the source code to the native methods, and you don’t get the source to the “sun” libraries, though.) Here is, for example, an excerpt from

        /**     * Writes a 32 bit int.     * @param v the integer value to be written     */    public final void writeInt(int v) throws IOException {   OutputStream out = this.out;   out.write((v >>> 24) & 0xFF);   out.write((v >>> 16) & 0xFF);   out.write((v >>>  8) & 0xFF);   out.write((v >>>  0) & 0xFF);   written += 4;    }
    As you can see, this code manually disassembles the integer into four bytes in big-endian order. Here is the code to read the integer back in.
        /**     * Reads a 32 bit int.     * @return the 32 bit integer read.     */    public final int readInt() throws IOException {   InputStream in =;   int ch1 =;   int ch2 =;   int ch3 =;   int ch4 =;   if ((ch1 | ch2 | ch3 | ch4) < 0)        throw new EOFException();   return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));    }
    While definitely an advanced technique, looking inside the source code can be very helpful to understand the inner workings of Java.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

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