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) < 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:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job