Why does Java's InflaterInputStream (and other similar classes) only conditionally call end on it's internal Inflater

As with many "why" questions, this is an educated guess. I didn't see any explicit explanation for this, so who knows what the original programmer was thinking? Anyway, take my answer with a grain of salt.

The other constructors all take an Inflater instance, which means that the user has a reference to the (internal) Inflater. Note that these classes have no getter to get the Inflater out. So the only way the user would have a reference to it, is by passing it from outside (well, that and using reflection, but let's not go there).

So perhaps the assumption was that since the user passed his own Inflater instance, then he wants to manage the Inflater himself, possibly reusing it after this steam has ended. Thus, closing the Inflater when the stream is closed isn't a good idea.


There are many methods in the Java Runtime Library that takes e.g. an OutputStream (such as Files.copy()). Unless those methods explicitly state that the stream will be closed by the method, the stream will not be closed. Closing the stream is the responsibility of the stream "owner", e.g. the caller of the method.

Similarly, neither constructor of InflaterInputStream that takes an Inflater states that they will end() the Inflater, which means that they won't. It is up to the caller to end it, when needed.

When using the constructor that creates the Inflater for you, the InflaterInputStream becomes the "owner" of that internal Inflater, and it is therefore the responsibility of the InflaterInputStream to end the Inflater.

Resource Management

The general guideline for resource management is that, unless otherwise documented, whoever allocates a resource is responsible for releasing (closing, ending, ...) the resource.

Inflater is a resource, so normal resource management rules apply.