Java WatchService and Firefox strange behavior

25 Views Asked by At

I use the sample from https://docs.oracle.com/javase/tutorial/essential/io/notification.html to watch user downloads folder in order to react when a specific file type is downloaded by user. The code listens only for ENTRY_CREATE events and I have discovered a strange behavior of Firefox: the code received ENTRY_CREATE event twice. I have modified the code to listen to all type of file events:

for (WatchEvent<?> event : key.pollEvents()) {
  WatchEvent.Kind<?> kind = event.kind();
  WatchEvent<Path> ev = (WatchEvent<Path>) event;
  Path filename = ev.context();
  Path filePath = downloadsFolder.resolve(filename);
  
  System.out.println(kind + ": " + filePath + ": " + filePath.toFile().length());
}

and for a file 'sample.pdf' downloaded via Firefox the log was:

ENTRY_CREATE: C:\Users\<user.home>\Downloads\sample.pdf: 458971
ENTRY_DELETE: C:\Users\<user.home>\Downloads\sample.pdf: 458971
ENTRY_CREATE: C:\Users\<user.home>\Downloads\sample.pdf: 458971
ENTRY_MODIFY: C:\Users\<user.home>\Downloads\sample.pdf: 458971

Is this a Firefox bug? What's the point of creating, deleting and than creating once again the same file?

0

There are 0 best solutions below