Fast reading of little endian integers from file

后端 未结 4 1169
无人及你
无人及你 2020-12-15 11:13

I need to read a binary file consisting of 4 byte integers (little endian) into a 2D array for my Android application. My current solution is the following:

         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-15 11:30

    First of all, your 'inp.read(buffer)' is unsafe, as read contract does not guarantee that it will read all 4 bytes.

    That aside, for quick transformation use the algorithm from DataInputStream.readInt

    I've adapted for you case of byte array of 4 bytes:

    int little2big(byte[ ] b) {
        return (b[3]&0xff)<<24)+((b[2]&0xff)<<16)+((b[1]&0xff)<<8)+(b[0]&0xff);
    }
    

提交回复
热议问题