Write once? Java Serialization?

Hi,

I have a simple java serialization issue on solaris. The same piece of code runs on windows and linux, but Solaris throws an exception on me. Any idea?

WinXP SP2, jdk1.4.2_11

RH Linux 7.3, jdk1.4.2_11

Solaris9 jdk1.4.2_13

Here is my code:

Tester.java

import java.io.*;

public class Tester

{

String marshellChild(Child child) throws Exception

{

Parent parent = child.getParent();

ByteArrayOutputStream fo = new ByteArrayOutputStream();

ObjectOutputStream so = new ObjectOutputStream(fo);

so.writeObject(parent);

so.flush();

so.close();

return new String(fo.toByteArray());

}

Child unmarshellChild(String strep) throws Exception

{

byte[] bytes = new String(strep).getBytes();

InputStream io = new ByteArrayInputStream(bytes);

ObjectInputStream os = new ObjectInputStream(io);

Parent parent = (Parent) os.readObject();

return parent.getChild();

}

public static void main(String[] args) throws Exception

{

Parent parent = new Parent();

Child child = new Child();

parent.setChild(child);

child.setParent(parent);

Tester tester = new Tester();

String strep = tester.marshellChild(child);

System.out.println(strep);

Child newChild = tester.unmarshellChild(strep);

System.out.println(newChild);

}

}

Parent.java

import java.io.*;

public class Parent implements Serializable

{

private Child child;

public void setChild(Child child)

{

this.child = child;

}

public Child getChild()

{

return this.child;

}

public String getName()

{

return "PARENT";

}

}

Child.java

import java.io.*;

public class Child implements Serializable

{

private Parent parent;

public void setParent(Parent parent)

{

this.parent = parent;

}

public Parent getParent()

{

return this.parent;

}

public String getName()

{

return "CHILD";

}

}

[2197 byte] By [hsheng4a] at [2007-11-26 18:27:16]
# 1

What exception? Do you think it is irrelevant? This stinks:

new String(fo.toByteArray());

fo contains inherently binary data. If you make a String out of them using the platform default encoding (can you tell what it is?), what do you expect to obtain?

BIJ001a at 2007-7-9 6:01:23 > top of Java-index,Core,Core APIs...
# 2

OK, I agree new String(fo.toByteArray()) stinks.

But this is not the excuse of having different result on Windows, Linux and Solaris from the same piece of JAVA code.

Do you think the character encoding will make any difference?

I can simplely change that line of code to fo.toString(), that would use the platform's default encoding. Do you think I can get the same result on all three platform now? Here you go on Solaris,

Exception in thread "main" java.io.StreamCorruptedException: invalid stream header

hsheng4a at 2007-7-9 6:01:23 > top of Java-index,Core,Core APIs...
# 3

> OK, I agree new String(fo.toByteArray()) stinks.

> But this is not the excuse of having different result on Windows,

> Linux and Solaris from the same piece of JAVA code.

Oh yes it is. String is not a container for binary data. Use the byte[] array directly.

> I can simplely change that line of code to

> fo.toString(), that would use the platform's default

> encoding. Do you think I can get the same result on

> all three platform now?

No. The technique of using a String to hold binary data is still wrong.

ejpa at 2007-7-9 6:01:23 > top of Java-index,Core,Core APIs...
# 4
> But this is not the excuse of having different result> on Windows, Linux and Solaris from the same piece of> JAVA code.As mentioned above, the differing platform default encoding can break your code.
BIJ001a at 2007-7-9 6:01:23 > top of Java-index,Core,Core APIs...