Skip to content

Commit be2b672

Browse files
committed
add resource entry util
1 parent 3e40ee1 commit be2b672

File tree

5 files changed

+178
-19
lines changed

5 files changed

+178
-19
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package dev.felnull.fnjl.io.resource;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
8+
/**
9+
* リソースデータの一覧のエントリ
10+
*
11+
* @author MORIMORI0317
12+
* @since 1.55
13+
*/
14+
public interface ResourceEntry {
15+
/**
16+
* 名前取得
17+
*
18+
* @return 名前
19+
*/
20+
@NotNull
21+
String getName();
22+
23+
/**
24+
* Jarファイル内のリソースかどうか
25+
*
26+
* @return jarファイル内かどうか
27+
*/
28+
default boolean isInJar() {
29+
return "jar".equals(getScheme());
30+
}
31+
32+
@NotNull
33+
String getScheme();
34+
35+
/**
36+
* ディレクトリかどうか取得
37+
*
38+
* @return ディレクトリかどうか
39+
*/
40+
boolean isDirectory();
41+
42+
/**
43+
* リソースのストリームを取得
44+
*
45+
* @return InputStream
46+
*/
47+
InputStream openInputStream() throws IOException;
48+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package dev.felnull.fnjl.io.resource.impl;
2+
3+
import dev.felnull.fnjl.io.resource.ResourceEntry;
4+
import dev.felnull.fnjl.util.FNDataUtil;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import java.io.InputStream;
8+
import java.util.Objects;
9+
10+
public class ResourceEntryImpl implements ResourceEntry {
11+
private final String name;
12+
private final boolean directory;
13+
private final String scheme;
14+
private final String path;
15+
private final Class<?> clazz;
16+
17+
public ResourceEntryImpl(String name, boolean directory, String scheme, String path, Class<?> clazz) {
18+
this.name = name;
19+
this.directory = directory;
20+
this.scheme = scheme;
21+
this.path = path;
22+
this.clazz = clazz;
23+
}
24+
25+
@Override
26+
public @NotNull String getName() {
27+
return name;
28+
}
29+
30+
@Override
31+
public @NotNull String getScheme() {
32+
return scheme;
33+
}
34+
35+
@Override
36+
public boolean isDirectory() {
37+
return directory;
38+
}
39+
40+
@Override
41+
public InputStream openInputStream() {
42+
if(isDirectory())
43+
throw new RuntimeException("directory");
44+
45+
return FNDataUtil.resourceExtractor(clazz, path);
46+
}
47+
48+
@Override
49+
public boolean equals(Object o) {
50+
if (this == o) return true;
51+
if (o == null || getClass() != o.getClass()) return false;
52+
ResourceEntryImpl that = (ResourceEntryImpl) o;
53+
return directory == that.directory && Objects.equals(name, that.name) && Objects.equals(scheme, that.scheme) && Objects.equals(path, that.path) && Objects.equals(clazz, that.clazz);
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
return Objects.hash(name, directory, scheme, path, clazz);
59+
}
60+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@ApiStatus.Internal
2+
package dev.felnull.fnjl.io.resource.impl;
3+
4+
import org.jetbrains.annotations.ApiStatus;
5+

common/src/main/java/dev/felnull/fnjl/util/FNDataUtil.java

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@
22

33
import dev.felnull.fnjl.io.FileWatcher;
44
import dev.felnull.fnjl.io.ProgressWriter;
5+
import dev.felnull.fnjl.io.resource.ResourceEntry;
6+
import dev.felnull.fnjl.io.resource.impl.ResourceEntryImpl;
57
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
69

710
import java.io.*;
811
import java.net.HttpURLConnection;
12+
import java.net.URI;
13+
import java.net.URISyntaxException;
914
import java.net.URL;
1015
import java.nio.charset.Charset;
1116
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.*;
1519
import java.security.MessageDigest;
1620
import java.security.NoSuchAlgorithmException;
17-
import java.util.HashMap;
18-
import java.util.Map;
21+
import java.util.*;
1922
import java.util.concurrent.atomic.AtomicInteger;
2023
import java.util.function.BiConsumer;
2124
import java.util.function.Consumer;
2225
import java.util.function.Function;
26+
import java.util.stream.Stream;
2327
import java.util.zip.GZIPInputStream;
2428
import java.util.zip.GZIPOutputStream;
2529
import java.util.zip.ZipEntry;
@@ -70,8 +74,7 @@ public static String readAllString(Reader reader) throws IOException {
7074
try (BufferedReader breader = new BufferedReader(reader)) {
7175
String next;
7276
while ((next = breader.readLine()) != null) {
73-
if (flg)
74-
sb.append('\n');
77+
if (flg) sb.append('\n');
7578
sb.append(next);
7679
flg = true;
7780
}
@@ -181,8 +184,7 @@ public static byte[] createMD5Hash(byte[] data) throws NoSuchAlgorithmException
181184
* @throws IOException 例外
182185
*/
183186
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");
186188
FileOutputStream fos = new FileOutputStream(file);
187189
BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
188190
ProgressWriter writer = new ProgressWriter(stream, length, data -> {
@@ -234,8 +236,7 @@ public static void fileCopyToProgress(File copyFile, File file, Consumer<Progres
234236
* @throws IOException 例外
235237
*/
236238
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");
239240
byte[] bytes = new byte[Math.toIntExact(length)];
240241
AtomicInteger cont = new AtomicInteger();
241242
ProgressWriter writer = new ProgressWriter(stream, length, data -> {
@@ -277,17 +278,16 @@ public static byte[] fileLoadToProgress(File file, Consumer<ProgressWriter.Write
277278
/**
278279
* リソースフォルダからデータを抽出
279280
*
280-
* @param targetClass リソースフォルダのクラス
281-
* @param path リソースパス
281+
* @param clazz リソースフォルダのクラス
282+
* @param path リソースパス
282283
* @return InputStream
283284
*/
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);
287288

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);
291291
return stream != null ? new BufferedInputStream(stream) : null;
292292
}
293293

@@ -412,4 +412,49 @@ public M apply(T t) {
412412
}
413413
};
414414
}
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+
415460
}

common/src/test/java/dev/felnull/fnjltest/Main.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
public class Main {
44
public static void main(String[] args) throws Exception {
5+
56
}
67
}

0 commit comments

Comments
 (0)