Georges Lteif
Georges Lteif

Reputation: 17

Java Exception java.io.OptionalDataException

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!

Upvotes: 2

Views: 1224

Answers (0)

Related Questions