Search code examples
androidbitmapfactorybufferedinputstream

Android BitmapFactory vs. BufferedInputStream


Does BitmapFactory.decodeStream(InputStream) already read in substantial chunks or should one be sure to pass a BufferedInputStream rather than a raw InputStream to it?

I ask as if BitmapFactory isn't doing IO in sizable blocks already, then one should clearly apply buffering to the input stream before passing it. If, however, it is already working in large blocks then applying a buffering is essentially just adding an unnecessary set of memcpy() operations.

It would be really nice if those writing APIs taking InputStream or OutputStream arguments were clear on such things in the Javadoc.

Currently, I am applying buffering unless InputStream is an instance of AssetManager.AssetInputStream, as I note special handling of that case under decodeStream().


Solution

  • If you look at the implementation, there are two methods that decodeStream might call.

    • nativeDecodeAsset(), which doesnt seem to show an implementation when I look through the class

    • decodeStreamInternal(), which reads 16 * 1024 bytes at once judging by the size of DECODE_BUFFER_SIZE

      (id post the code but the formatting seems to be broken right now)

    So I would assume that using BitmapFactory.decodeStream() is a safe choice