Android Reading from an Input stream efficiently
In Android, efficient input stream reading can be achieved by leveraging the BufferedInputStream
class in combination with a sensibly sized buffer. This strategy accelerates the performance by reducing the number of disk reads. You can implement it like this:
The essence here is bulk reads and buffers—they are your road to efficiency. The selected buffer size (set at 8KB here) is significant. It helps strike a balance between memory usage and the reduction of I/O operations.
Understanding buffer size
Selecting an efficient buffer size is an art. Too small, and you end up waking the I/O operations every other microsecond. Too large, you get awarded the "Wasting Memory" badge. Profile on several target devices to find your ideal buffer size.
Meet StringBuilder, your new best friend
The StringBuilder
class is your secret weapon for efficiently dealing with character data. It shuns the hassles associated with creating a new String
object every other second. Here's how to use it:
This design pattern uses InputStreamReader
and BufferedReader
as layers of decoration on our InputStream
for efficient character reading. We then amass the characters in a StringBuilder
.
Leveraging libraries for streamlining
Consider adding some steroids to your code —aka using Battle-tested libraries like Apache Commons IO or Google Guava:
- With Apache Commons IO,
IOUtils.toString(inputStream, charset)
can convert an InputStream into a String in one easy step. - With Guava,
ByteStreams.toByteArray(inputStream)
converts an InputStream into a byte array rapidly.
Here's how:
Remember, using these methods could ramp up your app's size, so make sure the benefits outweigh the costs.
Don’t forget to cover your exceptions
Always remember to handle those unwanted party guests, the IOExceptions
, neatly:
Employing the try-with-resources statement ensures the BufferedInputStream
scrams when it's not needed, keeping your memory squeaky clean.
Was this article helpful?