I am getting PrematureEndOfFile exceptions when attempting to process files upon receiving ENTRY_CREATE events. The files that are monitored are transferred via ftp, and sleeping for a second after receiving the event solves the problem, which leads me to believe that the ENTRY_CREATE key is signalled before the file has been finished. Also, I never encounter this problem when local files are being moved into the monitored folder.
Does anyone know if the ENTRY_CREATE key in NIO2 is generated after a new file has been written, or does it appear instantly?
What is a PrematureEndOfFile exception? I've never seen that before and as far as I know it doesn't exist in the JDK (not even in 7).
Writing files is not an atomic operation, so an FTP server usually creates the file first, then writes the content. This means that just because a file has been created does not mean that the content will be "complete".
The reason you didn't see this when dropping files into a local folder is probably because moves within a file system (= drive in Windows, usually) are atomic, so you won't ever see a "half-moved" file. Try copying into the folder and you should be able to see the same effect (within a shorter time, however since local copies are usually very fast).
There's no reliable way to know when an application is "done" writing a file and NIO2 doesn't change that.
Ideally, you could configure your FTP server to write its files to some temporary name and rename it to the final name once the transfer is complete. Then you can just ignore all events where the name matches the temporary files.