Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

46
LINES

< > BotCompany Repo | #1015923 // NIO WatchService Self-Test

JavaX source code (desktop) [tags: archive use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (5336L/34K).

1  
!7
2  
3  
import java.nio.file.*;
4  
import static java.nio.file.StandardWatchEventKinds.*;
5  
6  
p {
7  
  File dir = programDir();
8  
  deleteAllProgramFiles();
9  
  makeProgramDir();
10  
  final Path path = dir.toPath();
11  
  
12  
  final WatchService watchService = FileSystems.getDefault().newWatchService();
13  
  final WatchKey watchKey = path.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY, OVERFLOW);
14  
  
15  
  thread {
16  
    sleepSeconds(2);
17  
    saveProgramTextFile("bla", "1");
18  
    sleepSeconds(1);
19  
    //watchKey.cancel();
20  
    watchService.close();
21  
  }
22  
  
23  
  final new Set<S> changes;
24  
  final Countdown countdown = fiveSeconds();
25  
  print("Waiting");
26  
  expectException(ClosedWatchServiceException.class, r {
27  
    while (!countdown.done()) {
28  
      final WatchKey wk = watchService.take();
29  
      print("Key taken");
30  
      
31  
      for (WatchEvent<?> event : wk.pollEvents()) pcall {
32  
        final Path changed = (Path) event.context();
33  
        print("Changed: " + changed + ", " + event.kind());
34  
        changes.add(changed.toFile().getName());
35  
      }
36  
      
37  
      bool valid = wk.reset();
38  
      if (!valid)
39  
        print("Key has been unregistered");
40  
    }
41  
  });
42  
    
43  
  //watchService.close();
44  
  bool ok = eq(changes, lithashset("bla", "bla_temp"));
45  
  print("Self-Test: " + ok);
46  
}

Author comment

Began life as a copy of #1013732

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): cfunsshuasjs, mqqgnosmbjvj, tvejysmllsmz

No comments. add comment

Snippet ID: #1015923
Snippet name: NIO WatchService Self-Test
Eternal ID of this version: #1015923/12
Text MD5: a09d8f01434d6111f3abb2c3db3979ee
Transpilation MD5: 7f84b8cb8b35507eff1818165f65d10b
Author: stefan
Category: javax / io
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): Yes
Created/modified: 2018-05-31 01:31:00
Source code size: 1303 bytes / 46 lines
Pitched / IR pitched: No / No
Views / Downloads: 293 / 791
Version history: 11 change(s)
Referenced in: [show references]