Is is necessary to close the input stream returned from HttpServletRequest?

java, servlets

Solution

The thumb rule in I/O is, if you did not open/create the inputstream source yourself, then you do not necessarily need to close it as well. Here you are just wrapping the request's inputstream, so you don't necessarily need to close it.

If you did open the input yourself by e.g. `new FileInputStream("c:/file.ext")` then you obviously need to close it yourself in the finally block. The container ought to do so under the hood.

Problem

I've got some production code that does something like: ``` HttpServletRequest httpServletRequest ... DataInputStream dis = new DataInputStream(httpServletRequest.getInputStream()) ``` These streams are never closed explicitly. I'm assuming here that the servlet container manages this (JBOss Web). What is the correct way to handle this?

Original source

Related problems