I have a following usecase.
- A process serializes certain objects to a file using
BufferedOutputStream
. - After writing each object, process invokes
flush()
- The use case is that if the process crashes while writing an object, I want to recover the file upto the previous object that has been written successfully.
How can I deserialize such file? How will Java behave while deserializing such file.
- Will it successfully deserialize upto the object that were written successfully before crash?
- While reading the last partially written object, what will be the behavior. How can I detect that?
Update1 -
- I have tried to simulate process crash via manually killing the process while objects are being written. I have tried around 10-15 times.Each time i am able to deserialize the file and file does not has any partial object.
I am not sure if my test is exhaustive enough and therefore need further advice.
Update2 - Adam had pointed a way which could simulate such test using truncating the file randomly. Following is the behavior observed for trying out around 100 iterations -
- From the truncated file ( which should be equivalent to the condition of file when a process crashes), Java can read upto last complete object successfully.
- Upon reaching the last partially written object, Java does not throw any
StreamCorruptedException
orIOException
. It simply throwsEOFException
indicatedEOF
and ignores the partial object.