JDK-4171142 : Deserialization fails for Class object of primitive type
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io:serialization
  • Affected Version: 1.1.3,1.2.0,1.2.1,1.2.2,1.3.0
  • Priority: P5
  • Status: Closed
  • Resolution: Fixed
  • OS:
    generic,solaris_2.5.1,solaris_7,windows_nt generic,solaris_2.5.1,solaris_7,windows_nt
  • CPU: generic,x86,sparc
  • Submitted: 1998-09-03
  • Updated: 2001-10-11
  • Resolved: 2000-12-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.0 betaFixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Description

Name: rm29839			Date: 09/03/98


package jp.go.etl.takagi.test.bug_deserializing_class_of_primitive_type;
import java.io.*;
public class Test {
    public static void main(String[] args) throws Exception {
        PipedOutputStream os = new PipedOutputStream();
        InputStream is = new PipedInputStream(os);
        ObjectOutput oo = new ObjectOutputStream(os);
        ObjectInput oi = new ObjectInputStream(is);
        oo.writeObject(Integer.class);
        System.out.println(oi.readObject());
        oo.writeObject(int.class);
        System.out.println(oi.readObject());
    }
}
 
> java jp.go.etl.takagi.test.bug_deserializing_class_of_primitive_type.Test
class java.lang.Integer
java.lang.ClassNotFoundException: int
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:352)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:233)
        at jp.go.etl.takagi.test.bug_deserializing_class_of_primitive_type.Test.main(Test.java:12)
(Review ID: 34926)
======================================================================

Name: skT88420			Date: 09/14/99


Consider the following example:

import java.io.*;
import java.util.*;

public class Serial implements Serializable {
    Class cl = int.class;

    public static void main(String[] args){
        try {
            FileOutputStream out = new FileOutputStream("tmp");
            ObjectOutputStream ous = new ObjectOutputStream(out);
            ous.writeObject(new Serial());
            ous.close();

            FileInputStream in = new FileInputStream("tmp");
            ObjectInputStream ins = new ObjectInputStream(in);
            Serial s = (Serial)ins.readObject();
            ins.close();
        } catch (Throwable e) {
            System.out.println(e);
        }
    }

}

when run, it produces the following results:

J:\borsotti\jtest>java Serial
java.lang.ClassNotFoundException: int

The same problem occurs if the value of the serializable
field is another primitive class, like e.g. double, void, etc.
(Review ID: 95240)
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: merlin-beta FIXED IN: merlin-beta INTEGRATED IN: merlin-beta VERIFIED IN: merlin-beta3
14-06-2004

EVALUATION See public summary.
11-06-2004

WORK AROUND The following override of ObjectInputStream.resolveClass() works around the problem encounted. The problem is that ObjectStreamClass.resolveClass() does not work for ObjectStreamClass descriptors for primitive types, such as int or long. class MyObjectInputStream extends ObjectInputStream { public MyObjectInputStream(InputStream is) throws IOException, StreamCorruptedException { super(is); } public Class resolveClass(ObjectStreamClass desc) throws ClassNotFoundException, IOException { try { return super.resolveClass(desc); } catch (ClassNotFoundException e) { if (desc.getName().equals("int")) return int.class; else /* check for other primitive types if one wishes.*/ throw e; } } }
11-06-2004

PUBLIC COMMENTS ObjectInputStream.resolveClass() will not find primitive types, such as int. If one requires to write primitive types into a serialization stream, see the workaround provided.
10-06-2004