Java InputStream to ByteBuffer

前端 未结 1 1055
野性不改
野性不改 2020-12-18 18:11

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

相关标签:
1条回答
  • 2020-12-18 18:55

    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)
    

    JAVA 9 UPDATE: as stated by @saka1029 if you're using java 9+ you can use the default InputStream API which now includes InputStream::readAllBytes function, so no external libraries needed

    InputStream is;
    byte[] bytes = is.readAllBytes()
    
    0 讨论(0)
提交回复
热议问题