javaservletsmemory-leaksioresource-management

Should I explicitly close ZipOutputStream over response.getOutputStream()?


I have read following topic: Should one call .close() on HttpServletResponse.getOutputStream()/.getWriter()?

But what if I use following construction:

    ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());

Should I close it or container will do it instead of me?


Solution

  • Generally speaking the closing the outermost stream will propagate the close() to inner streams, closing all required resources.

    It's of course perfectly possible to create a badly behaving stream, but ZipOutputStream probably isn't one.

    In some cases it may not be enough to call close() on the outermost stream, but the documentation for the class should indicate any special behaviour.