Skip to content Skip to sidebar Skip to footer

Android Inputstream.read() Horribly Slow

I'm working on an Android app that needs to communicate with a server (running on my PC) which is also written by me. The problem is that InputStream.read() takes an eternity, proc

Solution 1:

Why are you reading each byte individually? It looks like you really want to read the first 3 bytes and figure out your length and then read that block.

IE:

final byte[] lengthBuffer = newbyte[3];

int b = is.read(lengthBuffer);

// make sure b was 3 and do your length calculation

final byte buffer = newbyte[length];

b = is.read(buffer);

// check b and then you have your bytes

Then you can at least get the optimizations that Inputstream can provide for reading blocks of data rather than one byte at a time. And you are not allocating that mega array like you currently have.

Solution 2:

You're reading 1 byte at a time. That's incredibly inefficient. YOu want to read a large number of bytes at once- as much as possible. That's the reason your app is slow.

Post a Comment for "Android Inputstream.read() Horribly Slow"