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; @SuppressWarnings("RedundantCast") final class IOUtil { private IOUtil() { } private static ByteBuffer resizeBuffer(ByteBuffer buffer, int newCapacity) { ByteBuffer newBuffer = createByteBuffer(newCapacity); ((Buffer) 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% } } } } ((Buffer) buffer).flip(); return memSlice(buffer); } }