Link to home
Start Free TrialLog in
Avatar of onaled777
onaled777Flag for United States of America

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?

	@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
			}
		}
		
	}

Open in new window

Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Why don't you just close the stream once in the finally block?
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial