diff options
author | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-05-04 18:36:37 +0100 |
---|---|---|
committer | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-05-04 18:36:37 +0100 |
commit | 77f8b7924ad61ebb8334d37a0a2ae2c8a297b400 (patch) | |
tree | 0f36b3509e4a2e20853996ce6e4a86b4fbc39c90 /src/main/java/cc/polyfrost/oneconfig/utils | |
parent | 74b42fa8a490b4226ca1a331fcddb22af149d217 (diff) | |
download | OneConfig-77f8b7924ad61ebb8334d37a0a2ae2c8a297b400.tar.gz OneConfig-77f8b7924ad61ebb8334d37a0a2ae2c8a297b400.tar.bz2 OneConfig-77f8b7924ad61ebb8334d37a0a2ae2c8a297b400.zip |
change some files and start on scrolling
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/utils')
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java new file mode 100644 index 0000000..6264451 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java @@ -0,0 +1,54 @@ +package cc.polyfrost.oneconfig.utils; + +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 IOUtils { + + private IOUtils() { + } + + /** + * 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 = org.apache.commons.io.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 = IOUtils.class.getResourceAsStream(path); + } + if (stream == null) { + throw new FileNotFoundException(path); + } + bytes = org.apache.commons.io.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 |