Moved stream closing into a finally block to cover all cases in the serveResource method.