javagzipoutputstreamautocloseable

Do I need to call finish() when using a GZIPOutputStream object inside try-wtth-resources block


When using a GZipOutputStream inside of a try-with-resources [autoclosable] block, do I need to explicitly call finish() after I'm done with my resource?


Solution

  • No, the close() method, which is called when leaving the try-with-resources block, calls finish(), so you do not need to do this manually.

    You can see this in its source code (GZipOutputStream does not override close(), so the close() method from its super class DeflaterOutptuStream is used). Also the JavaDoc of close() states that it "Writes remaining compressed data to the output stream", which is the same as finish() is documented to do.