Search in sources :

Example 1 with FileStream

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;
}
Also used : java.util(java.util) Getter(lombok.Getter) ApplicationException(org.rx.exception.ApplicationException) NonNull(lombok.NonNull) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Extends.values(org.rx.core.Extends.values) InvalidException(org.rx.exception.InvalidException) Extends.as(org.rx.core.Extends.as) CollectionUtils(org.apache.commons.collections4.CollectionUtils) File(java.io.File) Yaml(org.yaml.snakeyaml.Yaml) Slf4j(lombok.extern.slf4j.Slf4j) FileWatcher(org.rx.io.FileWatcher) Files(org.rx.io.Files) JSONObject(com.alibaba.fastjson.JSONObject) FileStream(org.rx.io.FileStream) ErrorCode(org.rx.annotation.ErrorCode) InputStream(java.io.InputStream) FileWatcher(org.rx.io.FileWatcher) InvalidException(org.rx.exception.InvalidException) FileStream(org.rx.io.FileStream) Yaml(org.yaml.snakeyaml.Yaml)

Example 2 with FileStream

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());
}
Also used : InputStream(java.io.InputStream) FileStream(org.rx.io.FileStream) File(java.io.File)

Example 3 with FileStream

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();
    }
}
Also used : FileStream(org.rx.io.FileStream) MessageDigest(java.security.MessageDigest) SneakyThrows(lombok.SneakyThrows)

Aggregations

FileStream (org.rx.io.FileStream)3 File (java.io.File)2 InputStream (java.io.InputStream)2 JSONObject (com.alibaba.fastjson.JSONObject)1 MessageDigest (java.security.MessageDigest)1 java.util (java.util)1 Getter (lombok.Getter)1 NonNull (lombok.NonNull)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 SneakyThrows (lombok.SneakyThrows)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CollectionUtils (org.apache.commons.collections4.CollectionUtils)1 ErrorCode (org.rx.annotation.ErrorCode)1 Extends.as (org.rx.core.Extends.as)1 Extends.values (org.rx.core.Extends.values)1 ApplicationException (org.rx.exception.ApplicationException)1 InvalidException (org.rx.exception.InvalidException)1 FileWatcher (org.rx.io.FileWatcher)1 Files (org.rx.io.Files)1 Yaml (org.yaml.snakeyaml.Yaml)1