Pinser
Pinser

Reputation: 1896

Cache BufferedInputStream? Wise?

I am building something related to caches, and want to understand whether it is wise to cache BufferedInputStream while I close the underlying InputStream.

In my case I am working on caching web calls (I know we cache in HTTP and well aware of how to that) so the objects are parsed into many different types, I read the InputStream and parse it into relevant type and as I understand the BufferedInputStream will have stored all bytes in it. I dont see anything wrong with caching, resetting and resuing it!

Need to know from someone experienced! Thanks!

Upvotes: -1

Views: 184

Answers (1)

Derek Fung
Derek Fung

Reputation: 8211

It will only work if the original inputstream contains less data than the Buffer size of your BufferedInputStream, the default should be 8K.

Also it is not favourable to rely on this, it is not designed to let you reuse it, the buffer was only to avoid too many reads in small size. Because of its purpose, you cannot guarantee the implementation to do what you want, it would access the underlying stream anytime theoretically.

Though you may go into the source code to confirm the behaviour, if you are willing to do this, why not clone and create your own CachedInputStream?

Upvotes: 1

Related Questions