I recently used Apache Commons-IO's class CountingInputStream
. It just keeps track how many bytes were read by updating a counter field.
I noticed that it updates its counter with the synchronized
keyword. The best source that I could find is IO-201, but it doesn't explain why.
I read in a few places that the Apache Commons code is good quality, but I'm now wondering why they have synchronized methods in an InputStream. I don't believe that thread safety is a useful property on a stream, nor do the commentors of IO-201.
Given that I'm not accessing an InputStream concurrently, is there any valid reason to have its methods synchronized? Or is there a valid use case to access an InputStream concurrently that will not produce data garbage?