onaled777
asked on
Potential Resource Leak Error Being Thrown
The code below throws the error 'Potential resource leak. respOut may not be closed at this location ' on the line indicated.
Can you help me figure out why?
Can you help me figure out why?
@RequestMapping(value = "/getProgressNotes.htm", method = RequestMethod.GET)
@ResponseBody
public void getProgressNotes(HttpServletRequest request,
HttpServletResponse response, @RequestParam Long noteId) throws PMRUserException {
byte[] byteArray = iprogressnotesService.getProgressNotes(noteId);
OutputStream respOut = null;
try{
response.setContentType("application/pdf");
response.setContentLength(byteArray.length );
respOut = response.getOutputStream();
respOut.write(byteArray);
respOut.flush();
respOut.close();
} catch (Exception e) {
throw new CustomUserException(e.getMessage(),e);
}finally{
try {
if(respOut!=null){
respOut.close();
}
} catch (IOException e) {
throw new CustomUserException(e.getMessage(),e); //ERROR THROWN HERE
}
}
}
Why don't you just close the stream once in the finally block?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.