- java.lang.Object
-
- eu.ess.xaos.core.util.io.DirectoryWatcher
-
- All Implemented Interfaces:
AutoCloseable
public class DirectoryWatcher extends Object implements AutoCloseable
Watches for changes in files and directories, and allows for standard operations on both files and directories.Usage:
ExecutorService executor = Executors.newSingleThreadExecutor(); DirectoryWatcher watcher = build(executor); watcher.events().subscribe(event -> { event.getEvents().stream().forEach(e -> { if ( StandardWatchEventKinds.ENTRY_CREATE.equals(e.kind()) ) { ... } else if ( StandardWatchEventKinds.ENTRY_DELETE.equals(e.kind()) ) { ... } else if ( StandardWatchEventKinds.ENTRY_MODIFY.equals(e.kind()) ) { ... } }); }); Path root = ... watcher.watch(root);
- Author:
- claudio.rosati@esss.se
- See Also:
- LiveDirsFX:org.fxmisc.livedirs.DirWatcher
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DirectoryWatcher.DirectoryEvent
Contains the information about entry create, delete or modify occurred to a watched directory.
-
Constructor Summary
Constructors Modifier Constructor Description protected
DirectoryWatcher(Executor eventThreadExecutor)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static DirectoryWatcher
build(Executor eventThreadExecutor)
Creates aDirectoryWatcher
instance.void
close()
Shutdown this watcher.void
createDirectories(Path dir, Consumer<Path> onSuccess, Consumer<Throwable> onError, FileAttribute<?>... attrs)
Create a new directory by creating all nonexistent parent directories first.void
createDirectory(Path dir, Consumer<Path> onSuccess, Consumer<Throwable> onError, FileAttribute<?>... attrs)
Create a new directory using the given path.void
createFile(Path file, Consumer<FileTime> onSuccess, Consumer<Throwable> onError, FileAttribute<?>... attrs)
Create a new file using the given path.void
delete(Path path, Consumer<Boolean> onSuccess, Consumer<Throwable> onError)
Deletes a file or an empty directory.void
deleteTree(Path root, Consumer<Void> onSuccess, Consumer<Throwable> onError)
Deletes a file tree rooted at the given path.io.reactivex.Observable<Throwable>
errors()
io.reactivex.Observable<DirectoryWatcher.DirectoryEvent>
events()
boolean
isCloseComplete()
Returnstrue
if this watcher was shutdown, and the shutdown process is completed (i.e.boolean
isClosed()
Returnstrue
if this watcher was shutdown, meaning that no elements will be posted to the errors and events streams.boolean
isWatched(Path dir)
Return whether the givenPath
is watched or not.void
readBinaryFile(Path file, Consumer<byte[]> onSuccess, Consumer<Throwable> onError)
Reads the contents of a binary file.void
readTextFile(Path file, Charset charset, Consumer<String> onSuccess, Consumer<Throwable> onError)
Reads the contents of a text file.void
unwatch(Path dir)
Unwatch the given directoryPath
.void
unwatchUp(Path dir)
Unwatch the given directory and all its parents.void
unwatchUp(Path dir, Path to)
Unwatch the given directory and all its parents up to the given ancestor.void
watch(Path dir)
Watch the given directory for entry create, delete, and modify events.void
watchOrStreamError(Path dir)
Watch the given directory for entry create, delete, and modify events.void
watchUp(Path dir)
Watch the given directory and all its parents for entry create, delete, and modify events.void
watchUp(Path dir, Path to)
Watch the given directory and all its parents up to the given ancestor, for entry create, delete, and modify events.void
watchUpOrStreamError(Path dir)
Watch the given directory and all its parents for entry create, delete, and modify events.void
watchUpOrStreamError(Path dir, Path to)
Watch the given directory and all its parents up to the given ancestor, for entry create, delete, and modify events.void
writeBinaryFile(Path file, byte[] content, Consumer<FileTime> onSuccess, Consumer<Throwable> onError)
Writes a binary file filling it with the givencontent
.void
writeTextFile(Path file, String content, Charset charset, Consumer<FileTime> onSuccess, Consumer<Throwable> onError)
Writes a text file filling it with the givencontent
.
-
-
-
Constructor Detail
-
DirectoryWatcher
protected DirectoryWatcher(Executor eventThreadExecutor) throws IOException
- Throws:
IOException
-
-
Method Detail
-
build
public static DirectoryWatcher build(Executor eventThreadExecutor) throws IOException
Creates aDirectoryWatcher
instance.- Parameters:
eventThreadExecutor
- TheExecutor
used to queue I/O events.- Returns:
- A newly created
DirectoryWatcher
instance. - Throws:
IOException
- If an I/O error occurs.
-
close
public void close()
Shutdown this watcher.Note: this method will not shutdown the event thread
Executor
used to create this watcher.- Specified by:
close
in interfaceAutoCloseable
-
createDirectories
public void createDirectories(Path dir, Consumer<Path> onSuccess, Consumer<Throwable> onError, FileAttribute<?>... attrs)
Create a new directory by creating all nonexistent parent directories first. One of the two givenConsumer
s will be called on success or on failure.
-
createDirectory
public void createDirectory(Path dir, Consumer<Path> onSuccess, Consumer<Throwable> onError, FileAttribute<?>... attrs)
Create a new directory using the given path. One of the two givenConsumer
s will be called on success or on failure.The
createDirectories
method should be used where it is required to create all nonexistent parent directories first.
-
createFile
public void createFile(Path file, Consumer<FileTime> onSuccess, Consumer<Throwable> onError, FileAttribute<?>... attrs)
Create a new file using the given path. One of the two givenConsumer
s will be called on success or on failure.Files.createFile(java.nio.file.Path, java.nio.file.attribute.FileAttribute...)
will be called to actually create the file.Note: the operation is executed by the
Executor
passed to thebuild(java.util.concurrent.Executor)
method, i.e. in a different thread from the caller's one.
-
delete
public void delete(Path path, Consumer<Boolean> onSuccess, Consumer<Throwable> onError)
Deletes a file or an empty directory. One of the two givenConsumer
s will be called on success or on failure.Files.delete(java.nio.file.Path)
will be called to actually delete the file or empty directory.Note: the operation is executed by the
Executor
passed to thebuild(java.util.concurrent.Executor)
method, i.e. in a different thread from the caller's one.- Parameters:
path
- Pathname of the file or directory to be deleted.onSuccess
- TheConsumer
called on success, where the passed parameter isBoolean.TRUE
if the file was deleted by this method;Boolean.FALSE
if the file could not be deleted because it did not exist.onError
- TheConsumer
called on failure.
-
deleteTree
public void deleteTree(Path root, Consumer<Void> onSuccess, Consumer<Throwable> onError)
Deletes a file tree rooted at the given path. One of the two givenConsumer
s will be called on success or on failure.
-
errors
public io.reactivex.Observable<Throwable> errors()
- Returns:
- The
Subject
of thrown errors.
-
events
public io.reactivex.Observable<DirectoryWatcher.DirectoryEvent> events()
- Returns:
- The
Subject
of signalledDirectoryWatcher.DirectoryEvent
s.
-
isCloseComplete
public final boolean isCloseComplete()
Returnstrue
if this watcher was shutdown, and the shutdown process is completed (i.e. the I/O thread is terminated).- Returns:
true
if this watcher's shutdown completed.
-
isClosed
public final boolean isClosed()
Returnstrue
if this watcher was shutdown, meaning that no elements will be posted to the errors and events streams.- Returns:
true
if this watcher was shutdown.
-
isWatched
public boolean isWatched(Path dir)
Return whether the givenPath
is watched or not. A path is watched ifwatch(Path)
orwatchOrStreamError(Path)
was invoked on it.- Parameters:
dir
- ThePath
to be checked.- Returns:
true
ifwatch(Path)
orwatchOrStreamError(Path)
was invoked on the givendir
.
-
readBinaryFile
public void readBinaryFile(Path file, Consumer<byte[]> onSuccess, Consumer<Throwable> onError)
Reads the contents of a binary file. One of the two givenConsumer
s will be called on success or on failure.Files.readAllBytes(java.nio.file.Path)
will be called to actually read the file.Note: the operation is executed by the
Executor
passed to thebuild(java.util.concurrent.Executor)
method, i.e. in a different thread from the caller's one.
-
readTextFile
public void readTextFile(Path file, Charset charset, Consumer<String> onSuccess, Consumer<Throwable> onError)
Reads the contents of a text file. One of the two givenConsumer
s will be called on success or on failure.Files.readAllBytes(java.nio.file.Path)
will be called to actually read the file.Note: the operation is executed by the
Executor
passed to thebuild(java.util.concurrent.Executor)
method, i.e. in a different thread from the caller's one.
-
unwatch
public void unwatch(Path dir)
Unwatch the given directoryPath
.- Parameters:
dir
- The directory to be unwatched.
-
unwatchUp
public void unwatchUp(Path dir)
Unwatch the given directory and all its parents.- Parameters:
dir
- The directory to be watched.
-
unwatchUp
public void unwatchUp(Path dir, Path to)
Unwatch the given directory and all its parents up to the given ancestor.- Parameters:
dir
- The directory to be watched.to
- Thedir
's ancestorPath
(exclusive) where unwatching will be stopped. Can benull
.
-
watch
public void watch(Path dir) throws IOException
Watch the given directory for entry create, delete, and modify events.- Parameters:
dir
- The directory to be watched.- Throws:
IOException
- If an I/O error occurs.
-
watchOrStreamError
public void watchOrStreamError(Path dir)
Watch the given directory for entry create, delete, and modify events. If an I/O error occurs, the exception is logged (emitted).- Parameters:
dir
- The directory to be watched.
-
watchUp
public void watchUp(Path dir) throws IOException
Watch the given directory and all its parents for entry create, delete, and modify events.This method is equivalent to calling
watchUp(Path, Path)
passingnull
to its second parameter.- Parameters:
dir
- The directory to be watched.- Throws:
IOException
- If an I/O error occurs.
-
watchUp
public void watchUp(Path dir, Path to) throws IOException, IllegalArgumentException
Watch the given directory and all its parents up to the given ancestor, for entry create, delete, and modify events.- Parameters:
dir
- The directory to be watched.to
- Thedir
's ancestorPath
(exclusive) where watching will be stopped. Can benull
.- Throws:
IOException
- If an I/O error occurs.IllegalArgumentException
- Ifto
is not an ancestor ofdir
.
-
watchUpOrStreamError
public void watchUpOrStreamError(Path dir)
Watch the given directory and all its parents for entry create, delete, and modify events. If an I/O error occurs, the exception is logged (emitted).This method is equivalent to calling
watchUpOrStreamError(Path, Path)
passingnull
to its second parameter.- Parameters:
dir
- The directory to be watched.
-
watchUpOrStreamError
public void watchUpOrStreamError(Path dir, Path to)
Watch the given directory and all its parents up to the given ancestor, for entry create, delete, and modify events. If an I/O error occurs, the exception is logged (emitted).- Parameters:
dir
- The directory to be watched.to
- Thedir
's ancestorPath
(exclusive) where watching will be stopped. Can benull
.
-
writeBinaryFile
public void writeBinaryFile(Path file, byte[] content, Consumer<FileTime> onSuccess, Consumer<Throwable> onError)
Writes a binary file filling it with the givencontent
. One of the two givenConsumer
s will be called on success or on failure.Files.write(java.nio.file.Path, java.lang.Iterable, java.nio.file.OpenOption...)
will be called to actually write the file.Note: the operation is executed by the
Executor
passed to thebuild(java.util.concurrent.Executor)
method, i.e. in a different thread from the caller's one.
-
writeTextFile
public void writeTextFile(Path file, String content, Charset charset, Consumer<FileTime> onSuccess, Consumer<Throwable> onError)
Writes a text file filling it with the givencontent
. One of the two givenConsumer
s will be called on success or on failure.Files.write(java.nio.file.Path, java.lang.Iterable, java.nio.file.OpenOption...)
will be called to actually write the file.Note: the operation is executed by the
Executor
passed to thebuild(java.util.concurrent.Executor)
method, i.e. in a different thread from the caller's one.- Parameters:
file
- The pathname of the file to be written.content
- TheString
content to be written.charset
- TheCharset
to be used in writing thecontent
into thefile
.onSuccess
- TheConsumer
called on success, where the passed parameter is the written file timestamp.onError
- TheConsumer
called on failure.
-
-