2

I getting the following exception when deserializing an Object:

java.io.OptionalDataException
    java.io.ObjectInputStream.readObject0(Unknown Source)
    java.io.ObjectInputStream.readObject(Unknown Source)
    java.util.HashSet.readObject(Unknown Source)
    sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)

The code I use to serialize and deserialize the object is as follows:

public class ObjectToFile
{
    public static void save(Object obj, String name)
    {
        FileOutputStream fos;
        try
        {
            ODebug.Write(Level.INFO, "SER-0049: Saving objects from file...");
            fos = new FileOutputStream(".//data//" + name);

            ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos));
            oos.writeObject(obj);

            oos.close();
            fos.close();
        }
        catch (Exception e)
        {
            ODebug.Write(Level.SEVERE, "SER-0000: Caught exception in save()", e);
        }
    }

    public static Object read(String name)
    {
        FileInputStream fis;
        Object obj;

        try
        {
            ODebug.Write(Level.INFO, "SER-0049: Reading objects from file...");

            fis = new FileInputStream(".//data//" + name);
            ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(fis));
            obj = ois.readObject();

            ois.close();
            fis.close();

            return obj;
        }
        catch (Exception e)
        {
            ODebug.Write(Level.SEVERE, "SER-0001: Caught exception in read()", e);
        }

        return null;
    }
}

The object I am trying to serialize/deserialize is a hashmap that looks like this:

public static HashMap<Integer, Document> ProductList = new HashMap<>();

The Document class has the following definition plus a few methods.

public class Document implements Serializable
{
     private static final long  serialVersionUID    = 1L;
     public Integer documentID;
     public DocumentZone titleZone = new DocumentZone();
     public DocumentZone    bodyZone = new DocumentZone();
}

Please help!

  • You don't need _Buffered Streams_, using directly _File Streams_ is fine. – Arnaud Feb 09 '16 at 13:18
  • I was using File Streams only but the process of reading and writing to the file was very slow and this enhanced it a lot. I dont beleive this is the issue as this was working fine earlier. – Georges Lteif Feb 09 '16 at 13:26
  • @Berger Adding buffered streams improves the performance. Using the file streams directly is rarely 'fine'. – user207421 Feb 09 '16 at 15:37
  • Right, I meant it is fine for this example, as it is easier to debug when there is less wrapping , and it would remove the buffered streams from the possible suspects. – Arnaud Feb 09 '16 at 15:48
  • @Berger I can't imagine how the buffered streams could ever be included among the possible suspects. – user207421 Feb 10 '16 at 02:39
  • 1
    I found that the culprit is the HashMap but I wasnt able to know why. Any help is most appreciated! – Georges Lteif Feb 14 '16 at 19:38

0 Answers0