public static < T > T withStreams ( Socket socket , @ ClosureParams ( value = SimpleType . class , options = { "java.io.InputStream" , "java.io.OutputStream" } ) Closure < T > closure ) throws IOException { InputStream input = socket . getInputStream ( ) ; OutputStream output = socket . getOutputStream ( ) ; try { T result = closure . call ( new Object [ ] { input , output } ) ; InputStream temp1 = input ; input = null ; temp1 . close ( ) ; OutputStream temp2 = output ; output = null ; temp2 . close ( ) ; return result ; } finally { closeWithWarning ( input ) ; closeWithWarning ( output ) ; } }
Passes the Socket s InputStream and OutputStream to the closure . The streams will be closed after the closure returns even if an exception is thrown .