aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java
diff options
context:
space:
mode:
authorWyvest <45589059+Wyvest@users.noreply.github.com>2022-04-15 18:20:30 +0900
committerWyvest <45589059+Wyvest@users.noreply.github.com>2022-04-15 18:20:30 +0900
commit30df3e578cef533cacedf737449bbd35d4ea5d11 (patch)
tree8463196ec6ffe0162cbb636189f4e373b8a81d72 /src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java
parentddca3a3232eff15a7130efda03e7e5c408554412 (diff)
downloadOneConfig-30df3e578cef533cacedf737449bbd35d4ea5d11.tar.gz
OneConfig-30df3e578cef533cacedf737449bbd35d4ea5d11.tar.bz2
OneConfig-30df3e578cef533cacedf737449bbd35d4ea5d11.zip
fix shadow and add nanovg support
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java')
-rw-r--r--src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java65
1 files changed, 65 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..d0f54f7
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java
@@ -0,0 +1,65 @@
+package io.polyfrost.oneconfig.lwjgl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.Buffer;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.SeekableByteChannel;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static org.lwjgl.BufferUtils.createByteBuffer;
+import static org.lwjgl.system.MemoryUtil.memSlice;
+
+final class IOUtil {
+
+ private IOUtil() {
+ }
+
+ private static ByteBuffer resizeBuffer(ByteBuffer buffer, int newCapacity) {
+ ByteBuffer newBuffer = createByteBuffer(newCapacity);
+ buffer.flip();
+ newBuffer.put(buffer);
+ return newBuffer;
+ }
+
+ static ByteBuffer resourceToByteBuffer(String resource, int bufferSize) throws IOException {
+ ByteBuffer buffer;
+
+ Path path = Paths.get(resource);
+ if (Files.isReadable(path)) {
+ try (SeekableByteChannel fc = Files.newByteChannel(path)) {
+ buffer = createByteBuffer((int)fc.size() + 1);
+ while (fc.read(buffer) != -1) {
+ //noinspection UnnecessarySemicolon
+ ;
+ }
+ }
+ } else {
+ try (
+ InputStream source = IOUtil.class.getResourceAsStream(resource);
+ ReadableByteChannel rbc = Channels.newChannel(source)
+ ) {
+ buffer = createByteBuffer(bufferSize);
+
+ while (true) {
+ int bytes = rbc.read(buffer);
+ if (bytes == -1) {
+ break;
+ }
+ if (buffer.remaining() == 0) {
+ buffer = resizeBuffer(buffer, buffer.capacity() * 3 / 2); // 50%
+ }
+ }
+ }
+ }
+
+ //noinspection RedundantCast
+ ((Buffer) buffer).flip();
+ return memSlice(buffer);
+ }
+
+} \ No newline at end of file