This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-net.git


The following commit(s) were added to refs/heads/master by this push:
     new bcd77d46 Use try-with-resources
bcd77d46 is described below

commit bcd77d46e5da200473b99f923518bdbc3d3ea0e1
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Wed Jul 19 11:08:07 2023 -0400

    Use try-with-resources
---
 .../org/apache/commons/net/bsd/RCommandClient.java | 37 ++++++++++------------
 1 file changed, 16 insertions(+), 21 deletions(-)

diff --git a/src/main/java/org/apache/commons/net/bsd/RCommandClient.java 
b/src/main/java/org/apache/commons/net/bsd/RCommandClient.java
index d8a0cb41..a790f0b2 100644
--- a/src/main/java/org/apache/commons/net/bsd/RCommandClient.java
+++ b/src/main/java/org/apache/commons/net/bsd/RCommandClient.java
@@ -216,31 +216,15 @@ public class RCommandClient extends RExecClient {
     // port number limitations.
     @Override
     InputStream createErrorStream() throws IOException {
-        int localPort;
         final Socket socket;
 
-        ServerSocket server = null;
-
-        for (localPort = MAX_CLIENT_PORT; localPort >= MIN_CLIENT_PORT; 
--localPort) {
-            try {
-                server = _serverSocketFactory_.createServerSocket(localPort, 
1, getLocalAddress());
-                break; // got a socket
-            } catch (final SocketException e) {
-                continue;
-            }
-        }
-
-        if (server == null) {
-            throw new BindException("All ports in use.");
+        try (ServerSocket server = createServer()) {
+            
_output_.write(Integer.toString(server.getLocalPort()).getBytes(StandardCharsets.UTF_8));
+            _output_.write(NULL_CHAR);
+            _output_.flush();
+            socket = server.accept();
         }
 
-        
_output_.write(Integer.toString(server.getLocalPort()).getBytes(StandardCharsets.UTF_8));
 // $NON-NLS
-        _output_.write(NULL_CHAR);
-        _output_.flush();
-
-        socket = server.accept();
-        server.close();
-
         if (isRemoteVerificationEnabled() && !verifyRemote(socket)) {
             socket.close();
             throw new IOException("Security violation: unexpected connection 
attempt by " + socket.getInetAddress().getHostAddress());
@@ -249,6 +233,17 @@ public class RCommandClient extends RExecClient {
         return new SocketInputStream(socket, socket.getInputStream());
     }
 
+    private ServerSocket createServer() throws IOException {
+        for (int localPort = MAX_CLIENT_PORT; localPort >= MIN_CLIENT_PORT; 
--localPort) {
+            try {
+                return _serverSocketFactory_.createServerSocket(localPort, 1, 
getLocalAddress());
+            } catch (final SocketException e) {
+                continue;
+            }
+        }
+        throw new BindException("All ports in use.");
+    }
+
     /**
      * Same as <code> rcommand(localUsername, remoteUsername, command, false); 
</code>
      *

Reply via email to