aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/lwjgl/IOUtil.java
diff options
context:
space:
mode:
authorDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-05-03 18:25:32 +0200
committerDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-05-03 18:25:32 +0200
commita0ff501947a84b268e099524a06b56a6b900dad2 (patch)
treedb27ca1b28dbc7e57b8c99f54c80732d3042e856 /src/main/java/cc/polyfrost/oneconfig/lwjgl/IOUtil.java
parentb798930b21b89b81be05a31281f768667a6dd7f3 (diff)
downloadOneConfig-a0ff501947a84b268e099524a06b56a6b900dad2.tar.gz
OneConfig-a0ff501947a84b268e099524a06b56a6b900dad2.tar.bz2
OneConfig-a0ff501947a84b268e099524a06b56a6b900dad2.zip
move to cc.polyfrost
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/lwjgl/IOUtil.java')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/lwjgl/IOUtil.java56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/IOUtil.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/IOUtil.java
new file mode 100644
index 0000000..1394239
--- /dev/null
+++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/IOUtil.java
@@ -0,0 +1,56 @@
+package cc.polyfrost.oneconfig.lwjgl;
+
+import org.apache.commons.io.IOUtils;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.nio.Buffer;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.file.Files;
+
+public final class IOUtil {
+
+ private IOUtil() {
+ }
+
+ /**
+ * Taken from legui under MIT License
+ * <a href="https://github.com/SpinyOwl/legui/blob/develop/LICENSE">https://github.com/SpinyOwl/legui/blob/develop/LICENSE</a>
+ */
+ public static ByteBuffer resourceToByteBuffer(String path) throws IOException {
+ byte[] bytes;
+ path = path.trim();
+ if (path.startsWith("http")) {
+ bytes = IOUtils.toByteArray(new URL(path));
+ } else {
+ InputStream stream;
+ File file = new File(path);
+ if (file.exists() && file.isFile()) {
+ stream = Files.newInputStream(file.toPath());
+ } else {
+ stream = IOUtil.class.getResourceAsStream(path);
+ }
+ if (stream == null) {
+ throw new FileNotFoundException(path);
+ }
+ bytes = IOUtils.toByteArray(stream);
+ }
+ ByteBuffer data = ByteBuffer.allocateDirect(bytes.length).order(ByteOrder.nativeOrder())
+ .put(bytes);
+ ((Buffer) data).flip();
+ return data;
+ }
+
+ public static ByteBuffer resourceToByteBufferNullable(String path) {
+ try {
+ return resourceToByteBuffer(path);
+ } catch (Exception ignored) {
+ return null;
+ }
+ }
+
+} \ No newline at end of file