Is serialization broken?

Barnet Wagman wagman@enteract.com
Fri Jan 26 00:14:00 GMT 2001


I'm getting an erroneous ClassCastException deserializing an object with
gcj,  gcc version 2.97 20010117 (experimental).
With the test program SerTest.java (below), I do the following
 
bash-2.03$ gcj -C SerTest.java
bash-2.03$ gcj --main=SerTest SerTest.class
bash-2.03$ a.out S
1000  1000  1000.0 10
T he serialization file gets created, but on deserialization:
bash-2.03$ a.out D
IOException from util.SerializeU.deserializeObject(): java.io.IOException
Exception in thread "main" java.lang.ClassCastException
   at 0x401991ca: java::lang::Throwable::Throwable()
(/usr/local/gcc/lib/libgcj.so.1)
   at 0x4018cc9d: java::lang::Exception::Exception()
(/usr/local/gcc/lib/libgcj.so.1)
   at 0x401911dd: java::lang::RuntimeException::RuntimeException()
(/usr/local/gcc/lib/libgcj.so.1)
   at 0x4018af5d: java::lang::ClassCastException::ClassCastException()
(/usr/local/gcc/lib/libgcj.so.1)
   at 0x401782ab: _Jv_CheckCast (/usr/local/gcc/lib/libgcj.so.1)
   at 0x0804c484: SerTest::main(JArray<java::lang::String*>*)
(/home/moi/GCJ_JavaTree/SerTest.class:0)
   at 0x4017498b: gnu::gcj::runtime::FirstThread::run()
(/usr/local/gcc/lib/libgcj.so.1)
   at 0x4017facb: java::lang::Thread::run_(java::lang::Object*)
(/usr/local/gcc/lib/libgcj.so.1)
   at 0x402a0d5a: _Jv_ThreadStart(java::lang::Thread*,
int*, void (*)(java::lang::Thread*)) (/usr/local/gcc/lib/libgcj.so.1)
   at 0x4017fbff: java::lang::Thread::start() (/usr/local/gcc/lib/libgcj.so.1)
   at 0x4015dea2: JvRunMain (/usr/local/gcc/lib/libgcj.so.1)
   at 0x0804cea3: main (/tmp/ccJoVWObmain.i:0)
   at 0x40473a5e: __libc_start_main (/lib/libc.so.6)
   at 0x08049a91: _start (??:0)
Any thoughts or suggestions?
Thanks,
Barnet

i mport java.util.*;
import java.io.*;
 
public class SerTest implements Serializable {
    HashMap hm;
    double[] v;
    public SerTest() {
        hm = new HashMap();
        v = new double [1000];
        for ( int i = 0; i <
v.length; i++ ) {
           
v[i] = ((double) i) * 100.0;
           
hm.put(new Integer(i), new Integer(i));
        }
    }
    public static void main(String[] argv) {
        SerTest st = null;
        if ( argv.length ==
1 ) {
           
if ( argv[0].equals("S") ) {
               
st = new SerTest();
               
serializeObject(st,"/home/moi/GCJ_JavaTree/xxx.ser");
           
}
           
else if ( argv[0].equals("D") ) {
               
st = (SerTest) deserializeObject("/home/moi/GCJ_JavaTree/xxx.ser");
           
}
           
System.err.println(st.hm.size() + "  " + st.v.length + "  " +
st.v[10] + " "
                              
+ st.hm.get(new Integer(10)) );
        }
    }
    public static boolean serializeObject(Object
obj, String filename) {
            
// If the file already exists, delete it.
        File f = new File(filename);
        if ( f.exists() )
           
f.delete();
        try {
            
FileOutputStream fos = new FileOutputStream(filename);
            
ObjectOutputStream oos = new ObjectOutputStream(fos);
            
oos.writeObject(obj);
            
oos.flush();
            
fos.close();
         }
         catch (IOException
e) {
            
System.err.println("IOException " + e );
            
return(false);
         }
         return(true);
    }
 
    public static Object deserializeObject(String
filename) {
        try {
           
FileInputStream fis = new FileInputStream(filename);
           
ObjectInputStream ois = new ObjectInputStream(fis);
           
Object obj = ois.readObject();
           
ois.close();
           
return(obj);
         }
         catch (FileNotFoundException
e) {
            
System.err.println("FileNotFoundException " + e );
         }
         catch (IOException
ex) {
            
System.err.println("IOException " + ex );
         }
         catch (ClassNotFoundException
ec) {
            
System.err.println("ClassNotFoundException " + ec );
         }
         return(new Object());
    }
}



More information about the Java mailing list