2929import java .nio .file .DirectoryIteratorException ;
3030import java .nio .file .DirectoryStream ;
3131import java .nio .file .Files ;
32- import java .nio .file .LinkOption ;
3332import java .nio .file .NotDirectoryException ;
3433import java .nio .file .Path ;
3534import java .nio .file .StandardWatchEventKinds ;
3635import java .nio .file .WatchEvent ;
3736import java .nio .file .WatchKey ;
3837import java .nio .file .attribute .BasicFileAttributes ;
38+ import java .nio .file .attribute .FileTime ;
3939import java .security .AccessController ;
4040import java .security .PrivilegedAction ;
4141import java .security .PrivilegedExceptionAction ;
5151import java .util .concurrent .ScheduledFuture ;
5252import java .util .concurrent .ThreadFactory ;
5353import java .util .concurrent .TimeUnit ;
54+ import static java .nio .file .LinkOption .NOFOLLOW_LINKS ;
5455
5556/**
5657 * Simple WatchService implementation that uses periodic tasks to poll
@@ -222,10 +223,10 @@ public Void run() {
222223 * Entry in directory cache to record file last-modified-time and tick-count
223224 */
224225 private static class CacheEntry {
225- private long lastModified ;
226+ private FileTime lastModified ;
226227 private int lastTickCount ;
227228
228- CacheEntry (long lastModified , int lastTickCount ) {
229+ CacheEntry (FileTime lastModified , int lastTickCount ) {
229230 this .lastModified = lastModified ;
230231 this .lastTickCount = lastTickCount ;
231232 }
@@ -234,11 +235,11 @@ int lastTickCount() {
234235 return lastTickCount ;
235236 }
236237
237- long lastModified () {
238+ FileTime lastModified () {
238239 return lastModified ;
239240 }
240241
241- void update (long lastModified , int tickCount ) {
242+ void update (FileTime lastModified , int tickCount ) {
242243 this .lastModified = lastModified ;
243244 this .lastTickCount = tickCount ;
244245 }
@@ -280,8 +281,7 @@ private class PollingWatchKey extends AbstractWatchKey {
280281 try (DirectoryStream <Path > stream = Files .newDirectoryStream (dir )) {
281282 for (Path entry : stream ) {
282283 // don't follow links
283- long lastModified =
284- Files .getLastModifiedTime (entry , LinkOption .NOFOLLOW_LINKS ).toMillis ();
284+ FileTime lastModified = Files .getLastModifiedTime (entry , NOFOLLOW_LINKS );
285285 entries .put (entry .getFileName (), new CacheEntry (lastModified , tickCount ));
286286 }
287287 } catch (DirectoryIteratorException e ) {
@@ -358,10 +358,9 @@ synchronized void poll() {
358358 // iterate over all entries in directory
359359 try {
360360 for (Path entry : stream ) {
361- long lastModified = 0L ;
361+ FileTime lastModified ;
362362 try {
363- lastModified =
364- Files .getLastModifiedTime (entry , LinkOption .NOFOLLOW_LINKS ).toMillis ();
363+ lastModified = Files .getLastModifiedTime (entry , NOFOLLOW_LINKS );
365364 } catch (IOException x ) {
366365 // unable to get attributes of entry. If file has just
367366 // been deleted then we'll report it as deleted on the
@@ -373,8 +372,7 @@ synchronized void poll() {
373372 CacheEntry e = entries .get (entry .getFileName ());
374373 if (e == null ) {
375374 // new file found
376- entries .put (entry .getFileName (),
377- new CacheEntry (lastModified , tickCount ));
375+ entries .put (entry .getFileName (), new CacheEntry (lastModified , tickCount ));
378376
379377 // queue ENTRY_CREATE if event enabled
380378 if (events .contains (StandardWatchEventKinds .ENTRY_CREATE )) {
@@ -393,7 +391,7 @@ synchronized void poll() {
393391 }
394392
395393 // check if file has changed
396- if (e .lastModified != lastModified ) {
394+ if (! e .lastModified (). equals ( lastModified ) ) {
397395 if (events .contains (StandardWatchEventKinds .ENTRY_MODIFY )) {
398396 signalEvent (StandardWatchEventKinds .ENTRY_MODIFY ,
399397 entry .getFileName ());
0 commit comments