6

Today I have came across strange behavior in java serialization and deserialization("strange" because I don't understand)

I was serializing and deserializing an object from a linux shared directory. While serializing everything worked without any problem, but when I tried to deserialize the same file it throws java. io. EOFException. Also deserializing was only failing for this newly created file and was working for all other old files in that directory.

So I searched across internet and found one thread which said Low disk space can also be cause of this error.

So I cleaned up some temp files and voila it worked. I do not understand how low disk space can only affect deserialization and not serialization?

I am using apache commons SerializationUtils class. Below is the code for serialization and deserialization

SerializationUtils. serialize(myObject, new FileOutputStream(new File(sharePath+FILEName) ;


MyObject object=SerializationUtils. deserialize( new FileInputStream(new File(sharePath+FILEName);

It would be really helpful if someone can explain this behavior. I suspect its a bug in SerializationUtils maybe gobbling up IOException.

Thanks

Dangling Piyush
  • 3,658
  • 8
  • 37
  • 52
  • It can't, unless you ignored a prior `IOException` when *writing* the file. – user207421 Dec 06 '16 at 10:00
  • Since I am not doing serialization by hand, IOExceptions are managed by SerializationUtils. Its in their javadoc. – Dangling Piyush Dec 06 '16 at 10:22
  • So it's their problem, or their poor API design. – user207421 Dec 10 '16 at 09:06
  • SerializationUtils is too simple to gobble the IOException, a SerializationException should be thrown if any IOException happens. Did you try on another system with another filesystem ? – ToYonos Dec 14 '16 at 16:06
  • @ToYonos No but it did work on the same file system after cleaning up memory. – Dangling Piyush Dec 15 '16 at 02:56
  • First of all check whether serialize object is complete or not , to check this serialize object at that JVM having low disk space and try to deserialize it in other machine hope this will track your problem. – Noman Akhtar Dec 16 '16 at 06:48

1 Answers1

8

My suspicion is that when writing the file, an ioexception is being thrown because the disk space has run out, but the beginning of the serialized data was still written to disk. This would mean that the serialized data stored on the disk is incomplete, so reading it would give invalid results, which in your case is causing an EOF Exception

In order to solve this problem, you need to see when the IO exception is thrown due to the disk space running out with exception.getMessage() and make sure not to write incomplete data.

This_Is_Alex_
  • 305
  • 1
  • 6
  • M using apache commons , and i couldn't find evidence of your claim inside it..let me know if you found something which is gobbling up IOException in apache's SerializationUtils.. – Dangling Piyush Dec 11 '16 at 18:55
  • 1
    Are you trying to catch the SerializationException? Because that will be thrown, not an IOException, if writing a file throws an IOException – This_Is_Alex_ Dec 16 '16 at 19:29
  • is this true ? http://weblog.janek.org/Archive/2004/12/20/ExceptionWhenWritingToAFu.html – Gaurav Feb 19 '19 at 15:45