Search code examples
javaxmlsax

SAXException: Unexpected end of file after null


I'm getting the error in the title occasionally from a process the parses lots of XML files.

The files themselves seem OK, and running the process again on the same files that generated the error works just fine.

The exception occurs on a call to XMLReader.parse(InputStream is)

Could this be a bug in the parser (I use piccolo)? Or is it something about how I open the file stream?

No multithreading is involved.

Piccolo seemed like a good idea at the time, but I don't really have a good excuse for using it. I will to try to switch to the default SAX parser and see if that helps.

Update: It didn't help, and I found that Piccolo is considerably faster for some of the workloads, so I went back.


Solution

  • I should probably tell the end of this story: it was a stupid mistake. There were two processes: one that produces XML files and another that reads them. The reader just scans the directory and tries to process every new file it sees.

    Every once in a while, the reader would detect a file before the producer was done writing, and so it legitimately raised an exception for "Unexpected end of file". Since we're talking about small files here, this event was pretty rare. By the time I came to check, the producer would already finish writing the file, so to me it seemed like the parser was complaining for nothing.

    I wrote "No multithreading was involved". Obviously this was very misleading.

    One solution would be to write the file elsewhere and move it to the monitored folder only after it was done. A better solution would be to use a proper message queue.