United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4171142 Deserialization fails for Class object of primitive type
JDK-4171142 : Deserialization fails for Class object of primitive type

Details
Type:
Bug
Submit Date:
1998-09-03
Status:
Closed
Updated Date:
2001-10-11
Project Name:
JDK
Resolved Date:
2000-12-12
Component:
core-libs
OS:
windows_nt,solaris_7,solaris_2.5.1,generic
Sub-Component:
java.io:serialization
CPU:
x86,sparc,generic
Priority:
P5
Resolution:
Fixed
Affected Versions:
1.1.3,1.2.0,1.2.1,1.2.2,1.3.0
Fixed Versions:
1.4.0 (beta)

Related Reports
Duplicate:
Duplicate:
Relates:
Relates:
Relates:

Sub Tasks

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
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.

                                     
2004-06-10
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;
        }
    }
}
                                     
2004-06-11
EVALUATION

See public summary.
                                     
2004-06-11
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
merlin-beta

FIXED IN:
merlin-beta

INTEGRATED IN:
merlin-beta

VERIFIED IN:
merlin-beta3


                                     
2004-06-14



Hardware and Software, Engineered to Work Together