Package org.codehaus.groovy.runtime
Class SocketGroovyMethods
java.lang.Object
org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport
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.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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 Socket
accept
(ServerSocket serverSocket, Closure closure) Accepts a connection and passes the resulting Socket to the closure which runs in a new Thread.static OutputStream
Overloads the left shift operator to provide an append mechanism to add bytes to the output stream of a socketstatic Writer
Overloads the left shift operator to provide an append mechanism to add things to the output stream of a socketstatic <T> T
withObjectStreams
(Socket socket, Closure<T> closure) Creates an InputObjectStream and an OutputObjectStream from a Socket, and passes them to the closure.static <T> T
withStreams
(Socket socket, Closure<T> closure) Passes the Socket's InputStream and OutputStream to the closure.Methods inherited from class org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport
cloneSimilarCollection, cloneSimilarMap, closeQuietly, closeWithWarning, createSimilarArray, createSimilarCollection, createSimilarCollection, createSimilarCollection, createSimilarList, createSimilarMap, createSimilarOrDefaultCollection, createSimilarQueue, createSimilarSet, normaliseIndex, sameType, subListBorders, subListBorders, subListRange, writeUTF16BomIfRequired, writeUTF16BomIfRequired, writeUTF16BomIfRequired, writeUTF16BomIfRequired
-
Constructor Details
-
SocketGroovyMethods
public SocketGroovyMethods()
-
-
Method Details
-
withStreams
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 Socketclosure
- a Closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.2
-
withObjectStreams
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 Socketclosure
- a Closure- Returns:
- the value returned by the closure
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.5.0
-
leftShift
Overloads the left shift operator to provide an append mechanism to add things to the output stream of a socket- Parameters:
self
- a Socketvalue
- a value to append- Returns:
- a Writer
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
leftShift
Overloads the left shift operator to provide an append mechanism to add bytes to the output stream of a socket- Parameters:
self
- a Socketvalue
- a value to append- Returns:
- an OutputStream
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
-
accept
Accepts a connection and passes the resulting Socket to the closure which runs in a new Thread.- Parameters:
serverSocket
- a ServerSocketclosure
- a Closure- Returns:
- a Socket
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.0
- See Also:
-
accept
public static Socket accept(ServerSocket serverSocket, boolean runInANewThread, Closure closure) throws 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 ServerSocketrunInANewThread
- This flag should be true, if the closure should be invoked in a new thread, else false.closure
- a Closure- Returns:
- a Socket
- Throws:
IOException
- if an IOException occurs.- Since:
- 1.7.6
- See Also:
-