|
2 | 2 |
|
3 | 3 | import dev.felnull.fnjl.io.FileWatcher;
|
4 | 4 | import dev.felnull.fnjl.io.ProgressWriter;
|
| 5 | +import dev.felnull.fnjl.io.resource.ResourceEntry; |
| 6 | +import dev.felnull.fnjl.io.resource.impl.ResourceEntryImpl; |
5 | 7 | import org.jetbrains.annotations.NotNull;
|
| 8 | +import org.jetbrains.annotations.Nullable; |
6 | 9 |
|
7 | 10 | import java.io.*;
|
8 | 11 | import java.net.HttpURLConnection;
|
| 12 | +import java.net.URI; |
| 13 | +import java.net.URISyntaxException; |
9 | 14 | import java.net.URL;
|
10 | 15 | import java.nio.charset.Charset;
|
11 | 16 | import java.nio.charset.StandardCharsets;
|
12 |
| -import java.nio.file.Files; |
13 |
| -import java.nio.file.Path; |
14 |
| -import java.nio.file.WatchEvent; |
| 17 | +import java.nio.file.FileSystem; |
| 18 | +import java.nio.file.*; |
15 | 19 | import java.security.MessageDigest;
|
16 | 20 | import java.security.NoSuchAlgorithmException;
|
17 |
| -import java.util.HashMap; |
18 |
| -import java.util.Map; |
| 21 | +import java.util.*; |
19 | 22 | import java.util.concurrent.atomic.AtomicInteger;
|
20 | 23 | import java.util.function.BiConsumer;
|
21 | 24 | import java.util.function.Consumer;
|
22 | 25 | import java.util.function.Function;
|
| 26 | +import java.util.stream.Stream; |
23 | 27 | import java.util.zip.GZIPInputStream;
|
24 | 28 | import java.util.zip.GZIPOutputStream;
|
25 | 29 | import java.util.zip.ZipEntry;
|
@@ -70,8 +74,7 @@ public static String readAllString(Reader reader) throws IOException {
|
70 | 74 | try (BufferedReader breader = new BufferedReader(reader)) {
|
71 | 75 | String next;
|
72 | 76 | while ((next = breader.readLine()) != null) {
|
73 |
| - if (flg) |
74 |
| - sb.append('\n'); |
| 77 | + if (flg) sb.append('\n'); |
75 | 78 | sb.append(next);
|
76 | 79 | flg = true;
|
77 | 80 | }
|
@@ -181,8 +184,7 @@ public static byte[] createMD5Hash(byte[] data) throws NoSuchAlgorithmException
|
181 | 184 | * @throws IOException 例外
|
182 | 185 | */
|
183 | 186 | public static void fileWriteToProgress(InputStream stream, long length, File file, Consumer<ProgressWriter.WriteProgressListener> progress) throws IOException {
|
184 |
| - if (length <= 0) |
185 |
| - throw new IOException("Invalid length"); |
| 187 | + if (length <= 0) throw new IOException("Invalid length"); |
186 | 188 | FileOutputStream fos = new FileOutputStream(file);
|
187 | 189 | BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
|
188 | 190 | ProgressWriter writer = new ProgressWriter(stream, length, data -> {
|
@@ -234,8 +236,7 @@ public static void fileCopyToProgress(File copyFile, File file, Consumer<Progres
|
234 | 236 | * @throws IOException 例外
|
235 | 237 | */
|
236 | 238 | public static byte[] loadToProgress(InputStream stream, long length, Consumer<ProgressWriter.WriteProgressListener> progress) throws IOException {
|
237 |
| - if (length <= 0) |
238 |
| - throw new IOException("Invalid length"); |
| 239 | + if (length <= 0) throw new IOException("Invalid length"); |
239 | 240 | byte[] bytes = new byte[Math.toIntExact(length)];
|
240 | 241 | AtomicInteger cont = new AtomicInteger();
|
241 | 242 | ProgressWriter writer = new ProgressWriter(stream, length, data -> {
|
@@ -277,17 +278,16 @@ public static byte[] fileLoadToProgress(File file, Consumer<ProgressWriter.Write
|
277 | 278 | /**
|
278 | 279 | * リソースフォルダからデータを抽出
|
279 | 280 | *
|
280 |
| - * @param targetClass リソースフォルダのクラス |
281 |
| - * @param path リソースパス |
| 281 | + * @param clazz リソースフォルダのクラス |
| 282 | + * @param path リソースパス |
282 | 283 | * @return InputStream
|
283 | 284 | */
|
284 |
| - public static InputStream resourceExtractor(Class<?> targetClass, String path) { |
285 |
| - if (path.startsWith("/")) |
286 |
| - path = path.substring(1); |
| 285 | + @Nullable |
| 286 | + public static InputStream resourceExtractor(@NotNull Class<?> clazz, @NotNull String path) { |
| 287 | + if (path.startsWith("/")) path = path.substring(1); |
287 | 288 |
|
288 |
| - InputStream stream = targetClass.getResourceAsStream("/" + path); |
289 |
| - if (stream == null) |
290 |
| - stream = ClassLoader.getSystemResourceAsStream(path); |
| 289 | + InputStream stream = clazz.getResourceAsStream("/" + path); |
| 290 | + if (stream == null) stream = ClassLoader.getSystemResourceAsStream(path); |
291 | 291 | return stream != null ? new BufferedInputStream(stream) : null;
|
292 | 292 | }
|
293 | 293 |
|
@@ -412,4 +412,49 @@ public M apply(T t) {
|
412 | 412 | }
|
413 | 413 | };
|
414 | 414 | }
|
| 415 | + |
| 416 | + /** |
| 417 | + * リソースディレクトリ内のファイルの一覧を表示 |
| 418 | + * |
| 419 | + * @param clazz 対象パッケージクラス |
| 420 | + * @param path パス |
| 421 | + * @return エントリ |
| 422 | + */ |
| 423 | + @NotNull |
| 424 | + public static List<ResourceEntry> resourceExtractEntry(@NotNull Class<?> clazz, @NotNull String path) { |
| 425 | + if (!path.startsWith("/")) path = "/" + path; |
| 426 | + URL url = clazz.getResource(path); |
| 427 | + Path tp = Paths.get(path); |
| 428 | + |
| 429 | + if (url != null) { |
| 430 | + try { |
| 431 | + URI uri = url.toURI(); |
| 432 | + String scheme = uri.getScheme(); |
| 433 | + if ("jar".equals(uri.getScheme())) { |
| 434 | + try (FileSystem fs = FileSystems.newFileSystem(uri, new HashMap<>())) { |
| 435 | + return getResourceEntry(fs.getPath(path), tp, scheme, clazz); |
| 436 | + } |
| 437 | + } else { |
| 438 | + return getResourceEntry(Paths.get(uri), tp, scheme, clazz); |
| 439 | + } |
| 440 | + } catch (URISyntaxException | IOException e) { |
| 441 | + throw new RuntimeException(e); |
| 442 | + } |
| 443 | + } |
| 444 | + |
| 445 | + throw new RuntimeException("not exists file"); |
| 446 | + } |
| 447 | + |
| 448 | + private static List<ResourceEntry> getResourceEntry(Path path, Path targetPath, String scheme, Class<?> clazz) throws IOException { |
| 449 | + List<ResourceEntry> entries = new ArrayList<>(); |
| 450 | + try (Stream<Path> wlk = Files.walk(path, 1).filter(n -> !n.equals(path))) { |
| 451 | + wlk.forEach(p -> { |
| 452 | + String name = p.getName(p.getNameCount() - 1).toString(); |
| 453 | + ResourceEntry re = new ResourceEntryImpl(name, Files.isDirectory(p), scheme, targetPath.resolve(name).toString(), clazz); |
| 454 | + entries.add(re); |
| 455 | + }); |
| 456 | + } |
| 457 | + return Collections.unmodifiableList(entries); |
| 458 | + } |
| 459 | + |
415 | 460 | }
|
0 commit comments