I need to read a binary file consisting of 4 byte integers (small end) in a 2D array for my Android application. My current solution is as follows:
DataInputStream inp = null; try { inp = new DataInputStream(new BufferedInputStream(new FileInputStream(procData), 32768)); } catch (FileNotFoundException e) { Log.e(TAG, "File not found"); } int[][] test_data = new int[SIZE_X][SIZE_Y]; byte[] buffer = new byte[4]; ByteBuffer byteBuffer = ByteBuffer.allocate(4); for (int i=0; i < SIZE_Y; i++) { for (int j=0; j < SIZE_X; j++) { inp.read(buffer); byteBuffer = ByteBuffer.wrap(buffer); test_data[j][SIZE_Y - i - 1] = byteBuffer.order(ByteOrder.LITTLE_ENDIAN).getInt(); } }
This is pretty slow for a 2k * 2k array, it takes about 25 seconds. I see in DDMS that the garbage collector is working overtime, so this is probably one of the reasons for the slowdown.
There should be a more efficient way to use ByteBuffer to read this file in an array, but I don't see it at the moment. Any idea on how to speed this up?
java performance android file-io endianness
Mad scientist
source share