diff --git a/scx-common/src/main/java/cool/scx/common/util/FileWatcher.java b/scx-common/src/main/java/cool/scx/common/util/FileWatcher.java deleted file mode 100644 index 9737d785..00000000 --- a/scx-common/src/main/java/cool/scx/common/util/FileWatcher.java +++ /dev/null @@ -1,106 +0,0 @@ -package cool.scx.common.util; - -import java.io.IOException; -import java.nio.file.FileSystems; -import java.nio.file.Path; -import java.nio.file.WatchKey; -import java.nio.file.WatchService; - -import static java.nio.file.StandardWatchEventKinds.*; - -/** - * 文件监听器 (只监听单个文件) - */ -public final class FileWatcher { - - private final WatchService watchService; - private final Path fileName; - private Thread watchThread; - private Runnable deleteHandler; - private Runnable createHandler; - private Runnable modifyHandler; - - public FileWatcher(Path path) throws IOException { - this.watchService = FileSystems.getDefault().newWatchService(); - this.fileName = path.getFileName(); - var fileParent = path.getParent(); - fileParent.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); - } - - public void _do() { - while (true) { - WatchKey watchKey; - try { - watchKey = watchService.take(); - } catch (InterruptedException e) { - //中断则退出 - break; - } - var watchEvents = watchKey.pollEvents(); - for (var event : watchEvents) { - Path context = (Path) event.context(); - //只处理指定文件 - if (this.fileName.equals(context)) { - var kind = event.kind(); - if (kind == ENTRY_CREATE) { - _callOnCreate(); - } else if (kind == ENTRY_MODIFY) { - _callOnModify(); - } else if (kind == ENTRY_DELETE) { - _callOnDelete(); - } - } - } - // reset the key - var valid = watchKey.reset(); - if (!valid) { - break; - } - } - } - - public FileWatcher onCreate(Runnable createHandler) { - this.createHandler = createHandler; - return this; - } - - public FileWatcher onModify(Runnable modifyHandler) { - this.modifyHandler = modifyHandler; - return this; - } - - public FileWatcher onDelete(Runnable deleteHandler) { - this.deleteHandler = deleteHandler; - return this; - } - - private void _callOnCreate() { - if (this.createHandler != null) { - this.createHandler.run(); - } - } - - private void _callOnModify() { - if (this.modifyHandler != null) { - this.modifyHandler.run(); - } - } - - private void _callOnDelete() { - if (this.deleteHandler != null) { - this.deleteHandler.run(); - } - } - - public synchronized void start() { - this.watchThread = Thread.ofVirtual().start(this::_do); - } - - public synchronized void stop() { - if (this.watchThread != null) { - this.watchThread.interrupt(); - this.watchThread = null; - } - } - -} diff --git a/scx-core/src/test/java/cool/scx/core/test/TestModule.java b/scx-core/src/test/java/cool/scx/core/test/TestModule.java index 78c2a9c3..9dce6b37 100644 --- a/scx-core/src/test/java/cool/scx/core/test/TestModule.java +++ b/scx-core/src/test/java/cool/scx/core/test/TestModule.java @@ -1,9 +1,6 @@ package cool.scx.core.test; import cool.scx.common.util.*; -import cool.scx.common.zip.UnZipBuilder; -import cool.scx.common.zip.ZipBuilder; -import cool.scx.common.zip.ZipOptions; import cool.scx.core.Scx; import cool.scx.core.ScxContext; import cool.scx.core.ScxModule; @@ -20,6 +17,9 @@ import cool.scx.http.media.multi_part.MultiPart; import cool.scx.http.routing.handler.StaticHandler; import cool.scx.http.uri.ScxURI; +import cool.scx.io.zip.UnZipBuilder; +import cool.scx.io.zip.ZipBuilder; +import cool.scx.io.zip.ZipOptions; import cool.scx.jdbc.sql.SQL; import cool.scx.scheduling.ScxScheduling; import org.testng.annotations.BeforeTest; diff --git a/scx-core/src/test/java/cool/scx/core/test/website/WebSiteController.java b/scx-core/src/test/java/cool/scx/core/test/website/WebSiteController.java index f63ae853..e0fd5da8 100644 --- a/scx-core/src/test/java/cool/scx/core/test/website/WebSiteController.java +++ b/scx-core/src/test/java/cool/scx/core/test/website/WebSiteController.java @@ -3,7 +3,6 @@ import cool.scx.common.util.HashUtils; import cool.scx.common.util.RandomUtils; import cool.scx.common.util.ScxExceptionHelper; -import cool.scx.common.zip.ZipBuilder; import cool.scx.core.ScxContext; import cool.scx.core.test.car.CarService; import cool.scx.core.test.person.Person; @@ -14,6 +13,7 @@ import cool.scx.http.helidon.ScxHttpClientHelper; import cool.scx.http.media.multi_part.MultiPartPart; import cool.scx.http.routing.RoutingContext; +import cool.scx.io.zip.ZipBuilder; import cool.scx.web.ScxWeb; import cool.scx.web.annotation.FromQuery; import cool.scx.web.annotation.FromUpload; diff --git a/scx-io/src/main/java/cool/scx/io/file/FileAttributesReader.java b/scx-io/src/main/java/cool/scx/io/file/FileAttributesReader.java new file mode 100644 index 00000000..526f022f --- /dev/null +++ b/scx-io/src/main/java/cool/scx/io/file/FileAttributesReader.java @@ -0,0 +1,114 @@ +package cool.scx.io.file; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.FileTime; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 通过 FileWatcher 主动更新 代替高频的 Files.readAttributes 查询 + */ +public class FileAttributesReader { + + //为了解决 ConcurrentHashMap 无法存储空值的问题 + private static final NotExist NOT_EXIST = new NotExist(); + + private final Path target; + private final FileWatcher fileWatcher; + private final Map cache; + + public FileAttributesReader(Path target) throws IOException { + this.target = target; + this.fileWatcher = new FileWatcher(target).listener(this::onChange).start(); + this.cache = new ConcurrentHashMap<>(); + } + + private void onChange(FileWatcher.ChangeEvent event) { + try { + if (event.type() == FileWatcher.ChangeEventType.DELETED) { + cache.put(event.target(), NOT_EXIST); + } else { + cache.put(event.target(), Files.readAttributes(event.target(), BasicFileAttributes.class)); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * 这里为了性能考虑并没有在 方法内部判断 + * 但是请保证 参数 path 一定是 target 的子目录或者子文件 否则将无法正确判断文件变化 + * + * @param path path + * @return 文件内容 (可能为 null ) + * @throws IOException a + */ + public BasicFileAttributes get(Path path) throws IOException { + var f = cache.computeIfAbsent(path, c -> { + try { + return Files.readAttributes(c, BasicFileAttributes.class); + } catch (NoSuchFileException e) { + return NOT_EXIST; + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + if (f == NOT_EXIST) { + return null; + } + return f; + } + + private static class NotExist implements BasicFileAttributes { + + @Override + public FileTime lastModifiedTime() { + return null; + } + + @Override + public FileTime lastAccessTime() { + return null; + } + + @Override + public FileTime creationTime() { + return null; + } + + @Override + public boolean isRegularFile() { + return false; + } + + @Override + public boolean isDirectory() { + return false; + } + + @Override + public boolean isSymbolicLink() { + return false; + } + + @Override + public boolean isOther() { + return false; + } + + @Override + public long size() { + return 0; + } + + @Override + public Object fileKey() { + return null; + } + } + +} diff --git a/scx-io/src/main/java/cool/scx/io/file/FileWatcher.java b/scx-io/src/main/java/cool/scx/io/file/FileWatcher.java new file mode 100644 index 00000000..ccd1fa0e --- /dev/null +++ b/scx-io/src/main/java/cool/scx/io/file/FileWatcher.java @@ -0,0 +1,116 @@ +package cool.scx.io.file; + +import java.io.IOException; +import java.nio.file.*; +import java.util.function.Consumer; + +import static java.nio.file.StandardWatchEventKinds.*; + +/** + * 文件监听器 + */ +public final class FileWatcher { + + private final WatchService watchService; + private final Path target; + private final Path watchTarget; + private final boolean isFile; + private Thread watchThread; + private Consumer listener; + + public FileWatcher(Path target) throws IOException { + this.watchService = FileSystems.getDefault().newWatchService(); + this.target = target; + this.isFile = !Files.isDirectory(target); + this.watchTarget = isFile ? target.getParent() : target; + watchTarget.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); + } + + public void _do() { + while (true) { + WatchKey watchKey; + try { + watchKey = watchService.take(); + } catch (InterruptedException e) { + //中断则退出 + break; + } + var watchEvents = watchKey.pollEvents(); + for (var event : watchEvents) { + var eventPath = (Path) event.context(); + var eventKind = event.kind(); + + //使用全路径方便处理 + eventPath = watchTarget.resolve(eventPath); + + //如果监听的是单个文件 但是发生变化的并不是这个文件 我们跳过 + if (isFile && !target.equals(eventPath)) { + continue; + } + //调用监听 + _callListener(eventPath, eventKind); + } + // reset the key + var valid = watchKey.reset(); + if (!valid) { + break; + } + } + } + + public FileWatcher listener(Consumer listener) { + this.listener = listener; + return this; + } + + private void _callListener(Path target, WatchEvent.Kind kind) { + if (listener != null) { + this.listener.accept(new ChangeEvent(target, ChangeEventType.of(kind))); + } + } + + public FileWatcher start() { + this.watchThread = Thread.ofVirtual().name("FileWatcher-WatchThread").start(this::_do); + return this; + } + + public void stop() { + if (this.watchThread != null) { + this.watchThread.interrupt(); + this.watchThread = null; + } + try { + watchService.close(); + } catch (IOException _) { + + } + } + + public enum ChangeEventType { + + MODIFY, + + DELETED, + + CREATED; + + public static ChangeEventType of(WatchEvent.Kind t) { + if (t == ENTRY_CREATE) { + return CREATED; + } + if (t == ENTRY_MODIFY) { + return MODIFY; + } + if (t == ENTRY_DELETE) { + return DELETED; + } + throw new IllegalArgumentException("未知类型 : " + t.toString()); + } + + } + + public record ChangeEvent(Path target, ChangeEventType type) { + + } + +} diff --git a/scx-common/src/main/java/cool/scx/common/util/image/ProgressiveJPEGBuilder.java b/scx-io/src/main/java/cool/scx/io/image/ProgressiveJPEGBuilder.java similarity index 90% rename from scx-common/src/main/java/cool/scx/common/util/image/ProgressiveJPEGBuilder.java rename to scx-io/src/main/java/cool/scx/io/image/ProgressiveJPEGBuilder.java index d70b9f6d..c7ce4d02 100644 --- a/scx-common/src/main/java/cool/scx/common/util/image/ProgressiveJPEGBuilder.java +++ b/scx-io/src/main/java/cool/scx/io/image/ProgressiveJPEGBuilder.java @@ -1,7 +1,7 @@ -package cool.scx.common.util.image; +package cool.scx.io.image; -import cool.scx.common.io_stream_source.InputStreamSource; -import cool.scx.common.io_stream_source.OutputStreamSource; +import cool.scx.io.io_stream_source.InputStreamSource; +import cool.scx.io.io_stream_source.OutputStreamSource; import javax.imageio.IIOImage; import javax.imageio.ImageIO; @@ -15,7 +15,7 @@ import java.nio.file.Path; import java.util.function.Supplier; -import static cool.scx.common.io_stream_source.InputStreamSource.of; +import static cool.scx.io.io_stream_source.InputStreamSource.of; /** * 将图片转换为 渐进式 JPEG diff --git a/scx-common/src/main/java/cool/scx/common/io_stream_source/BytesSource.java b/scx-io/src/main/java/cool/scx/io/io_stream_source/BytesSource.java similarity index 95% rename from scx-common/src/main/java/cool/scx/common/io_stream_source/BytesSource.java rename to scx-io/src/main/java/cool/scx/io/io_stream_source/BytesSource.java index 6e3e06a6..e5959b47 100644 --- a/scx-common/src/main/java/cool/scx/common/io_stream_source/BytesSource.java +++ b/scx-io/src/main/java/cool/scx/io/io_stream_source/BytesSource.java @@ -1,4 +1,4 @@ -package cool.scx.common.io_stream_source; +package cool.scx.io.io_stream_source; import java.io.ByteArrayInputStream; import java.io.IOException; diff --git a/scx-common/src/main/java/cool/scx/common/io_stream_source/BytesSupplierSource.java b/scx-io/src/main/java/cool/scx/io/io_stream_source/BytesSupplierSource.java similarity index 96% rename from scx-common/src/main/java/cool/scx/common/io_stream_source/BytesSupplierSource.java rename to scx-io/src/main/java/cool/scx/io/io_stream_source/BytesSupplierSource.java index 172c7985..bccb6f13 100644 --- a/scx-common/src/main/java/cool/scx/common/io_stream_source/BytesSupplierSource.java +++ b/scx-io/src/main/java/cool/scx/io/io_stream_source/BytesSupplierSource.java @@ -1,4 +1,4 @@ -package cool.scx.common.io_stream_source; +package cool.scx.io.io_stream_source; import java.io.ByteArrayInputStream; import java.io.IOException; diff --git a/scx-common/src/main/java/cool/scx/common/io_stream_source/InputStreamSource.java b/scx-io/src/main/java/cool/scx/io/io_stream_source/InputStreamSource.java similarity index 97% rename from scx-common/src/main/java/cool/scx/common/io_stream_source/InputStreamSource.java rename to scx-io/src/main/java/cool/scx/io/io_stream_source/InputStreamSource.java index 600e6338..551e451b 100644 --- a/scx-common/src/main/java/cool/scx/common/io_stream_source/InputStreamSource.java +++ b/scx-io/src/main/java/cool/scx/io/io_stream_source/InputStreamSource.java @@ -1,4 +1,4 @@ -package cool.scx.common.io_stream_source; +package cool.scx.io.io_stream_source; import java.io.IOException; import java.io.InputStream; diff --git a/scx-common/src/main/java/cool/scx/common/io_stream_source/NullSource.java b/scx-io/src/main/java/cool/scx/io/io_stream_source/NullSource.java similarity index 94% rename from scx-common/src/main/java/cool/scx/common/io_stream_source/NullSource.java rename to scx-io/src/main/java/cool/scx/io/io_stream_source/NullSource.java index 1375b588..fa35f432 100644 --- a/scx-common/src/main/java/cool/scx/common/io_stream_source/NullSource.java +++ b/scx-io/src/main/java/cool/scx/io/io_stream_source/NullSource.java @@ -1,4 +1,4 @@ -package cool.scx.common.io_stream_source; +package cool.scx.io.io_stream_source; import java.io.IOException; import java.io.InputStream; diff --git a/scx-common/src/main/java/cool/scx/common/io_stream_source/OutputStreamSource.java b/scx-io/src/main/java/cool/scx/io/io_stream_source/OutputStreamSource.java similarity index 95% rename from scx-common/src/main/java/cool/scx/common/io_stream_source/OutputStreamSource.java rename to scx-io/src/main/java/cool/scx/io/io_stream_source/OutputStreamSource.java index 4ecee268..6d963b1f 100644 --- a/scx-common/src/main/java/cool/scx/common/io_stream_source/OutputStreamSource.java +++ b/scx-io/src/main/java/cool/scx/io/io_stream_source/OutputStreamSource.java @@ -1,4 +1,4 @@ -package cool.scx.common.io_stream_source; +package cool.scx.io.io_stream_source; import java.io.ByteArrayOutputStream; import java.io.IOException; diff --git a/scx-common/src/main/java/cool/scx/common/io_stream_source/PathSource.java b/scx-io/src/main/java/cool/scx/io/io_stream_source/PathSource.java similarity index 95% rename from scx-common/src/main/java/cool/scx/common/io_stream_source/PathSource.java rename to scx-io/src/main/java/cool/scx/io/io_stream_source/PathSource.java index 44021453..3c9cd341 100644 --- a/scx-common/src/main/java/cool/scx/common/io_stream_source/PathSource.java +++ b/scx-io/src/main/java/cool/scx/io/io_stream_source/PathSource.java @@ -1,4 +1,4 @@ -package cool.scx.common.io_stream_source; +package cool.scx.io.io_stream_source; import java.io.IOException; import java.io.InputStream; diff --git a/scx-common/src/main/java/cool/scx/common/io_stream_source/RawInputStreamSource.java b/scx-io/src/main/java/cool/scx/io/io_stream_source/RawInputStreamSource.java similarity index 93% rename from scx-common/src/main/java/cool/scx/common/io_stream_source/RawInputStreamSource.java rename to scx-io/src/main/java/cool/scx/io/io_stream_source/RawInputStreamSource.java index 4afe9bda..a18ad534 100644 --- a/scx-common/src/main/java/cool/scx/common/io_stream_source/RawInputStreamSource.java +++ b/scx-io/src/main/java/cool/scx/io/io_stream_source/RawInputStreamSource.java @@ -1,4 +1,4 @@ -package cool.scx.common.io_stream_source; +package cool.scx.io.io_stream_source; import java.io.IOException; import java.io.InputStream; diff --git a/scx-common/src/main/java/cool/scx/common/io_stream_source/ZipEntrySource.java b/scx-io/src/main/java/cool/scx/io/io_stream_source/ZipEntrySource.java similarity index 92% rename from scx-common/src/main/java/cool/scx/common/io_stream_source/ZipEntrySource.java rename to scx-io/src/main/java/cool/scx/io/io_stream_source/ZipEntrySource.java index ff8352bd..ab808855 100644 --- a/scx-common/src/main/java/cool/scx/common/io_stream_source/ZipEntrySource.java +++ b/scx-io/src/main/java/cool/scx/io/io_stream_source/ZipEntrySource.java @@ -1,4 +1,4 @@ -package cool.scx.common.io_stream_source; +package cool.scx.io.io_stream_source; import java.io.IOException; import java.io.InputStream; diff --git a/scx-common/src/main/java/cool/scx/common/zip/GunzipBuilder.java b/scx-io/src/main/java/cool/scx/io/zip/GunzipBuilder.java similarity index 85% rename from scx-common/src/main/java/cool/scx/common/zip/GunzipBuilder.java rename to scx-io/src/main/java/cool/scx/io/zip/GunzipBuilder.java index 9b7389c4..66f6f08f 100644 --- a/scx-common/src/main/java/cool/scx/common/zip/GunzipBuilder.java +++ b/scx-io/src/main/java/cool/scx/io/zip/GunzipBuilder.java @@ -1,6 +1,6 @@ -package cool.scx.common.zip; +package cool.scx.io.zip; -import cool.scx.common.io_stream_source.InputStreamSource; +import cool.scx.io.io_stream_source.InputStreamSource; import java.io.IOException; import java.io.InputStream; @@ -8,7 +8,7 @@ import java.util.function.Supplier; import java.util.zip.GZIPInputStream; -import static cool.scx.common.io_stream_source.InputStreamSource.of; +import static cool.scx.io.io_stream_source.InputStreamSource.of; /** * GunzipBuilder diff --git a/scx-common/src/main/java/cool/scx/common/zip/GzipBuilder.java b/scx-io/src/main/java/cool/scx/io/zip/GzipBuilder.java similarity index 87% rename from scx-common/src/main/java/cool/scx/common/zip/GzipBuilder.java rename to scx-io/src/main/java/cool/scx/io/zip/GzipBuilder.java index 47b0819f..98f85230 100644 --- a/scx-common/src/main/java/cool/scx/common/zip/GzipBuilder.java +++ b/scx-io/src/main/java/cool/scx/io/zip/GzipBuilder.java @@ -1,7 +1,7 @@ -package cool.scx.common.zip; +package cool.scx.io.zip; -import cool.scx.common.io_stream_source.InputStreamSource; -import cool.scx.common.io_stream_source.OutputStreamSource; +import cool.scx.io.io_stream_source.InputStreamSource; +import cool.scx.io.io_stream_source.OutputStreamSource; import java.io.IOException; import java.io.InputStream; diff --git a/scx-common/src/main/java/cool/scx/common/zip/PathZipBuilderItem.java b/scx-io/src/main/java/cool/scx/io/zip/PathZipBuilderItem.java similarity index 96% rename from scx-common/src/main/java/cool/scx/common/zip/PathZipBuilderItem.java rename to scx-io/src/main/java/cool/scx/io/zip/PathZipBuilderItem.java index eb1526b0..6b617315 100644 --- a/scx-common/src/main/java/cool/scx/common/zip/PathZipBuilderItem.java +++ b/scx-io/src/main/java/cool/scx/io/zip/PathZipBuilderItem.java @@ -1,8 +1,8 @@ -package cool.scx.common.zip; +package cool.scx.io.zip; -import cool.scx.common.io_stream_source.InputStreamSource; import cool.scx.common.util.StringUtils; import cool.scx.common.util.URIBuilder; +import cool.scx.io.io_stream_source.InputStreamSource; import java.io.IOException; import java.nio.file.FileVisitResult; diff --git a/scx-common/src/main/java/cool/scx/common/zip/UnZipBuilder.java b/scx-io/src/main/java/cool/scx/io/zip/UnZipBuilder.java similarity index 96% rename from scx-common/src/main/java/cool/scx/common/zip/UnZipBuilder.java rename to scx-io/src/main/java/cool/scx/io/zip/UnZipBuilder.java index 3ab191c9..226e6888 100644 --- a/scx-common/src/main/java/cool/scx/common/zip/UnZipBuilder.java +++ b/scx-io/src/main/java/cool/scx/io/zip/UnZipBuilder.java @@ -1,8 +1,8 @@ -package cool.scx.common.zip; +package cool.scx.io.zip; -import cool.scx.common.io_stream_source.InputStreamSource; import cool.scx.common.util.FileUtils; import cool.scx.common.util.URIBuilder; +import cool.scx.io.io_stream_source.InputStreamSource; import java.io.IOException; import java.io.InputStream; diff --git a/scx-common/src/main/java/cool/scx/common/zip/ZipBuilder.java b/scx-io/src/main/java/cool/scx/io/zip/ZipBuilder.java similarity index 99% rename from scx-common/src/main/java/cool/scx/common/zip/ZipBuilder.java rename to scx-io/src/main/java/cool/scx/io/zip/ZipBuilder.java index a99c4375..7483bedf 100644 --- a/scx-common/src/main/java/cool/scx/common/zip/ZipBuilder.java +++ b/scx-io/src/main/java/cool/scx/io/zip/ZipBuilder.java @@ -1,4 +1,4 @@ -package cool.scx.common.zip; +package cool.scx.io.zip; import cool.scx.common.util.StringUtils; import cool.scx.common.util.URIBuilder; diff --git a/scx-common/src/main/java/cool/scx/common/zip/ZipBuilderItem.java b/scx-io/src/main/java/cool/scx/io/zip/ZipBuilderItem.java similarity index 95% rename from scx-common/src/main/java/cool/scx/common/zip/ZipBuilderItem.java rename to scx-io/src/main/java/cool/scx/io/zip/ZipBuilderItem.java index 59f56f03..76b294d2 100644 --- a/scx-common/src/main/java/cool/scx/common/zip/ZipBuilderItem.java +++ b/scx-io/src/main/java/cool/scx/io/zip/ZipBuilderItem.java @@ -1,7 +1,7 @@ -package cool.scx.common.zip; +package cool.scx.io.zip; -import cool.scx.common.io_stream_source.InputStreamSource; import cool.scx.common.util.URIBuilder; +import cool.scx.io.io_stream_source.InputStreamSource; import java.io.IOException; import java.io.InputStream; diff --git a/scx-common/src/main/java/cool/scx/common/zip/ZipOptions.java b/scx-io/src/main/java/cool/scx/io/zip/ZipOptions.java similarity index 98% rename from scx-common/src/main/java/cool/scx/common/zip/ZipOptions.java rename to scx-io/src/main/java/cool/scx/io/zip/ZipOptions.java index 9c397bd2..8ca841f3 100644 --- a/scx-common/src/main/java/cool/scx/common/zip/ZipOptions.java +++ b/scx-io/src/main/java/cool/scx/io/zip/ZipOptions.java @@ -1,4 +1,4 @@ -package cool.scx.common.zip; +package cool.scx.io.zip; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; diff --git a/scx-io/src/test/java/cool/scx/io/test/FileAttributesReaderTest.java b/scx-io/src/test/java/cool/scx/io/test/FileAttributesReaderTest.java new file mode 100644 index 00000000..3b25006d --- /dev/null +++ b/scx-io/src/test/java/cool/scx/io/test/FileAttributesReaderTest.java @@ -0,0 +1,29 @@ +package cool.scx.io.test; + +import cool.scx.io.file.FileAttributesReader; + +import java.io.IOException; +import java.nio.file.Path; + +public class FileAttributesReaderTest { + + public static void main(String[] args) throws IOException { + test1(); + } + + public static void test1() throws IOException { + var s = new FileAttributesReader(Path.of("xxxx")); + var p = Path.of("xxxx/xxxx"); + var l = System.nanoTime(); + for (int i = 0; i < 99999; i++) { + var basicFileAttributes = s.get(p); + if (basicFileAttributes != null) { + System.out.println(basicFileAttributes.size()); + } else { + System.out.println("未找到"); + } + } + System.out.println((System.nanoTime() - l) / 1000_000); + } + +} diff --git a/scx-common/src/test/java/cool/scx/common/util/test/FileWatcherTest.java b/scx-io/src/test/java/cool/scx/io/test/FileWatcherTest.java similarity index 56% rename from scx-common/src/test/java/cool/scx/common/util/test/FileWatcherTest.java rename to scx-io/src/test/java/cool/scx/io/test/FileWatcherTest.java index fee6bda9..3ea0eb92 100644 --- a/scx-common/src/test/java/cool/scx/common/util/test/FileWatcherTest.java +++ b/scx-io/src/test/java/cool/scx/io/test/FileWatcherTest.java @@ -1,7 +1,7 @@ -package cool.scx.common.util.test; +package cool.scx.io.test; import cool.scx.common.util.ClassUtils; -import cool.scx.common.util.FileWatcher; +import cool.scx.io.file.FileWatcher; import org.testng.annotations.Test; import java.io.IOException; @@ -16,16 +16,8 @@ public static void main(String[] args) throws IOException { public static void test1() throws IOException { var w = new FileWatcher(ClassUtils.getAppRoot(FileWatcherTest.class)); - w.onCreate(() -> { - System.out.println("创建了"); - }); - - w.onModify(() -> { - System.out.println("修改了"); - }); - - w.onDelete(() -> { - System.out.println("删除了"); + w.listener(c -> { + System.out.println(c.type() + " -> " + c.target()); }); w.start(); diff --git a/scx-common/src/test/java/cool/scx/common/zip/test/ZipTest.java b/scx-io/src/test/java/cool/scx/io/test/ZipTest.java similarity index 87% rename from scx-common/src/test/java/cool/scx/common/zip/test/ZipTest.java rename to scx-io/src/test/java/cool/scx/io/test/ZipTest.java index f152b9dd..cacd0161 100644 --- a/scx-common/src/test/java/cool/scx/common/zip/test/ZipTest.java +++ b/scx-io/src/test/java/cool/scx/io/test/ZipTest.java @@ -1,8 +1,8 @@ -package cool.scx.common.zip.test; +package cool.scx.io.test; -import cool.scx.common.zip.GunzipBuilder; -import cool.scx.common.zip.GzipBuilder; -import cool.scx.common.zip.ZipBuilder; +import cool.scx.io.zip.GunzipBuilder; +import cool.scx.io.zip.GzipBuilder; +import cool.scx.io.zip.ZipBuilder; import org.testng.Assert; import org.testng.annotations.Test;