diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-04-18 18:11:17 +0200 |
---|---|---|
committer | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-04-18 18:11:17 +0200 |
commit | 01c2733db797fc1bb88cee627f792d05ab00f379 (patch) | |
tree | 6b448f04b05fc2c746f2ba542f8c8b0ebb987d8f /src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java | |
parent | 859f214759c688fcaaf97a2a02f933a28662c116 (diff) | |
parent | 2265233e317298db1da920430b8f8c21d6e3067e (diff) | |
download | OneConfig-01c2733db797fc1bb88cee627f792d05ab00f379.tar.gz OneConfig-01c2733db797fc1bb88cee627f792d05ab00f379.tar.bz2 OneConfig-01c2733db797fc1bb88cee627f792d05ab00f379.zip |
temporary commit so no more conflicts now stop touching me code :anrgydoggo:
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java')
-rw-r--r-- | src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java new file mode 100644 index 0000000..f5105fb --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java @@ -0,0 +1,53 @@ +package io.polyfrost.oneconfig.lwjgl; + +import org.apache.commons.io.IOUtils; + +import java.io.*; +import java.net.URL; +import java.nio.Buffer; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +public final class IOUtil { + + private IOUtil() { + } + + /** + * Taken from legui under MIT License + * https://github.com/SpinyOwl/legui/blob/develop/LICENSE + */ + @SuppressWarnings("RedundantCast") + 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 = new FileInputStream(file); + } 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 |