Added Resource.readAsBytes to read a resource to a ByteBuffer

This commit is contained in:
OLEGSHA 2020-08-31 18:32:54 +03:00
parent 3ff074e384
commit 6afb7f8f1b

View File

@ -21,7 +21,11 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import ru.windcorp.progressia.Progressia;
@ -50,4 +54,25 @@ public class Resource extends Named {
}
}
public ByteBuffer readAsBytes(ByteBuffer output) {
byte[] byteArray;
try (InputStream stream = getInputStream()) {
byteArray = ByteStreams.toByteArray(stream);
} catch (IOException e) {
throw new RuntimeException(e); // TODO handle gracefully
}
if (output == null || output.remaining() < byteArray.length) {
output = BufferUtils.createByteBuffer(byteArray.length);
}
output.put(byteArray);
return output;
}
public ByteBuffer readAsBytes() {
return readAsBytes(null);
}
}