From db275620ec0a68a6603192d71f97f59b5919b260 Mon Sep 17 00:00:00 2001 From: yukihane Date: Thu, 1 Sep 2011 10:46:48 +0900 Subject: [PATCH] =?utf8?q?=E3=83=87=E3=82=A3=E3=83=AC=E3=82=AF=E3=83=88?= =?utf8?q?=E3=83=AA=E5=86=85=E3=81=AE=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?utf8?q?=E8=BF=BD=E5=8A=A0/=E5=89=8A=E9=99=A4=E7=9B=A3=E8=A6=96=E3=82=B5?= =?utf8?q?=E3=83=BC=E3=83=93=E3=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../src/yukihane/inqubus/filewatch/FileWatch.java | 108 +++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 frontend/src/yukihane/inqubus/filewatch/FileWatch.java diff --git a/frontend/src/yukihane/inqubus/filewatch/FileWatch.java b/frontend/src/yukihane/inqubus/filewatch/FileWatch.java new file mode 100644 index 0000000..7c100fa --- /dev/null +++ b/frontend/src/yukihane/inqubus/filewatch/FileWatch.java @@ -0,0 +1,108 @@ +package yukihane.inqubus.filewatch; + +import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardWatchEventKinds; +import java.nio.file.WatchEvent; +import java.nio.file.WatchEvent.Kind; +import java.nio.file.WatchKey; +import java.nio.file.WatchService; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +/** + * + * @author yuki + */ +public final class FileWatch implements Runnable { + + private final Set files; + private final Set directories; + + public FileWatch(Collection dirs) throws IOException { + FileDir res = new FileDir(); + getFileDir(dirs, res); + + this.files = new HashSet<>(res.files); + this.directories = new HashSet<>(res.dirs); + } + + public Set getFiles() { + synchronized (files) { + return new HashSet<>(files); + } + } + + private void getFileDir(Collection paths, FileDir res) throws IOException { + for (Path p : paths) { + getFileDir(p, res); + } + } + + private void getFileDir(Path p, FileDir res) throws IOException { + + if (Files.isDirectory(p)) { + res.dirs.add(p); + DirectoryStream dir = Files.newDirectoryStream(p); + for (Path pp : dir) { + getFileDir(pp, res); + } + } else if (Files.isRegularFile(p)) { + res.files.add(p); + } + } + + private static class FileDir { + + private final Set files = new HashSet<>(); + private final Set dirs = new HashSet<>(); + } + + @Override + public void run() { + try { + final WatchService ws = FileSystems.getDefault().newWatchService(); + for (Path p : directories) { + p.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE); + } + + while (true) { + final WatchKey wk = ws.take(); + for (final WatchEvent event : wk.pollEvents()) { + final Kind kind = event.kind(); + if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE)) { + final Path p = (Path) event.context(); + if (Files.isRegularFile(p)) { + synchronized (files) { + files.add(p); + } + } + } else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE)) { + final Path p = (Path) event.context(); + if (Files.isRegularFile(p)) { + synchronized (files) { + files.remove(p); + } + } + } + } + + if (!wk.reset()) { + System.out.println("No longer valid"); + wk.cancel(); + ws.close(); + break; + } + + } + } catch (InterruptedException ex) { + ex.printStackTrace(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } +} -- 2.11.0