ObjectInputStream - cast exception

Hi;

I make the call:

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serFile));

Document correctDoc = (Document) ois.readObject();

And get the exception:
1) testTemplates(net.windward.format.wordml.test.TestWordMLParser)java.lang.ClassCastException: Unable to cast object of type 'System.Byte[]' to type 'System.Object[]'.
   at java.io.ObjectInputStream.fillArray(Object obj, Int32 len, Class elemType)
   at java.io.ObjectInputStream.readObject()
   at java.io.ObjectInputStream.fillArray(Object obj, Int32 len, Class elemType)
   at java.io.ObjectInputStream.readObject()
   at java.io.ObjectInputStream.getFieldValue(Char typeCode)
   at java.io.ObjectInputStream.defaultReadObject()
   at java.io.ObjectInputStream.readObject()
   at java.io.ObjectInputStream.getFieldValue(Char typeCode)
   at java.io.ObjectInputStream.defaultReadObject()
   at java.io.ObjectInputStream.readObject()
   at java.io.ObjectInputStream.getFieldValue(Char typeCode)
   at java.io.ObjectInputStream.defaultReadObject()
   at java.io.ObjectInputStream.readObject()
   at java.io.ObjectInputStream.getFieldValue(Char typeCode)
   at java.io.ObjectInputStream.defaultReadObject()
   at java.io.ObjectInputStream.readObject()
   at java.io.ObjectInputStream.getFieldValue(Char typeCode)
   at java.io.ObjectInputStream.defaultReadObject()
   at java.io.ObjectInputStream.readObject()
   at java.io.ObjectInputStream.getFieldValue(Char typeCode)
   at java.io.ObjectInputStream.defaultReadObject()
   at java.io.ObjectInputStream.readObject()
   at net.windward.format.wordml.test.TestWordMLParser.testTemplates() in C:\src\wr\DotNetEngine\TestEngine\net\windward\format\wordml\test\TestWordMLParser.java:line 119
   at net.windward.test.MyTestRunner.main(String[] args) in C:\src\wr\DotNetEngine\TestEngine\net\windward\test\MyTestRunner.jsl:line 50




Answer this question

ObjectInputStream - cast exception

  • Miguel Loureiro

    Hi David,
    After deeper investigation we have found that this is more a design choice we made rather than a bug. Here is the inputs from our Program manager Pratap Lakshman...

    Background:
    J# users can use both Java serialization or .NET serialization.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    Java serialization understands only types that explicitly support Java serialization, or types intrinsically understood by the Java serialization engine (for e.g. Java language primitive types). All Java language primitive types are signed. System.Byte is an unsigned type, and not recognized by the Java language, and does not explicitly support Java serialization.
    Not automatically enabling Java serialization for any System.* type is by design. We would end up having to support it for all System.* types!
    (BTW, you could repro the bug with other unsigned types too (for e.g. try System.UInt32).
    On the other hand, if a JDK type is Java serializable, then J# makes it .NET serializable too.

    Our guidance to users must be as follows:
    (1) Use only Java serializable types in your class if you want to support both Java serialization as well as .NET serialization of the same code base. This implies that you use only signed Java-language primitive types, or their equivalent System.* types, or JDK types that support Java serialization.

    (2) If your class has unsigned types (like ubyte), or their equivalent System.* types, or types that do not already support Java serialization, you must either use (a) .NET serialization, or (b) custom serialization.

    Please post back if you need more description here.

    Thanks.



  • dimondlight

    Hi David,
    We are looking into this issue and will get back to you soon. Looks like it repros when there is a byte array inside the serialized class and i try to deserialize it.

    Can you please tell us...

    1. what exactly the Document class contains
    2. Does it contain a Byte Array.
    3. Is it javax.swing.text.Documnet type

    Thanks.



  • JoelB2

    Hi David,
    At this moment it is rather difficult to predict any timeline. We will keep you updated on this issue.

    Thanks.

  • SnakeLair

    Hi;

    It contains a lot of classes that each contain a lot of stuff. There is definitely a byte[] in it. Also, there is definitely NOT any swing classes.

    thanks - dave

  • snowsquirrel

    Hello;

    I do not have a System.Byte[] in my class, I have a java.lang.Byte[] in there. I know that it is java only because I build and run the same code on java.

    - thanks - dave

    ps - I agree with you about not supporting the .net classes in the java serializer.

  • Zaerion

    Hi,

    I have tried to repro this issue. java.lang.byte serialization works perfectly for me. Here is the code snippet i have tried..

    import java.io.*;

    public class Program implements java.io.Serializable
    {
      public java.lang.Byte[] byteArr = new java.lang.Byte[10];

      public static void main(String[] args) throws Exception
      {
        Program one = new Program();
        FileOutputStream file = new java.io.FileOutputStream("fileout");
        ObjectOutputStream output = new java.io.ObjectOutputStream(file);
        output.writeObject(one);
        output.flush();
        output.close();
        file.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("fileout"));
        Program correctDoc = (Program) ois.readObject();
      }
    }

    Can you please check if this code works for you If yes then can you please send the repro code for this issue

    Thanks.

     



  • Mike Lavender

    Hi David,
    We looked more into this issue and found that it's a bug on us :(. J# serialization fails if the Class being serialized has a Byte[].
    To work around this issue you may like to use .net serialization. I have checked it for the same class which was giving exception in J# and it worked fine. Here is the example using .net serialization ...

    package ConsoleApplication1;
    import System.Runtime.Serialization.*;
    import System.Runtime.Serialization.Formatters.Binary.*;
    import System.*;
    import System.IO.*;

    /** @attribute System.Serializable()
    */

    public class Program
    {
       public System.Byte [] byteArr = new System.Byte[10];
       public static void main(String[] args)
       {
          Program c = new Program(); 
          Stream s = File.Open("temp.dat", FileMode.Create);
          BinaryFormatter b = new BinaryFormatter();
          b.Serialize(s, c);
          s.Close();
          s =
    File.Open("temp.dat", FileMode.Open);
          b =
    new BinaryFormatter();
          c = (
    Program)b.Deserialize(s); 
          s.Close();
       }
    }

    Please don't forget to apply System.Serializable attribute to your class, as i have done in above example.
    To know more about .net serialization you may refer this link.

    Again we really appreciate your invaluable feedback and great efforts for reporting these issues.

    Thanks.



  • Darren Baldwin

    Hi;

    This is for a unti test that I use in java as well as J# so I can't use the .net serialization. Any idea when you will have a fix for this

    thanks - dave

  • ObjectInputStream - cast exception