You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/11/12 21:05:25 UTC

[commons-net] 02/03: Use try-with-resources

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

commit b902b4255e2254f6978156d0319dd2267e659517
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Nov 12 15:58:31 2022 -0500

    Use try-with-resources
---
 .../java/org/apache/commons/net/bsd/RCommandClient.java   |  3 +--
 src/main/java/org/apache/commons/net/bsd/RExecClient.java | 15 ++++++---------
 .../java/org/apache/commons/net/io/SocketInputStream.java |  2 --
 3 files changed, 7 insertions(+), 13 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 909d9ce9..dcb5a370 100644
--- a/src/main/java/org/apache/commons/net/bsd/RCommandClient.java
+++ b/src/main/java/org/apache/commons/net/bsd/RCommandClient.java
@@ -219,11 +219,10 @@ public class RCommandClient extends RExecClient {
     @Override
     InputStream createErrorStream() throws IOException {
         int localPort;
-        ServerSocket server;
         final Socket socket;
 
         localPort = MAX_CLIENT_PORT;
-        server = null;
+        ServerSocket server = null;
 
         for (localPort = MAX_CLIENT_PORT; localPort >= MIN_CLIENT_PORT; --localPort) {
             try {
diff --git a/src/main/java/org/apache/commons/net/bsd/RExecClient.java b/src/main/java/org/apache/commons/net/bsd/RExecClient.java
index 1c8ce930..bce823c8 100644
--- a/src/main/java/org/apache/commons/net/bsd/RExecClient.java
+++ b/src/main/java/org/apache/commons/net/bsd/RExecClient.java
@@ -79,17 +79,14 @@ public class RExecClient extends SocketClient {
     // This can be overridden in local package to implement port range
     // limitations of rcmd and rlogin
     InputStream createErrorStream() throws IOException {
-        final ServerSocket server;
         final Socket socket;
 
-        server = _serverSocketFactory_.createServerSocket(0, 1, getLocalAddress());
-
-        _output_.write(Integer.toString(server.getLocalPort()).getBytes(StandardCharsets.UTF_8)); // $NON-NLS-1$
-        _output_.write(NULL_CHAR);
-        _output_.flush();
-
-        socket = server.accept();
-        server.close();
+        try (final ServerSocket server = _serverSocketFactory_.createServerSocket(0, 1, getLocalAddress())) {
+            _output_.write(Integer.toString(server.getLocalPort()).getBytes(StandardCharsets.UTF_8)); // $NON-NLS-1$
+            _output_.write(NULL_CHAR);
+            _output_.flush();
+            socket = server.accept();
+        }
 
         if (remoteVerificationEnabled && !verifyRemote(socket)) {
             socket.close();
diff --git a/src/main/java/org/apache/commons/net/io/SocketInputStream.java b/src/main/java/org/apache/commons/net/io/SocketInputStream.java
index f21eb40d..f38c3321 100644
--- a/src/main/java/org/apache/commons/net/io/SocketInputStream.java
+++ b/src/main/java/org/apache/commons/net/io/SocketInputStream.java
@@ -27,10 +27,8 @@ import java.net.Socket;
  * afterward. This class is useful for situations where you are dealing with a stream originating from a socket, but do not have a reference to the socket, and
  * want to make sure it closes when the stream closes.
  *
- *
  * @see SocketOutputStream
  */
-
 public class SocketInputStream extends FilterInputStream {
     private final Socket socket;