use of org.rx.io.FileStream in project rxlib by RockyLOMO.
the class YamlConfiguration method enableWatch.
public synchronized YamlConfiguration enableWatch(@NonNull String outputFile) {
if (watcher != null) {
throw new InvalidException("Already watched");
}
if (!yaml.isEmpty()) {
try (FileStream fs = new FileStream(outputFile)) {
fs.setPosition(0);
fs.writeString(new Yaml().dumpAsMap(yaml));
fs.flip();
}
}
watcher = new FileWatcher(Files.getFullPath(this.outputFile = outputFile), p -> p.toString().equals(this.outputFile));
watcher.onChanged.combine((s, e) -> {
String filePath = e.getPath().toString();
log.info("Config changing {} {} -> {}", e.isCreate(), filePath, yaml);
synchronized (this) {
yaml.clear();
if (!e.isDelete()) {
write(filePath);
}
}
log.info("Config changed {} {} -> {}", e.isCreate(), filePath, yaml);
raiseEvent(onChanged, new ChangedEventArgs(filePath));
});
return this;
}
use of org.rx.io.FileStream in project rxlib by RockyLOMO.
the class YamlConfiguration method loadYaml.
public static Map<String, Object> loadYaml(String... fileNames) {
return loadYaml(NQuery.of(fileNames).selectMany(p -> {
File file = new File(p);
if (file.exists()) {
return Arrays.toList(new FileStream(file).getReader());
}
NQuery<InputStream> resources = Reflects.getResources(p);
if (resources.any()) {
return resources.reverse();
}
return resources;
}).toList());
}
use of org.rx.io.FileStream in project rxlib by RockyLOMO.
the class MD5Util method md5.
@SneakyThrows
public static byte[] md5(File file) {
try (FileStream fs = new FileStream(file)) {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buf = Bytes.arrayBuffer();
int read;
while ((read = fs.read(buf)) > 0) {
md.update(buf, 0, read);
}
return md.digest();
}
}
Aggregations