public static < T , U extends OutputStream > T withStream ( U os , @ ClosureParams ( value = FirstParam . class ) Closure < T > closure ) throws IOException { try { T result = closure . call ( os ) ; os . flush ( ) ; OutputStream temp = os ; os = null ; temp . close ( ) ; return result ; } finally { closeWithWarning ( os ) ; } }
Passes this OutputStream to the closure ensuring that the stream is closed after the closure returns regardless of errors .