FileUtils.java 2.8 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
12
13
14
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Melledy's avatar
Melledy committed
15

KingRainbow44's avatar
KingRainbow44 committed
16
public final class FileUtils {
Melledy's avatar
Melledy committed
17
18
19
20
21
22
	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
23
			Grasscutter.getLogger().warn("Failed to write file: " + dest);
Melledy's avatar
Melledy committed
24
25
26
27
28
29
30
31
32
33
34
		}
	}
	
	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
35
			Grasscutter.getLogger().warn("Failed to read file: " + path);
Melledy's avatar
Melledy committed
36
37
38
39
		}
		
		return new byte[0];
	}
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

	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
55
56
57
58
	
	public static byte[] read(File file) {
		return read(file.getPath());
	}
59
60
61
62
63
64
65
66
67

	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
68
69
70
71
72
73
74
75
	
	public static String getFilenameWithoutPath(String fileName) {
		if (fileName.indexOf(".") > 0) {
		   return fileName.substring(0, fileName.lastIndexOf("."));
		} else {
		   return fileName;
		}
	}
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

	// From https://mkyong.com/java/java-read-a-file-from-resources-folder/
	public static List<Path> getPathsFromResource(String folder) throws URISyntaxException, IOException {
		List<Path> result;

		// get path of the current running JAR
		String jarPath = Grasscutter.class.getProtectionDomain()
				.getCodeSource()
				.getLocation()
				.toURI()
				.getPath();

		// 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());
		}

		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
105
}