Added Resource.readAsBytes to read a resource to a ByteBuffer
This commit is contained in:
parent
3ff074e384
commit
6afb7f8f1b
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user