src/com/gynt/jdirwatch/windows/WatchDir.java
public void processEvents() {

		for (;;) {

			// wait for key to be signalled
			WatchKey key;

			key = watcher.poll();
			// key = watcher.take();

			if (key == null) {
				if(events.size() > 0 && System.currentTimeMillis()-lastEvent > cutoff) {
					EventSet set = new EventSet(events.toArray(new RawEvent[0]));
					queue.offer(set);
					events.clear();
					lastEvent = 0;
					System.out.format("EventSet: total: %s, path: %s\n", set.getEvents().length, set.getEvents()[0].getFullPath());
				}
				try {
					Thread.sleep(100);
				} catch (InterruptedException x) {
					return;
				}			
				continue;
			} 
			
			Path dir = keys.get(key);
			if (dir == null) {
				System.err.println("WatchKey not recognized!!");
				continue;
			}
			
			

			List<WatchEvent<?>> poll = key.pollEvents();
			System.out.println("Events in poll: " + poll.size());
			
			//TODO: Consider a single poll to be a chunk of events... Not sure how this works with slow computers.
			
			for (WatchEvent<?> event : poll) {
				WatchEvent.Kind<?> kind = event.kind();

				// TBD - provide example of how OVERFLOW event is handled
				if (kind == StandardWatchEventKinds.OVERFLOW) {
					continue;
				}

				// Context for directory entry event is the file name of entry
				WatchEvent<Path> ev = cast(event);
				Path name = ev.context();
				Path child = dir.resolve(name);
				
				RawEvent raw = new RawEvent(ev, dir);
				
				if(events.size()>0) {
					if(events.get(events.size()-1).getFullPath().equals(raw.getFullPath())) {
						events.add(raw);
						lastEvent = System.currentTimeMillis();
						System.out.format("Add event to Set: path: %s\n", child);
					} else {
						EventSet set = new EventSet(events.toArray(new RawEvent[0]));
						queue.offer(set);
						events.clear();
						lastEvent = 0;
						System.out.format("EventSet: total: %s, path: %s\n", set.getEvents().length, child);
					}
				} else {
					events.add(raw);
					lastEvent = System.currentTimeMillis();
					System.out.format("Add event to Set: path: %s\n", child);
				}

				// print out event
				System.out.format("%s: count: %s path: %s\n", event.kind().name(), event.count(), child);

				// if directory is created, and watching recursively, then
				// register it and its sub-directories
				// if (recursive && (kind ==
				// StandardWatchEventKinds.ENTRY_CREATE)) {
				// try {
				// if (Files.isDirectory(child, LinkOption.NOFOLLOW_LINKS)) {
				// registerAll(child);
				// }
				// } catch (IOException x) {
				// // ignore to keep sample readbale
				// }
				// }
			}

			// reset key and remove from set if directory no longer accessible
			boolean valid = key.reset();
			if (!valid) {
				keys.remove(key);

				// all directories are inaccessible
				if (keys.isEmpty()) {
					break;
				}
			}
		}
	}
/**
	 * Process all events for keys queued to the watcher
	 */
Process all events for keys queued to the watcher
Process all events for keys queued to the watcher
public void processEvents() {
		for (;;) {
			WatchKey key;
			key = watcher.poll();
			if (key == null) {
				if(events.size() > 0 && System.currentTimeMillis()-lastEvent > cutoff) {
					EventSet set = new EventSet(events.toArray(new RawEvent[0]));
					queue.offer(set);
					events.clear();
					lastEvent = 0;
					System.out.format("EventSet: total: %s, path: %s\n", set.getEvents().length, set.getEvents()[0].getFullPath());
				}
				try {
					Thread.sleep(100);
				} catch (InterruptedException x) {
					return;
				}			
				continue;
			} 
			Path dir = keys.get(key);
			if (dir == null) {
				System.err.println("WatchKey not recognized!!");
				continue;
			}
			List<WatchEvent<?>> poll = key.pollEvents();
			System.out.println("Events in poll: " + poll.size());
			for (WatchEvent<?> event : poll) {
				WatchEvent.Kind<?> kind = event.kind();
				if (kind == StandardWatchEventKinds.OVERFLOW) {
					continue;
				}
				WatchEvent<Path> ev = cast(event);
				Path name = ev.context();
				Path child = dir.resolve(name);
				RawEvent raw = new RawEvent(ev, dir);
				if(events.size()>0) {
					if(events.get(events.size()-1).getFullPath().equals(raw.getFullPath())) {
						events.add(raw);
						lastEvent = System.currentTimeMillis();
						System.out.format("Add event to Set: path: %s\n", child);
					} else {
						EventSet set = new EventSet(events.toArray(new RawEvent[0]));
						queue.offer(set);
						events.clear();
						lastEvent = 0;
						System.out.format("EventSet: total: %s, path: %s\n", set.getEvents().length, child);
					}
				} else {
					events.add(raw);
					lastEvent = System.currentTimeMillis();
					System.out.format("Add event to Set: path: %s\n", child);
				}
				System.out.format("%s: count: %s path: %s\n", event.kind().name(), event.count(), child);
			}
			boolean valid = key.reset();
			if (!valid) {
				keys.remove(key);
				if (keys.isEmpty()) {
					break;
				}
			}
		}
	}
public void processEvents ( ) { for ( ; ; ) { WatchKey key ; key = watcher . poll ( ) ; if ( key == null ) { if ( events . size ( ) > 0 && System . currentTimeMillis ( ) - lastEvent > cutoff ) { EventSet set = new EventSet ( events . toArray ( new RawEvent [ 0 ] ) ) ; queue . offer ( set ) ; events . clear ( ) ; lastEvent = 0 ; System . out . format ( "EventSet: total: %s, path: %s\n" , set . getEvents ( ) . length , set . getEvents ( ) [ 0 ] . getFullPath ( ) ) ; } try { Thread . sleep ( 100 ) ; } catch ( InterruptedException x ) { return ; } continue ; } Path dir = keys . get ( key ) ; if ( dir == null ) { System . err . println ( "WatchKey not recognized!!" ) ; continue ; } List < WatchEvent < ? > > poll = key . pollEvents ( ) ; System . out . println ( "Events in poll: " + poll . size ( ) ) ; for ( WatchEvent < ? > event : poll ) { WatchEvent . Kind < ? > kind = event . kind ( ) ; if ( kind == StandardWatchEventKinds . OVERFLOW ) { continue ; } WatchEvent < Path > ev = cast ( event ) ; Path name = ev . context ( ) ; Path child = dir . resolve ( name ) ; RawEvent raw = new RawEvent ( ev , dir ) ; if ( events . size ( ) > 0 ) { if ( events . get ( events . size ( ) - 1 ) . getFullPath ( ) . equals ( raw . getFullPath ( ) ) ) { events . add ( raw ) ; lastEvent = System . currentTimeMillis ( ) ; System . out . format ( "Add event to Set: path: %s\n" , child ) ; } else { EventSet set = new EventSet ( events . toArray ( new RawEvent [ 0 ] ) ) ; queue . offer ( set ) ; events . clear ( ) ; lastEvent = 0 ; System . out . format ( "EventSet: total: %s, path: %s\n" , set . getEvents ( ) . length , child ) ; } } else { events . add ( raw ) ; lastEvent = System . currentTimeMillis ( ) ; System . out . format ( "Add event to Set: path: %s\n" , child ) ; } System . out . format ( "%s: count: %s path: %s\n" , event . kind ( ) . name ( ) , event . count ( ) , child ) ; } boolean valid = key . reset ( ) ; if ( ! valid ) { keys . remove ( key ) ; if ( keys . isEmpty ( ) ) { break ; } } } }
Process all events for keys queued to the watcher
Process all events for keys queued to the watcher
// wait for key to be signalled
 // key = watcher.take();
 //TODO: Consider a single poll to be a chunk of events... Not sure how this works with slow computers.
 // TBD - provide example of how OVERFLOW event is handled
 // Context for directory entry event is the file name of entry
 // print out event
 // if directory is created, and watching recursively, then
 // register it and its sub-directories
 // if (recursive && (kind ==
 // StandardWatchEventKinds.ENTRY_CREATE)) {
 // try {
 // if (Files.isDirectory(child, LinkOption.NOFOLLOW_LINKS)) {
 // registerAll(child);
 // }
 // } catch (IOException x) {
 // // ignore to keep sample readbale
 // }
 // }
 // reset key and remove from set if directory no longer accessible
 // all directories are inaccessible
