Java InputStream to ByteBuffer

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

I am reading dds textures, but since once built the jar I can't access those textures through url and file and have to use InputStream instead.

So I would need to know how I can obtain a from an java.io.InputStream.

Ps: no matter through 3rd part libraries, I just need it working

回答1:

For me the best in this case is Apache commons-io to handle this and similar tasks.

The IOUtils type has a static method to read an InputStream and return a byte[].

InputStream is; byte[] bytes = IOUtils.toByteArray(is); 

Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls toByteArray().

UPDATE: as long as you have the byte array, as @Peter pointed, you have to convert to ByteBuffer

ByteBuffer.wrap(bytes) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!