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 4b5ac366f0d7dc341bc244b9cc4dae0b4317b194
Author: Gary D. Gregory <garydgreg...@gmail.com>
AuthorDate: Sun Feb 16 09:47:31 2025 -0500

    Reuse IOUtils
    
    Javadoc
---
 .../java/org/apache/commons/net/SocketClient.java  | 22 +++++++++++++++++++++
 .../org/apache/commons/net/bsd/RCommandClient.java |  9 ++++-----
 .../org/apache/commons/net/bsd/RExecClient.java    |  9 ++++-----
 .../commons/net/examples/ftp/TFTPExample.java      | 11 ++++++++---
 .../java/org/apache/commons/net/ftp/FTPClient.java | 23 +++++++++++-----------
 .../org/apache/commons/net/ftp/FTPSClient.java     | 20 +++++++------------
 src/main/java/org/apache/commons/net/io/Util.java  | 21 +++++++-------------
 .../org/apache/commons/net/nntp/ReplyIterator.java |  8 ++++----
 .../apache/commons/net/util/KeyManagerUtils.java   |  4 ++--
 9 files changed, 70 insertions(+), 57 deletions(-)

diff --git a/src/main/java/org/apache/commons/net/SocketClient.java 
b/src/main/java/org/apache/commons/net/SocketClient.java
index 6348e88a..31d6b873 100644
--- a/src/main/java/org/apache/commons/net/SocketClient.java
+++ b/src/main/java/org/apache/commons/net/SocketClient.java
@@ -60,6 +60,28 @@ public abstract class SocketClient {
     /** The socket's connect timeout (0 = infinite timeout) */
     private static final int DEFAULT_CONNECT_TIMEOUT = 60000;
 
+    /**
+     * Gets the IP address string of the given Socket in textual presentation.
+     *
+     * @param socket the socket to query.
+     * @return  the raw IP address in a string format.
+     * @since 3.12.0
+     */
+    protected static String getHostAddress(final Socket socket) {
+        return getHostAddress(socket.getInetAddress());
+    }
+
+    /**
+     * Gets the IP address string of the given InetAddress in textual 
presentation.
+     *
+     * @param inetAddress Internet Protocol (IP) address to query.
+     * @return  the raw IP address in a string format.
+     * @since 3.12.0
+     */
+    protected static String getHostAddress(final InetAddress inetAddress) {
+        return inetAddress != null ? inetAddress.getHostAddress() : null;
+    }
+
     /**
      * A ProtocolCommandSupport object used to manage the registering of 
ProtocolCommandListeners and the firing of ProtocolCommandEvents.
      */
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 af4bace4..52022315 100644
--- a/src/main/java/org/apache/commons/net/bsd/RCommandClient.java
+++ b/src/main/java/org/apache/commons/net/bsd/RCommandClient.java
@@ -27,6 +27,7 @@ import java.net.SocketException;
 import java.net.UnknownHostException;
 import java.nio.charset.StandardCharsets;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.net.io.SocketInputStream;
 
 /**
@@ -220,19 +221,17 @@ public class RCommandClient extends RExecClient {
     @Override
     InputStream createErrorStream() throws IOException {
         final Socket socket;
-
         try (ServerSocket server = createServer()) {
             
_output_.write(Integer.toString(server.getLocalPort()).getBytes(StandardCharsets.UTF_8));
             _output_.write(NULL_CHAR);
             _output_.flush();
             socket = server.accept();
         }
-
         if (isRemoteVerificationEnabled() && !verifyRemote(socket)) {
-            socket.close();
-            throw new IOException("Security violation: unexpected connection 
attempt by " + socket.getInetAddress().getHostAddress());
+            final String hostAddress = getHostAddress(socket);
+            IOUtils.closeQuietly(socket);
+            throw new IOException("Security violation: unexpected connection 
attempt by " + hostAddress);
         }
-
         return new SocketInputStream(socket, socket.getInputStream());
     }
 
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 fc469eee..a121bff7 100644
--- a/src/main/java/org/apache/commons/net/bsd/RExecClient.java
+++ b/src/main/java/org/apache/commons/net/bsd/RExecClient.java
@@ -24,6 +24,7 @@ import java.net.ServerSocket;
 import java.net.Socket;
 import java.nio.charset.StandardCharsets;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.net.SocketClient;
 import org.apache.commons.net.io.SocketInputStream;
 import org.apache.commons.net.util.NetConstants;
@@ -82,19 +83,17 @@ public class RExecClient extends SocketClient {
     // limitations of rcmd and rlogin
     InputStream createErrorStream() throws IOException {
         final Socket socket;
-
         try (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();
-            throw new IOException("Security violation: unexpected connection 
attempt by " + socket.getInetAddress().getHostAddress());
+            final String hostAddress = getHostAddress(socket);
+            IOUtils.closeQuietly(socket);
+            throw new IOException("Security violation: unexpected connection 
attempt by " + hostAddress);
         }
-
         return new SocketInputStream(socket, socket.getInputStream());
     }
 
diff --git a/src/main/java/org/apache/commons/net/examples/ftp/TFTPExample.java 
b/src/main/java/org/apache/commons/net/examples/ftp/TFTPExample.java
index 76feaf9d..0e90f180 100644
--- a/src/main/java/org/apache/commons/net/examples/ftp/TFTPExample.java
+++ b/src/main/java/org/apache/commons/net/examples/ftp/TFTPExample.java
@@ -26,6 +26,7 @@ import java.net.SocketException;
 import java.net.UnknownHostException;
 import java.time.Duration;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.net.tftp.TFTP;
 import org.apache.commons.net.tftp.TFTPClient;
 import org.apache.commons.net.tftp.TFTPPacket;
@@ -50,9 +51,7 @@ public final class TFTPExample {
         boolean closed;
         tftp.close();
         try {
-            if (output != null) {
-                output.close();
-            }
+            IOUtils.close(output);
             closed = true;
         } catch (final IOException e) {
             closed = false;
@@ -62,6 +61,12 @@ public final class TFTPExample {
         return closed;
     }
 
+    /**
+     * Runs this application.
+     *
+     * @param args command line arguments.
+     * @throws IOException if a network or I/O error occurs.
+     */
     public static void main(final String[] args) throws IOException {
         boolean receiveFile = true, closed;
         int transferMode = TFTP.BINARY_MODE, argc;
diff --git a/src/main/java/org/apache/commons/net/ftp/FTPClient.java 
b/src/main/java/org/apache/commons/net/ftp/FTPClient.java
index 6931e8e8..7e0763d0 100644
--- a/src/main/java/org/apache/commons/net/ftp/FTPClient.java
+++ b/src/main/java/org/apache/commons/net/ftp/FTPClient.java
@@ -47,6 +47,7 @@ import java.util.Random;
 import java.util.Set;
 import java.util.regex.Matcher;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.net.MalformedServerReplyException;
 import org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory;
 import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory;
@@ -682,12 +683,12 @@ public class FTPClient extends FTP implements 
Configurable {
      * {@link #setRestartOffset(long)}, a REST command is issued to the server 
with the offset as an argument before establishing the data connection. Active
      * mode connections also cause a local PORT command to be issued.
      *
-     * @deprecated (3.3) Use {@link #_openDataConnection_(FTPCmd, String)} 
instead
      * @param command The int representation of the FTP command to send.
      * @param arg     The arguments to the FTP command. If this parameter is 
set to null, then the command is sent with no argument.
      * @return A Socket corresponding to the established data connection. Null 
is returned if an FTP protocol error is reported at any point during the
      *         establishment and initialization of the connection.
      * @throws IOException If an I/O error occurs while either sending a 
command to the server or receiving a reply from the server.
+     * @deprecated (3.3) Use {@link #_openDataConnection_(FTPCmd, String)} 
instead
      */
     @Deprecated
     protected Socket _openDataConnection_(final int command, final String arg) 
throws IOException {
@@ -807,10 +808,10 @@ public class FTPClient extends FTP implements 
Configurable {
         }
         if (remoteVerificationEnabled && !verifyRemote(socket)) {
             // Grab the host before we close the socket to avoid NET-663
-            final InetAddress socketHost = socket.getInetAddress();
-            socket.close();
-            throw new IOException(
-                    "Host attempting data connection " + 
socketHost.getHostAddress() + " is not same as server " + 
getRemoteAddress().getHostAddress());
+            final String socketHostAddress = getHostAddress(socket);
+            final String remoteHostAddress = getHostAddress(_socket_);
+            IOUtils.closeQuietly(socket);
+            throw new IOException("Host attempting data connection " + 
socketHostAddress + " is not same as server " + remoteHostAddress);
         }
         return socket;
     }
@@ -917,12 +918,12 @@ public class FTPClient extends FTP implements 
Configurable {
                 // Treat everything else as binary for now
                 Util.copyStream(input, local, getBufferSize(), 
CopyStreamEvent.UNKNOWN_STREAM_SIZE, mergeListeners(csl), false);
             } finally {
-                Util.closeQuietly(input);
+                IOUtils.closeQuietly(input);
             }
             // Get the transfer response
             return completePendingCommand();
         } finally {
-            Util.closeQuietly(socket);
+            IOUtils.closeQuietly(socket);
             if (csl != null) {
                 cslDebug = csl.cleanUp(); // fetch any outstanding keepalive 
replies
             }
@@ -992,8 +993,8 @@ public class FTPClient extends FTP implements Configurable {
             // Get the transfer response
             return completePendingCommand();
         } catch (final IOException e) {
-            Util.closeQuietly(output); // ignore close errors here
-            Util.closeQuietly(socket); // ignore close errors here
+            IOUtils.closeQuietly(output); // ignore close errors here
+            IOUtils.closeQuietly(socket); // ignore close errors here
             throw e;
         } finally {
             if (csl != null) {
@@ -2017,7 +2018,7 @@ public class FTPClient extends FTP implements 
Configurable {
         try {
             engine.readServerList(socket.getInputStream(), 
getControlEncoding());
         } finally {
-            Util.closeQuietly(socket);
+            IOUtils.closeQuietly(socket);
         }
         completePendingCommand();
         return engine;
@@ -2133,7 +2134,7 @@ public class FTPClient extends FTP implements 
Configurable {
         try {
             engine.readServerList(socket.getInputStream(), 
getControlEncoding());
         } finally {
-            Util.closeQuietly(socket);
+            IOUtils.closeQuietly(socket);
             completePendingCommand();
         }
         return engine;
diff --git a/src/main/java/org/apache/commons/net/ftp/FTPSClient.java 
b/src/main/java/org/apache/commons/net/ftp/FTPSClient.java
index 806e1c0e..d51f9ae9 100644
--- a/src/main/java/org/apache/commons/net/ftp/FTPSClient.java
+++ b/src/main/java/org/apache/commons/net/ftp/FTPSClient.java
@@ -38,6 +38,7 @@ import javax.net.ssl.SSLSocket;
 import javax.net.ssl.SSLSocketFactory;
 import javax.net.ssl.TrustManager;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.net.util.SSLContextUtils;
 import org.apache.commons.net.util.SSLSocketUtils;
 import org.apache.commons.net.util.TrustManagerUtils;
@@ -330,19 +331,14 @@ public class FTPSClient extends FTPClient {
     }
 
     /**
-     * Close open sockets.
+     * Closes sockets.
      *
-     * @param socket    main socket for proxy if enabled
-     * @param sslSocket ssl socket
-     * @throws IOException closing sockets is not successful
+     * @param socket    main socket for proxy if enabled.
+     * @param sslSocket SSL socket.
+     * @throws IOException closing sockets is not successful.
      */
     private void closeSockets(final Socket socket, final Socket sslSocket) 
throws IOException {
-        if (socket != null) {
-            socket.close();
-        }
-        if (sslSocket != null) {
-            sslSocket.close();
-        }
+        IOUtils.close(socket, sslSocket);
     }
 
     /**
@@ -372,9 +368,7 @@ public class FTPSClient extends FTPClient {
     @Override
     public void disconnect() throws IOException {
         super.disconnect();
-        if (plainSocket != null) {
-            plainSocket.close();
-        }
+        IOUtils.close(plainSocket);
         setSocketFactory(null);
         setServerSocketFactory(null);
     }
diff --git a/src/main/java/org/apache/commons/net/io/Util.java 
b/src/main/java/org/apache/commons/net/io/Util.java
index fd53a52f..abdb2520 100644
--- a/src/main/java/org/apache/commons/net/io/Util.java
+++ b/src/main/java/org/apache/commons/net/io/Util.java
@@ -29,6 +29,7 @@ import java.io.Writer;
 import java.net.Socket;
 import java.nio.charset.Charset;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.net.util.NetConstants;
 
 /**
@@ -53,15 +54,11 @@ public final class Util {
      *
      * @param closeable the object to close, may be {@code null}
      * @since 3.0
+     * @deprecated Use {@link IOUtils#closeQuietly(Closeable)}.
      */
+    @Deprecated
     public static void closeQuietly(final Closeable closeable) {
-        if (closeable != null) {
-            try {
-                closeable.close();
-            } catch (final IOException e) {
-                // Ignored
-            }
-        }
+        IOUtils.closeQuietly(closeable);
     }
 
     /**
@@ -69,15 +66,11 @@ public final class Util {
      *
      * @param socket the socket to close, may be {@code null}
      * @since 3.0
+     * @deprecated Use {@link IOUtils#closeQuietly(Socket)}.
      */
+    @Deprecated
     public static void closeQuietly(final Socket socket) {
-        if (socket != null) {
-            try {
-                socket.close();
-            } catch (final IOException e) {
-                // Ignored
-            }
-        }
+        IOUtils.closeQuietly(socket);
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/net/nntp/ReplyIterator.java 
b/src/main/java/org/apache/commons/net/nntp/ReplyIterator.java
index fcf814d4..67018e5b 100644
--- a/src/main/java/org/apache/commons/net/nntp/ReplyIterator.java
+++ b/src/main/java/org/apache/commons/net/nntp/ReplyIterator.java
@@ -22,8 +22,8 @@ import java.io.IOException;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.net.io.DotTerminatedMessageReader;
-import org.apache.commons.net.io.Util;
 
 /**
  * Wraps a {@link BufferedReader} and returns an {@code Iterable<String>} 
which returns the individual lines from the reader.
@@ -52,7 +52,7 @@ final class ReplyIterator implements Iterator<String>, 
Iterable<String> {
         reader = addDotReader ? new DotTerminatedMessageReader(_reader) : 
_reader;
         line = reader.readLine(); // prime the iterator
         if (line == null) {
-            Util.closeQuietly(reader);
+            IOUtils.closeQuietly(reader);
         }
     }
 
@@ -81,11 +81,11 @@ final class ReplyIterator implements Iterator<String>, 
Iterable<String> {
         try {
             line = reader.readLine(); // save next line
             if (line == null) {
-                Util.closeQuietly(reader);
+                IOUtils.closeQuietly(reader);
             }
         } catch (final IOException ex) {
             savedException = ex; // if it fails, save the exception, as it 
does not apply to this call
-            Util.closeQuietly(reader);
+            IOUtils.closeQuietly(reader);
         }
         return prev;
     }
diff --git a/src/main/java/org/apache/commons/net/util/KeyManagerUtils.java 
b/src/main/java/org/apache/commons/net/util/KeyManagerUtils.java
index 9bddd96c..2a71cb75 100644
--- a/src/main/java/org/apache/commons/net/util/KeyManagerUtils.java
+++ b/src/main/java/org/apache/commons/net/util/KeyManagerUtils.java
@@ -34,7 +34,7 @@ import java.util.Enumeration;
 import javax.net.ssl.KeyManager;
 import javax.net.ssl.X509ExtendedKeyManager;
 
-import org.apache.commons.net.io.Util;
+import org.apache.commons.io.IOUtils;
 
 /**
  * General KeyManager utilities
@@ -219,7 +219,7 @@ public final class KeyManagerUtils {
             stream = new FileInputStream(storePath);
             ks.load(stream, storePass.toCharArray());
         } finally {
-            Util.closeQuietly(stream);
+            IOUtils.closeQuietly(stream);
         }
         return ks;
     }

Reply via email to