devxlogo

Non-portable Data Problem

Non-portable Data Problem

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

Answer:
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
or
        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 src.zip 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 DataOutputStream.java:

        /**     * 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 = this.in;   int ch1 = in.read();   int ch2 = in.read();   int ch3 = in.read();   int ch4 = in.read();   if ((ch1 | ch2 | ch3 | ch4) While definitely an advanced technique, looking inside the source code can be very helpful to understand the inner workings of Java.
    
devxblackblue

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