113 lines
3.0 KiB
Java
Raw Normal View History

2022-04-17 05:43:07 -07:00
package emu.grasscutter.utils;
2022-04-18 01:11:27 -04:00
import emu.grasscutter.Grasscutter;
2022-04-17 05:43:07 -07:00
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
2022-05-19 00:10:02 -07:00
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
2022-04-17 05:43:07 -07:00
2022-04-18 01:11:27 -04:00
public final class FileUtils {
2022-04-17 05:43:07 -07:00
public static void write(String dest, byte[] bytes) {
Path path = Paths.get(dest);
try {
Files.write(path, bytes);
} catch (IOException e) {
2022-04-18 01:11:27 -04:00
Grasscutter.getLogger().warn("Failed to write file: " + dest);
2022-04-17 05:43:07 -07:00
}
}
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) {
2022-04-18 01:11:27 -04:00
Grasscutter.getLogger().warn("Failed to read file: " + path);
2022-04-17 05:43:07 -07:00
}
return new byte[0];
}
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];
}
2022-04-17 05:43:07 -07:00
public static byte[] read(File file) {
return read(file.getPath());
}
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);
}
}
2022-04-17 05:43:07 -07:00
public static String getFilenameWithoutPath(String fileName) {
if (fileName.indexOf(".") > 0) {
return fileName.substring(0, fileName.lastIndexOf("."));
} else {
return fileName;
}
}
// From https://mkyong.com/java/java-read-a-file-from-resources-folder/
public static List<Path> getPathsFromResource(String folder) throws URISyntaxException, IOException {
2022-05-19 00:10:02 -07:00
List<Path> result = null;
// Get path of the current running JAR
String jarPath = Grasscutter.class.getProtectionDomain()
.getCodeSource()
.getLocation()
.toURI()
.getPath();
2022-05-19 00:10:02 -07:00
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();
}
2022-05-19 00:10:02 -07:00
return result;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static String readToString(InputStream file) throws IOException {
byte[] content = file.readAllBytes();
return new String(content, StandardCharsets.UTF_8);
}
2022-04-17 05:43:07 -07:00
}