diff options
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java')
-rw-r--r-- | src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java | 65 |
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 |