89 private volatile long handle = INVALID_HANDLE_VALUE;
90
91 // interest events
92 private Set<? extends WatchEvent.Kind<?>> events;
93
94 // subtree
95 private boolean watchSubtree;
96
97 // buffer for change events
98 private NativeBuffer buffer;
99
100 // pointer to bytes returned (in buffer)
101 private long countAddress;
102
103 // pointer to overlapped structure (in buffer)
104 private long overlappedAddress;
105
106 // completion key (used to map I/O completion to WatchKey)
107 private int completionKey;
108
109 WindowsWatchKey(AbstractWatchService watcher, FileKey fileKey) {
110 super(watcher);
111 this.fileKey = fileKey;
112 }
113
114 WindowsWatchKey init(long handle,
115 Set<? extends WatchEvent.Kind<?>> events,
116 boolean watchSubtree,
117 NativeBuffer buffer,
118 long countAddress,
119 long overlappedAddress,
120 int completionKey)
121 {
122 this.handle = handle;
123 this.events = events;
124 this.watchSubtree = watchSubtree;
125 this.buffer = buffer;
126 this.countAddress = countAddress;
127 this.overlappedAddress = overlappedAddress;
128 this.completionKey = completionKey;
129 return this;
130 }
388 long overlappedAddress = bufferAddress + size - SIZEOF_OVERLAPPED;
389 long countAddress = overlappedAddress - SIZEOF_DWORD;
390
391 // start async read of changes to directory
392 try {
393 ReadDirectoryChangesW(handle,
394 bufferAddress,
395 CHANGES_BUFFER_SIZE,
396 watchSubtree,
397 ALL_FILE_NOTIFY_EVENTS,
398 countAddress,
399 overlappedAddress);
400 } catch (WindowsException x) {
401 buffer.release();
402 return new IOException(x.getMessage());
403 }
404
405 WindowsWatchKey watchKey;
406 if (existing == null) {
407 // not registered so create new watch key
408 watchKey = new WindowsWatchKey(watcher, fk)
409 .init(handle, events, watchSubtree, buffer, countAddress,
410 overlappedAddress, completionKey);
411 // map file key to watch key
412 fk2key.put(fk, watchKey);
413 } else {
414 // directory already registered so need to:
415 // 1. remove mapping from old completion key to existing watch key
416 // 2. release existing key's resources (handle/buffer)
417 // 3. re-initialize key with new handle/buffer
418 int2key.remove(existing.completionKey());
419 existing.releaseResources();
420 watchKey = existing.init(handle, events, watchSubtree, buffer,
421 countAddress, overlappedAddress, completionKey);
422 }
423 // map completion map to watch key
424 int2key.put(completionKey, watchKey);
425
426 registered = true;
427 return watchKey;
428
|
89 private volatile long handle = INVALID_HANDLE_VALUE;
90
91 // interest events
92 private Set<? extends WatchEvent.Kind<?>> events;
93
94 // subtree
95 private boolean watchSubtree;
96
97 // buffer for change events
98 private NativeBuffer buffer;
99
100 // pointer to bytes returned (in buffer)
101 private long countAddress;
102
103 // pointer to overlapped structure (in buffer)
104 private long overlappedAddress;
105
106 // completion key (used to map I/O completion to WatchKey)
107 private int completionKey;
108
109 WindowsWatchKey(Path dir,
110 AbstractWatchService watcher,
111 FileKey fileKey)
112 {
113 super(dir, watcher);
114 this.fileKey = fileKey;
115 }
116
117 WindowsWatchKey init(long handle,
118 Set<? extends WatchEvent.Kind<?>> events,
119 boolean watchSubtree,
120 NativeBuffer buffer,
121 long countAddress,
122 long overlappedAddress,
123 int completionKey)
124 {
125 this.handle = handle;
126 this.events = events;
127 this.watchSubtree = watchSubtree;
128 this.buffer = buffer;
129 this.countAddress = countAddress;
130 this.overlappedAddress = overlappedAddress;
131 this.completionKey = completionKey;
132 return this;
133 }
391 long overlappedAddress = bufferAddress + size - SIZEOF_OVERLAPPED;
392 long countAddress = overlappedAddress - SIZEOF_DWORD;
393
394 // start async read of changes to directory
395 try {
396 ReadDirectoryChangesW(handle,
397 bufferAddress,
398 CHANGES_BUFFER_SIZE,
399 watchSubtree,
400 ALL_FILE_NOTIFY_EVENTS,
401 countAddress,
402 overlappedAddress);
403 } catch (WindowsException x) {
404 buffer.release();
405 return new IOException(x.getMessage());
406 }
407
408 WindowsWatchKey watchKey;
409 if (existing == null) {
410 // not registered so create new watch key
411 watchKey = new WindowsWatchKey(dir, watcher, fk)
412 .init(handle, events, watchSubtree, buffer, countAddress,
413 overlappedAddress, completionKey);
414 // map file key to watch key
415 fk2key.put(fk, watchKey);
416 } else {
417 // directory already registered so need to:
418 // 1. remove mapping from old completion key to existing watch key
419 // 2. release existing key's resources (handle/buffer)
420 // 3. re-initialize key with new handle/buffer
421 int2key.remove(existing.completionKey());
422 existing.releaseResources();
423 watchKey = existing.init(handle, events, watchSubtree, buffer,
424 countAddress, overlappedAddress, completionKey);
425 }
426 // map completion map to watch key
427 int2key.put(completionKey, watchKey);
428
429 registered = true;
430 return watchKey;
431
|