public static <T>T withObjectStreams(Socket socket,@ClosureParams(value=SimpleType.class,options={"java.io.ObjectInputStream","java.io.ObjectOutputStream"}) Closure<T> closure) throws IOException {
  InputStream input=socket.getInputStream();
  OutputStream output=socket.getOutputStream();
  ObjectOutputStream oos=new ObjectOutputStream(output);
  ObjectInputStream ois=new ObjectInputStream(input);
  try {
    T result=closure.call(new Object[]{ois,oos});
    InputStream temp1=ois;
    ois=null;
    temp1.close();
    temp1=input;
    input=null;
    temp1.close();
    OutputStream temp2=oos;
    oos=null;
    temp2.close();
    temp2=output;
    output=null;
    temp2.close();
    return result;
  }
  finally {
    closeWithWarning(ois);
    closeWithWarning(input);
    closeWithWarning(oos);
    closeWithWarning(output);
  }
}

public static < T > T withObjectStreams ( Socket socket , @ ClosureParams ( value = SimpleType . class , options = { "java.io.ObjectInputStream" , "java.io.ObjectOutputStream" } ) Closure < T > closure ) throws IOException { InputStream input = socket . getInputStream ( ) ; OutputStream output = socket . getOutputStream ( ) ; ObjectOutputStream oos = new ObjectOutputStream ( output ) ; ObjectInputStream ois = new ObjectInputStream ( input ) ; try { T result = closure . call ( new Object [ ] { ois , oos } ) ; InputStream temp1 = ois ; ois = null ; temp1 . close ( ) ; temp1 = input ; input = null ; temp1 . close ( ) ; OutputStream temp2 = oos ; oos = null ; temp2 . close ( ) ; temp2 = output ; output = null ; temp2 . close ( ) ; return result ; } finally { closeWithWarning ( ois ) ; closeWithWarning ( input ) ; closeWithWarning ( oos ) ; closeWithWarning ( output ) ; } }
creates an inputobjectstream and an outputobjectstream from a socket , and passes them to the closure . the streams will be closed after the closure returns , even if an exception is thrown .
