Brad Pepers <[EMAIL PROTECTED]> wrote:
> Timothy Murphy wrote:
> >
> > Couldn't one write an int to a file,
> > and then read it as a character array?
> > [Just a slightly random thought.]
>
> I would imagine that if Java is done right the file format the
> int will be written in will be specified to have a particular
> endian that may or may not really reflect the endian internally.
>
> This must be true since you should be able to hook up two Java
> programs talking over a socket and use read/writeInt and not
> have to worry about endian. Object serialization would depend
> on this so you could un-serialize an object on a different VM
> than it was originally written on.
Indeed. From java.io.DataOutputStream:
/**
* Writes an <code>int</code> to the underlying output stream as four
* bytes, high byte first.
*
* @param v an <code>int</code> to be written.
* @exception IOException if an I/O error occurs.
* @see java.io.FilterOutputStream#out
* @since JDK1.0
*/
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;
}
You can see that it's written out in MSB, regardless of how the
JVM stores it in the word.
--
+---------------------------------------------+-----------------------------+
| Peter Naulls - [EMAIL PROTECTED] | |
| http://free.prohosting.com/~chocky/ | Java and JVM Consultant |
| Java for Risc OS and ARM - [EMAIL PROTECTED] | Technical Author |
| http://free.prohosting.com/~chocky/java/ | Program performance analyst |
+---------------------------------------------+-----------------------------+