5

I've read that wrapping a BufferedInputStream around an input stream is only of benefit if you're reading the input stream in small chunks. Otherwise, using it may actually have adverse effects.

What is the situation when the input stream is bitmap data fetched with HttpURLConnection (or your favourite networking library, e.g. OkHttp)... is it a help or a hindrance?

I'm wondering not only in terms of overall time/speed, but also in terms of resiliency... would using a buffer help at all with flaky network conditions where the connection is dropping in and out?

boolean useBufferedInputStream = true;  // <--- true or false?
URL url = new URL("https://example.com/my_bitmap.png");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream;
if (useBufferedInputStream) {
    inputStream = new BufferedInputStream(connection.getInputStream());
} else {
    inputStream = connection.getInputStream();
}
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
drmrbrewer
  • 11,491
  • 21
  • 85
  • 181
  • 2
    Well you have set up a scenario where you are able to test what you are interested in, i.e. speed/memory usage/resiliency/cpu cycles\battery usage.... The point is you are byte based (Bitmap) **NOT** character based. You can *pontificate* until the cows come home about which is best, but **try it**, **measure it** and see what suits you. There is no **one** answer. – Jon Goodwin Feb 15 '19 at 18:58
  • This must surely have been done before... people have been fetching bitmaps from Android applications ever since the first Android application was written, so I figured that there is no point in re-inventing the wheel... thought there might be clear answer that this buffering is or is not worth it for fetching bitmaps, based on the innards of how HttpURLConnection works. – drmrbrewer Feb 15 '19 at 20:39
  • 1
    It's likely that in your use-case the `BufferedInputStream` will not help. Previously I've seen it optimise reading of large local files in between a `FileInputStream` and `InputStream`. But this is different. Your bitmaps are likely too small to be significant. I also believe this comment here maintaining the state of the Http connection is important. https://stackoverflow.com/a/3076355/949224. But as per the earlier comment, compare both and test based on parameters you care most about. – dr_g Feb 19 '19 at 17:29

0 Answers0