Package: org.codehaus.groovy.runtime

## [Java] Class SocketGroovyMethods

- org.codehaus.groovy.runtime.SocketGroovyMethods

- This class defines new groovy methods for Sockets which enhance JDK classes inside the Groovy environment.

  NOTE: While this class contains many 'public' static methods, it is primarily regarded as an internal class (its internal package name suggests this also). We value backwards compatibility of these methods when used within Groovy but value less backwards compatibility at the Java method call level. I.e. future versions of Groovy may remove or move a method call in this file but would normally aim to keep the method available from within Groovy.

- - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `static Socket` | `accept(ServerSocket serverSocket, Closure closure)` Accepts a connection and passes the resulting Socket to the closure which runs in a new Thread.
    `` | `static Socket` | `accept(ServerSocket serverSocket, boolean runInANewThread, Closure closure)` Accepts a connection and passes the resulting Socket to the closure which runs in a new Thread or the calling thread, as needed.
    `` | `static Writer` | `leftShift(Socket self, Object value)` Overloads the left shift operator to provide an append mechanism to add things to the output stream of a socket
    `` | `static OutputStream` | `leftShift(Socket self, byte[] value)` Overloads the left shift operator to provide an append mechanism to add bytes to the output stream of a socket
    `<T>` | `static T` | `withObjectStreams(Socket socket, Closure<T> closure)` Creates an InputObjectStream and an OutputObjectStream from a Socket, and passes them to the closure.
    `<T>` | `static T` | `withStreams(Socket socket, Closure<T> closure)` Passes the Socket's InputStream and OutputStream to the closure.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class DefaultGroovyMethodsSupport` | `cloneSimilarCollection, cloneSimilarMap, closeQuietly, closeWithWarning, createSimilarArray, createSimilarCollection, createSimilarCollection, createSimilarCollection, createSimilarList, createSimilarMap, createSimilarOrDefaultCollection, createSimilarQueue, createSimilarSet, normaliseIndex, sameType, subListBorders, subListBorders`
    `class Object` | `wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll`

- - ### Method Detail

    - #### public static Socket accept(ServerSocket serverSocket, @ClosureParams(value=SimpleType.class, options="java.net.Socket") Closure closure)

      Accepts a connection and passes the resulting Socket to the closure which runs in a new Thread.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `serverSocket` - a ServerSocket
        `closure` - a Closure
      Returns:
        a Socket
      See Also:
        ServerSocket.accept
      Since:
        1.0

    - #### public static Socket accept(ServerSocket serverSocket, boolean runInANewThread, @ClosureParams(value=SimpleType.class, options="java.net.Socket") Closure closure)

      Accepts a connection and passes the resulting Socket to the closure which runs in a new Thread or the calling thread, as needed.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `serverSocket` - a ServerSocket
        `runInANewThread` - This flag should be true, if the closure should be invoked in a new thread, else false.
        `closure` - a Closure
      Returns:
        a Socket
      See Also:
        ServerSocket.accept
      Since:
        1.7.6

    - #### public static Writer leftShift(Socket self, Object value)

      Overloads the left shift operator to provide an append mechanism to add things to the output stream of a socket

      throws:
        IOException if an IOException occurs.
      Parameters:
        `self` - a Socket
        `value` - a value to append
      Returns:
        a Writer
      Since:
        1.0

    - #### public static OutputStream leftShift(Socket self, byte[] value)

      Overloads the left shift operator to provide an append mechanism to add bytes to the output stream of a socket

      throws:
        IOException if an IOException occurs.
      Parameters:
        `self` - a Socket
        `value` - a value to append
      Returns:
        an OutputStream
      Since:
        1.0

    - #### <T> public static T withObjectStreams(Socket socket, @ClosureParams(value=SimpleType.class, options={"java.io.ObjectInputStream","java.io.ObjectOutputStream"}) Closure<T> closure)

      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.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `socket` - this Socket
        `closure` - a Closure
      Returns:
        the value returned by the closure
      Since:
        1.5.0

    - #### <T> public static T withStreams(Socket socket, @ClosureParams(value=SimpleType.class, options={"java.io.InputStream","java.io.OutputStream"}) Closure<T> closure)

      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.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `socket` - a Socket
        `closure` - a Closure
      Returns:
        the value returned by the closure
      Since:
        1.5.2