FileUtils.java 3.04 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.utils;

KingRainbow44's avatar
KingRainbow44 committed
3
4
import emu.grasscutter.Grasscutter;

Melledy's avatar
Melledy committed
5
6
import java.io.File;
import java.io.IOException;
7
8
9
10
11
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
Melledy's avatar
Melledy committed
12
import java.util.Arrays;
13
14
15
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Melledy's avatar
Melledy committed
16

KingRainbow44's avatar
KingRainbow44 committed
17
public final class FileUtils {
Melledy's avatar
Melledy committed
18
19
20
21
22
23
	public static void write(String dest, byte[] bytes) {
		Path path = Paths.get(dest);
		
		try {
			Files.write(path, bytes);
		} catch (IOException e) {
KingRainbow44's avatar
KingRainbow44 committed
24
			Grasscutter.getLogger().warn("Failed to write file: " + dest);
Melledy's avatar
Melledy committed
25
26
27
28
29
30
31
32
33
34
35
		}
	}
	
	public static byte[] read(String dest) {
		return read(Paths.get(dest));
	}

	public static byte[] read(Path path) {
		try {
			return Files.readAllBytes(path);
		} catch (IOException e) {
KingRainbow44's avatar
KingRainbow44 committed
36
			Grasscutter.getLogger().warn("Failed to read file: " + path);
Melledy's avatar
Melledy committed
37
38
39
40
		}
		
		return new byte[0];
	}
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

	public static InputStream readResourceAsStream(String resourcePath) {
		return Grasscutter.class.getResourceAsStream(resourcePath);
	}

	public static byte[] readResource(String resourcePath) {
		try (InputStream is = Grasscutter.class.getResourceAsStream(resourcePath)) {
			return is.readAllBytes();
		} catch (Exception exception) {
			Grasscutter.getLogger().warn("Failed to read resource: " + resourcePath);
			exception.printStackTrace();
		}

		return new byte[0];
	}
Melledy's avatar
Melledy committed
56
57
58
59
	
	public static byte[] read(File file) {
		return read(file.getPath());
	}
60
61
62
63
64
65
66
67
68

	public static void copyResource(String resourcePath, String destination) {
		try {
			byte[] resource = FileUtils.readResource(resourcePath);
			FileUtils.write(destination, resource);
		} catch (Exception exception) {
			Grasscutter.getLogger().warn("Failed to copy resource: " + resourcePath + "\n" + exception);
		}
	}
Melledy's avatar
Melledy committed
69
70
71
72
73
74
75
76
	
	public static String getFilenameWithoutPath(String fileName) {
		if (fileName.indexOf(".") > 0) {
		   return fileName.substring(0, fileName.lastIndexOf("."));
		} else {
		   return fileName;
		}
	}
77
78
79

	// From https://mkyong.com/java/java-read-a-file-from-resources-folder/
	public static List<Path> getPathsFromResource(String folder) throws URISyntaxException, IOException {
Melledy's avatar
Melledy committed
80
81
82
		List<Path> result = null;
		
		// Get path of the current running JAR
83
84
85
86
87
88
		String jarPath = Grasscutter.class.getProtectionDomain()
				.getCodeSource()
				.getLocation()
				.toURI()
				.getPath();

Melledy's avatar
Melledy committed
89
90
91
92
93
94
95
96
97
98
99
100
		try {
			// file walks JAR
			URI uri = URI.create("jar:file:" + jarPath);
			try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
				result = Files.walk(fs.getPath(folder))
						.filter(Files::isRegularFile)
						.collect(Collectors.toList());
			}
		} catch (Exception e) {
			// Eclipse puts resources in its bin folder
			File f = new File(jarPath + "defaults/data/");
			result = Arrays.stream(f.listFiles()).map(File::toPath).toList();
101
		}
Melledy's avatar
Melledy committed
102
		
103
104
105
106
107
108
109
110
111
		return result;
	}

	@SuppressWarnings("ResultOfMethodCallIgnored")
	public static String readToString(InputStream file) throws IOException {
		byte[] content = file.readAllBytes();

		return new String(content, StandardCharsets.UTF_8);
	}
Melledy's avatar
Melledy committed
112
}