Class SocketGroovyMethods


  • public class SocketGroovyMethods
    extends DefaultGroovyMethodsSupport
    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.

    • Constructor Detail

      • SocketGroovyMethods

        public SocketGroovyMethods()
    • Method Detail

      • withStreams

        public static <T> T withStreams​(java.net.Socket socket,
                                        Closure<T> closure)
                                 throws java.io.IOException
        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.
        Parameters:
        socket - a Socket
        closure - a Closure
        Returns:
        the value returned by the closure
        Throws:
        java.io.IOException - if an IOException occurs.
        Since:
        1.5.2
      • withObjectStreams

        public static <T> T withObjectStreams​(java.net.Socket socket,
                                              Closure<T> closure)
                                       throws java.io.IOException
        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.
        Parameters:
        socket - this Socket
        closure - a Closure
        Returns:
        the value returned by the closure
        Throws:
        java.io.IOException - if an IOException occurs.
        Since:
        1.5.0
      • leftShift

        public static java.io.Writer leftShift​(java.net.Socket self,
                                               java.lang.Object value)
                                        throws java.io.IOException
        Overloads the left shift operator to provide an append mechanism to add things to the output stream of a socket
        Parameters:
        self - a Socket
        value - a value to append
        Returns:
        a Writer
        Throws:
        java.io.IOException - if an IOException occurs.
        Since:
        1.0
      • leftShift

        public static java.io.OutputStream leftShift​(java.net.Socket self,
                                                     byte[] value)
                                              throws java.io.IOException
        Overloads the left shift operator to provide an append mechanism to add bytes to the output stream of a socket
        Parameters:
        self - a Socket
        value - a value to append
        Returns:
        an OutputStream
        Throws:
        java.io.IOException - if an IOException occurs.
        Since:
        1.0
      • accept

        public static java.net.Socket accept​(java.net.ServerSocket serverSocket,
                                             Closure closure)
                                      throws java.io.IOException
        Accepts a connection and passes the resulting Socket to the closure which runs in a new Thread.
        Parameters:
        serverSocket - a ServerSocket
        closure - a Closure
        Returns:
        a Socket
        Throws:
        java.io.IOException - if an IOException occurs.
        Since:
        1.0
        See Also:
        ServerSocket.accept()
      • accept

        public static java.net.Socket accept​(java.net.ServerSocket serverSocket,
                                             boolean runInANewThread,
                                             Closure closure)
                                      throws java.io.IOException
        Accepts a connection and passes the resulting Socket to the closure which runs in a new Thread or the calling thread, as needed.
        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
        Throws:
        java.io.IOException - if an IOException occurs.
        Since:
        1.7.6
        See Also:
        ServerSocket.accept()