FTPClient.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.net.ftp;

  18. import java.io.BufferedInputStream;
  19. import java.io.BufferedOutputStream;
  20. import java.io.BufferedReader;
  21. import java.io.BufferedWriter;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.InputStreamReader;
  25. import java.io.OutputStream;
  26. import java.io.OutputStreamWriter;
  27. import java.io.Reader;
  28. import java.net.Inet6Address;
  29. import java.net.InetAddress;
  30. import java.net.InetSocketAddress;
  31. import java.net.ServerSocket;
  32. import java.net.Socket;
  33. import java.net.SocketException;
  34. import java.net.SocketTimeoutException;
  35. import java.net.UnknownHostException;
  36. import java.nio.charset.StandardCharsets;
  37. import java.time.Duration;
  38. import java.time.Instant;
  39. import java.util.ArrayList;
  40. import java.util.Calendar;
  41. import java.util.HashMap;
  42. import java.util.HashSet;
  43. import java.util.Locale;
  44. import java.util.Properties;
  45. import java.util.Random;
  46. import java.util.Set;
  47. import java.util.regex.Matcher;

  48. import org.apache.commons.net.MalformedServerReplyException;
  49. import org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory;
  50. import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory;
  51. import org.apache.commons.net.ftp.parser.MLSxEntryParser;
  52. import org.apache.commons.net.io.CRLFLineReader;
  53. import org.apache.commons.net.io.CopyStreamAdapter;
  54. import org.apache.commons.net.io.CopyStreamEvent;
  55. import org.apache.commons.net.io.CopyStreamListener;
  56. import org.apache.commons.net.io.FromNetASCIIInputStream;
  57. import org.apache.commons.net.io.SocketOutputStream;
  58. import org.apache.commons.net.io.ToNetASCIIOutputStream;
  59. import org.apache.commons.net.io.Util;
  60. import org.apache.commons.net.util.NetConstants;

  61. /**
  62.  * FTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server. This class takes care of all low level details of
  63.  * interacting with an FTP server and provides a convenient higher level interface. As with all classes derived from
  64.  * {@link org.apache.commons.net.SocketClient}, you must first connect to the server with {@link org.apache.commons.net.SocketClient#connect connect } before
  65.  * doing anything, and finally {@link org.apache.commons.net.SocketClient#disconnect() disconnect} after you're completely finished interacting with the server.
  66.  * Then you need to check the FTP reply code to see if the connection was successful. For example:
  67.  *
  68.  * <pre>
  69.  *    FTPClient ftp = new FTPClient();
  70.  *    FTPClientConfig config = new FTPClientConfig();
  71.  *    config.setXXX(YYY); // change required options
  72.  *    // for example config.setServerTimeZoneId("Pacific/Pitcairn")
  73.  *    ftp.configure(config );
  74.  *    boolean error = false;
  75.  *    try {
  76.  *      int reply;
  77.  *      String server = "ftp.example.com";
  78.  *      ftp.connect(server);
  79.  *      System.out.println("Connected to " + server + ".");
  80.  *      System.out.print(ftp.getReplyString());
  81.  *
  82.  *      // After connection attempt, you should check the reply code to verify
  83.  *      // success.
  84.  *      reply = ftp.getReplyCode();
  85.  *
  86.  *      if (!FTPReply.isPositiveCompletion(reply)) {
  87.  *        ftp.disconnect();
  88.  *        System.err.println("FTP server refused connection.");
  89.  *        System.exit(1);
  90.  *      }
  91.  *      ... // transfer files
  92.  *      ftp.logout();
  93.  *    } catch (IOException e) {
  94.  *      error = true;
  95.  *      e.printStackTrace();
  96.  *    } finally {
  97.  *      if (ftp.isConnected()) {
  98.  *        try {
  99.  *          ftp.disconnect();
  100.  *        } catch (IOException ioe) {
  101.  *          // do nothing
  102.  *        }
  103.  *      }
  104.  *      System.exit(error ? 1 : 0);
  105.  *    }
  106.  * </pre>
  107.  * <p>
  108.  * Immediately after connecting is the only real time you need to check the reply code (because connect is of type void). The convention for all the FTP command
  109.  * methods in FTPClient is such that they either return a boolean value or some other value. The boolean methods return true on a successful completion reply
  110.  * from the FTP server and false on a reply resulting in an error condition or failure. The methods returning a value other than boolean return a value
  111.  * containing the higher level data produced by the FTP command, or null if a reply resulted in an error condition or failure. If you want to access the exact
  112.  * FTP reply code causing a success or failure, you must call {@link org.apache.commons.net.ftp.FTP#getReplyCode getReplyCode } after a success or failure.
  113.  * </p>
  114.  * <p>
  115.  * The default settings for FTPClient are for it to use <code>FTP.ASCII_FILE_TYPE</code>, <code>FTP.NON_PRINT_TEXT_FORMAT</code>,
  116.  * <code>FTP.STREAM_TRANSFER_MODE</code>, and <code>FTP.FILE_STRUCTURE</code>. The only file types directly supported are <code>FTP.ASCII_FILE_TYPE</code>
  117.  * and <code>FTP.BINARY_FILE_TYPE</code>. Because there are at least 4 different EBCDIC encodings, we have opted not to provide direct support for EBCDIC. To
  118.  * transfer EBCDIC and other unsupported file types you must create your own filter InputStreams and OutputStreams and wrap them around the streams returned or
  119.  * required by the FTPClient methods. FTPClient uses the {@link ToNetASCIIOutputStream NetASCII} filter streams to provide transparent handling of ASCII files.
  120.  * We will consider incorporating EBCDIC support if there is enough demand.
  121.  * </p>
  122.  * <p>
  123.  * <code>FTP.NON_PRINT_TEXT_FORMAT</code>, <code>FTP.STREAM_TRANSFER_MODE</code>, and <code>FTP.FILE_STRUCTURE</code> are the only supported formats,
  124.  * transfer modes, and file structures.
  125.  * </p>
  126.  * <p>
  127.  * Because the handling of sockets on different platforms can differ significantly, the FTPClient automatically issues a new PORT (or EPRT) command prior to
  128.  * every transfer requiring that the server connect to the client's data port. This ensures identical problem-free behavior on Windows, Unix, and Macintosh
  129.  * platforms. Additionally, it relieves programmers from having to issue the PORT (or EPRT) command themselves and dealing with platform dependent issues.
  130.  * </p>
  131.  * <p>
  132.  * Additionally, for security purposes, all data connections to the client are verified to ensure that they originated from the intended party (host and port).
  133.  * If a data connection is initiated by an unexpected party, the command will close the socket and throw an IOException. You may disable this behavior with
  134.  * {@link #setRemoteVerificationEnabled setRemoteVerificationEnabled()}.
  135.  * </p>
  136.  * <p>
  137.  * You should keep in mind that the FTP server may choose to prematurely close a connection if the client has been idle for longer than a given time period
  138.  * (usually 900 seconds). The FTPClient class will detect a premature FTP server connection closing when it receives a
  139.  * {@link org.apache.commons.net.ftp.FTPReply#SERVICE_NOT_AVAILABLE FTPReply.SERVICE_NOT_AVAILABLE } response to a command. When that occurs, the FTP class
  140.  * method encountering that reply will throw an {@link org.apache.commons.net.ftp.FTPConnectionClosedException} . <code>FTPConnectionClosedException</code> is a
  141.  * subclass of <code>IOException</code> and therefore need not be caught separately, but if you are going to catch it separately, its catch block must appear
  142.  * before the more general <code>IOException</code> catch block. When you encounter an {@link org.apache.commons.net.ftp.FTPConnectionClosedException} , you
  143.  * must disconnect the connection with {@link #disconnect disconnect() } to properly clean up the system resources used by FTPClient. Before disconnecting, you
  144.  * may check the last reply code and text with {@link org.apache.commons.net.ftp.FTP#getReplyCode getReplyCode },
  145.  * {@link org.apache.commons.net.ftp.FTP#getReplyString getReplyString }, and {@link org.apache.commons.net.ftp.FTP#getReplyStrings getReplyStrings}. You may
  146.  * avoid server disconnections while the client is idle by periodically sending NOOP commands to the server.
  147.  * </p>
  148.  * <p>
  149.  * Rather than list it separately for each method, we mention here that every method communicating with the server and throwing an IOException can also throw a
  150.  * {@link org.apache.commons.net.MalformedServerReplyException} , which is a subclass of IOException. A MalformedServerReplyException will be thrown when the
  151.  * reply received from the server deviates enough from the protocol specification that it cannot be interpreted in a useful manner despite attempts to be as
  152.  * lenient as possible.
  153.  * </p>
  154.  * <p>
  155.  * Listing API Examples Both paged and unpaged examples of directory listings are available, as follows:
  156.  * </p>
  157.  * <p>
  158.  * Unpaged (whole list) access, using a parser accessible by auto-detect:
  159.  * </p>
  160.  *
  161.  * <pre>
  162.  * FTPClient f = new FTPClient();
  163.  * f.connect(server);
  164.  * f.login(user, password);
  165.  * FTPFile[] files = f.listFiles(directory);
  166.  * </pre>
  167.  * <p>
  168.  * Paged access, using a parser not accessible by auto-detect. The class defined in the first parameter of initateListParsing should be derived from
  169.  * org.apache.commons.net.FTPFileEntryParser:
  170.  * </p>
  171.  *
  172.  * <pre>
  173.  * FTPClient f = new FTPClient();
  174.  * f.connect(server);
  175.  * f.login(user, password);
  176.  * FTPListParseEngine engine = f.initiateListParsing("com.whatever.YourOwnParser", directory);
  177.  *
  178.  * while (engine.hasNext()) {
  179.  *     FTPFile[] files = engine.getNext(25); // "page size" you want
  180.  *     // do whatever you want with these files, display them, etc.
  181.  *     // expensive FTPFile objects not created until needed.
  182.  * }
  183.  * </pre>
  184.  * <p>
  185.  * Paged access, using a parser accessible by auto-detect:
  186.  * </p>
  187.  *
  188.  * <pre>
  189.  * FTPClient f = new FTPClient();
  190.  * f.connect(server);
  191.  * f.login(user, password);
  192.  * FTPListParseEngine engine = f.initiateListParsing(directory);
  193.  *
  194.  * while (engine.hasNext()) {
  195.  *     FTPFile[] files = engine.getNext(25); // "page size" you want
  196.  *     // do whatever you want with these files, display them, etc.
  197.  *     // expensive FTPFile objects not created until needed.
  198.  * }
  199.  * </pre>
  200.  * <p>
  201.  * For examples of using FTPClient on servers whose directory listings
  202.  * </p>
  203.  * <ul>
  204.  * <li>use languages other than English</li>
  205.  * <li>use date formats other than the American English "standard" <code>MM d yyyy</code></li>
  206.  * <li>are in different time zones and you need accurate timestamps for dependency checking as in Ant</li>
  207.  * </ul>
  208.  * see {@link FTPClientConfig FTPClientConfig}.
  209.  * <p>
  210.  * <b>Control channel keep-alive feature</b>:
  211.  * </p>
  212.  * <p>
  213.  * <b>Please note:</b> this does not apply to the methods where the user is responsible for writing or reading the data stream, i.e.
  214.  * {@link #retrieveFileStream(String)} , {@link #storeFileStream(String)} and the other xxxFileStream methods
  215.  * </p>
  216.  * <p>
  217.  * During file transfers, the data connection is busy, but the control connection is idle. FTP servers know that the control connection is in use, so won't
  218.  * close it through lack of activity, but it's a lot harder for network routers to know that the control and data connections are associated with each other.
  219.  * Some routers may treat the control connection as idle, and disconnect it if the transfer over the data connection takes longer than the allowable idle time
  220.  * for the router.
  221.  * <p>
  222.  * One solution to this is to send a safe command (i.e. NOOP) over the control connection to reset the router's idle timer. This is enabled as follows:
  223.  * </p>
  224.  *
  225.  * <pre>
  226.  * // Set timeout to 5 minutes
  227.  * ftpClient.setControlKeepAliveTimeout(Duration.ofMinutes(5));
  228.  * </pre>
  229.  *
  230.  * <p>
  231.  * This will cause the file upload/download methods to send a NOOP approximately every 5 minutes. The following public methods support this:
  232.  * </p>
  233.  * <ul>
  234.  * <li>{@link #retrieveFile(String, OutputStream)}</li>
  235.  * <li>{@link #appendFile(String, InputStream)}</li>
  236.  * <li>{@link #storeFile(String, InputStream)}</li>
  237.  * <li>{@link #storeUniqueFile(InputStream)}</li>
  238.  * <li>{@link #storeUniqueFileStream(String)}</li>
  239.  * </ul>
  240.  * <p>
  241.  * This feature does not apply to the methods where the user is responsible for writing or reading the data stream, i.e. {@link #retrieveFileStream(String)} ,
  242.  * {@link #storeFileStream(String)} and the other xxxFileStream methods. In such cases, the user is responsible for keeping the control connection alive if
  243.  * necessary.
  244.  * </p>
  245.  * <p>
  246.  * The implementation currently uses a {@link CopyStreamListener} which is passed to the
  247.  * {@link Util#copyStream(InputStream, OutputStream, int, long, CopyStreamListener, boolean)} method, so the timing is partially dependent on how long each
  248.  * block transfer takes.
  249.  * </p>
  250.  * <p>
  251.  * <b>This keep-alive feature is optional; if it does not help or causes problems then don't use it.</b>
  252.  * </p>
  253.  *
  254.  * @see #FTP_SYSTEM_TYPE
  255.  * @see #SYSTEM_TYPE_PROPERTIES
  256.  * @see FTP
  257.  * @see FTPConnectionClosedException
  258.  * @see FTPFileEntryParser
  259.  * @see FTPFileEntryParserFactory
  260.  * @see DefaultFTPFileEntryParserFactory
  261.  * @see FTPClientConfig
  262.  * @see org.apache.commons.net.MalformedServerReplyException
  263.  */
  264. public class FTPClient extends FTP implements Configurable {

  265.     // @since 3.0
  266.     private static final class CSL implements CopyStreamListener {

  267.         private final FTPClient parent;
  268.         private final long idleMillis;
  269.         private final int currentSoTimeoutMillis;

  270.         private long lastIdleTimeMillis = System.currentTimeMillis();
  271.         private int notAcked;
  272.         private int acksAcked;
  273.         private int ioErrors;

  274.         CSL(final FTPClient parent, final Duration idleDuration, final Duration maxWaitDuration) throws SocketException {
  275.             this.idleMillis = idleDuration.toMillis();
  276.             this.parent = parent;
  277.             this.currentSoTimeoutMillis = parent.getSoTimeout();
  278.             parent.setSoTimeout(DurationUtils.toMillisInt(maxWaitDuration));
  279.         }

  280.         @Override
  281.         public void bytesTransferred(final CopyStreamEvent event) {
  282.             bytesTransferred(event.getTotalBytesTransferred(), event.getBytesTransferred(), event.getStreamSize());
  283.         }

  284.         @Override
  285.         public void bytesTransferred(final long totalBytesTransferred, final int bytesTransferred, final long streamSize) {
  286.             final long nowMillis = System.currentTimeMillis();
  287.             if (nowMillis - lastIdleTimeMillis > idleMillis) {
  288.                 try {
  289.                     parent.__noop();
  290.                     acksAcked++;
  291.                 } catch (final SocketTimeoutException e) {
  292.                     notAcked++;
  293.                 } catch (final IOException e) {
  294.                     ioErrors++;
  295.                     // Ignored
  296.                 }
  297.                 lastIdleTimeMillis = nowMillis;
  298.             }
  299.         }

  300.         int[] cleanUp() throws IOException {
  301.             final int remain = notAcked;
  302.             try {
  303.                 while (notAcked > 0) {
  304.                     parent.getReply(); // we do want to see these
  305.                     notAcked--; // only decrement if actually received
  306.                 }
  307.             } catch (final SocketTimeoutException e) { // NET-584
  308.                 // ignored
  309.             } finally {
  310.                 parent.setSoTimeout(currentSoTimeoutMillis);
  311.             }
  312.             return new int[] { acksAcked, remain, notAcked, ioErrors }; // debug counts
  313.         }

  314.     }

  315.     /**
  316.      * Strategy interface for updating host names received from FTP server for passive NAT workaround.
  317.      *
  318.      * @since 3.6
  319.      */
  320.     public interface HostnameResolver {
  321.         String resolve(String hostname) throws UnknownHostException;
  322.     }

  323.     /**
  324.      * Default strategy for passive NAT workaround (site-local replies are replaced.)
  325.      *
  326.      * @since 3.6
  327.      */
  328.     public static class NatServerResolverImpl implements HostnameResolver {
  329.         private final FTPClient client;

  330.         public NatServerResolverImpl(final FTPClient client) {
  331.             this.client = client;
  332.         }

  333.         @Override
  334.         public String resolve(final String hostname) throws UnknownHostException {
  335.             String newHostname = hostname;
  336.             final InetAddress host = InetAddress.getByName(newHostname);
  337.             // reply is a local address, but target is not - assume NAT box changed the PASV reply
  338.             if (host.isSiteLocalAddress()) {
  339.                 final InetAddress remote = this.client.getRemoteAddress();
  340.                 if (!remote.isSiteLocalAddress()) {
  341.                     newHostname = remote.getHostAddress();
  342.                 }
  343.             }
  344.             return newHostname;
  345.         }
  346.     }

  347.     private static final class PropertiesSingleton {

  348.         static final Properties PROPERTIES;

  349.         static {
  350.             final InputStream resourceAsStream = FTPClient.class.getResourceAsStream(SYSTEM_TYPE_PROPERTIES);
  351.             Properties p = null;
  352.             if (resourceAsStream != null) {
  353.                 p = new Properties();
  354.                 try {
  355.                     p.load(resourceAsStream);
  356.                 } catch (final IOException e) {
  357.                     // Ignored
  358.                 } finally {
  359.                     try {
  360.                         resourceAsStream.close();
  361.                     } catch (final IOException e) {
  362.                         // Ignored
  363.                     }
  364.                 }
  365.             }
  366.             PROPERTIES = p;
  367.         }

  368.     }

  369.     /**
  370.      * The system property ({@value}) which can be used to override the system type.<br>
  371.      * If defined, the value will be used to create any automatically created parsers.
  372.      *
  373.      * @since 3.0
  374.      */
  375.     public static final String FTP_SYSTEM_TYPE = "org.apache.commons.net.ftp.systemType";

  376.     /**
  377.      * The system property ({@value}) which can be used as the default system type.<br>
  378.      * If defined, the value will be used if the SYST command fails.
  379.      *
  380.      * @since 3.1
  381.      */
  382.     public static final String FTP_SYSTEM_TYPE_DEFAULT = "org.apache.commons.net.ftp.systemType.default";

  383.     /**
  384.      * The system property that defines the default for {@link #isIpAddressFromPasvResponse()}. This property, if present, configures the default for the
  385.      * following: If the client receives the servers response for a PASV request, then that response will contain an IP address. If this property is true, then
  386.      * the client will use that IP address, as requested by the server. This is compatible to version {@code 3.8.0}, and before. If this property is false, or
  387.      * absent, then the client will ignore that IP address, and instead use the remote address of the control connection.
  388.      *
  389.      * @see #isIpAddressFromPasvResponse()
  390.      * @see #setIpAddressFromPasvResponse(boolean)
  391.      * @since 3.9.0
  392.      */
  393.     public static final String FTP_IP_ADDRESS_FROM_PASV_RESPONSE = "org.apache.commons.net.ftp.ipAddressFromPasvResponse";

  394.     /**
  395.      * The name of an optional systemType properties file ({@value}), which is loaded using {@link Class#getResourceAsStream(String)}.<br>
  396.      * The entries are the systemType (as determined by {@link FTPClient#getSystemType}) and the values are the replacement type or parserClass, which is passed
  397.      * to {@link FTPFileEntryParserFactory#createFileEntryParser(String)}.<br>
  398.      * For example:
  399.      *
  400.      * <pre>
  401.      * Plan 9=Unix
  402.      * OS410=org.apache.commons.net.ftp.parser.OS400FTPEntryParser
  403.      * </pre>
  404.      *
  405.      * @since 3.0
  406.      */
  407.     public static final String SYSTEM_TYPE_PROPERTIES = "/systemType.properties";

  408.     /**
  409.      * A constant indicating the FTP session is expecting all transfers to occur between the client (local) and server and that the server should connect to the
  410.      * client's data port to initiate a data transfer. This is the default data connection mode when and FTPClient instance is created.
  411.      */
  412.     public static final int ACTIVE_LOCAL_DATA_CONNECTION_MODE = 0;

  413.     /**
  414.      * A constant indicating the FTP session is expecting all transfers to occur between two remote servers and that the server the client is connected to
  415.      * should connect to the other server's data port to initiate a data transfer.
  416.      */
  417.     public static final int ACTIVE_REMOTE_DATA_CONNECTION_MODE = 1;

  418.     /**
  419.      * A constant indicating the FTP session is expecting all transfers to occur between the client (local) and server and that the server is in passive mode,
  420.      * requiring the client to connect to the server's data port to initiate a transfer.
  421.      */
  422.     public static final int PASSIVE_LOCAL_DATA_CONNECTION_MODE = 2;

  423.     /**
  424.      * A constant indicating the FTP session is expecting all transfers to occur between two remote servers and that the server the client is connected to is in
  425.      * passive mode, requiring the other server to connect to the first server's data port to initiate a data transfer.
  426.      */
  427.     public static final int PASSIVE_REMOTE_DATA_CONNECTION_MODE = 3;

  428.     /** Pattern for PASV mode responses. Groups: (n,n,n,n),(n),(n) */
  429.     private static final java.util.regex.Pattern PARMS_PAT;

  430.     static {
  431.         PARMS_PAT = java.util.regex.Pattern.compile("(\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3}),(\\d{1,3}),(\\d{1,3})");
  432.     }

  433.     private static Properties getOverrideProperties() {
  434.         return PropertiesSingleton.PROPERTIES;
  435.     }

  436.     /**
  437.      * Parse the pathname from a CWD reply.
  438.      * <p>
  439.      * According to <a href="http://www.ietf.org/rfc/rfc959.txt">RFC959</a>, it should be the same as for MKD i.e.
  440.      * {@code 257<space>"<directory-name>"[<space>commentary]} where any double-quotes in {@code <directory-name>} are doubled. Unlike MKD, the commentary is
  441.      * optional.
  442.      * </p>
  443.      * <p>
  444.      * However, see NET-442 for an exception.
  445.      * </p>
  446.      *
  447.      * @param reply
  448.      * @return the pathname, without enclosing quotes, or the full string after the reply code and space if the syntax is invalid (i.e. enclosing quotes are
  449.      *         missing or embedded quotes are not doubled)
  450.      */
  451.     // package protected for access by test cases
  452.     static String parsePathname(final String reply) {
  453.         final String param = reply.substring(REPLY_CODE_LEN + 1);
  454.         if (param.startsWith("\"")) {
  455.             final StringBuilder sb = new StringBuilder();
  456.             boolean quoteSeen = false;
  457.             // start after initial quote
  458.             for (int i = 1; i < param.length(); i++) {
  459.                 final char ch = param.charAt(i);
  460.                 if (ch == '"') {
  461.                     if (quoteSeen) {
  462.                         sb.append(ch);
  463.                         quoteSeen = false;
  464.                     } else {
  465.                         // don't output yet, in case doubled
  466.                         quoteSeen = true;
  467.                     }
  468.                 } else {
  469.                     if (quoteSeen) { // found lone trailing quote within string
  470.                         return sb.toString();
  471.                     }
  472.                     sb.append(ch); // just another character
  473.                 }
  474.             }
  475.             if (quoteSeen) { // found lone trailing quote at end of string
  476.                 return sb.toString();
  477.             }
  478.         }
  479.         // malformed reply, return all after reply code and space
  480.         return param;
  481.     }

  482.     private int dataConnectionMode;
  483.     private Duration dataTimeout;

  484.     private int passivePort;
  485.     private String passiveHost;
  486.     private final Random random;
  487.     private int activeMinPort;
  488.     private int activeMaxPort;
  489.     private InetAddress activeExternalHost;

  490.     /** Overrides __activeExternalHost in EPRT/PORT commands. */
  491.     private InetAddress reportActiveExternalHost;

  492.     /** The address to bind to on passive connections, if necessary. */
  493.     private InetAddress passiveLocalHost;
  494.     private int fileType;
  495.     @SuppressWarnings("unused") // fields are written, but currently not read
  496.     private int fileFormat;
  497.     @SuppressWarnings("unused") // field is written, but currently not read
  498.     private int fileStructure;
  499.     private int fileTransferMode;

  500.     private boolean remoteVerificationEnabled;

  501.     private long restartOffset;

  502.     private FTPFileEntryParserFactory parserFactory;

  503.     private int bufferSize; // for buffered data streams

  504.     private int sendDataSocketBufferSize;

  505.     private int receiveDataSocketBufferSize;

  506.     private boolean listHiddenFiles;

  507.     private boolean useEPSVwithIPv4; // whether to attempt EPSV with an IPv4 connection

  508.     // __systemName is a cached value that should not be referenced directly
  509.     // except when assigned in getSystemName and __initDefaults.
  510.     private String systemName;

  511.     // __entryParser is a cached value that should not be referenced directly
  512.     // except when assigned in listFiles(String, String) and __initDefaults.
  513.     private FTPFileEntryParser entryParser;

  514.     // Key used to create the parser; necessary to ensure that the parser type is not ignored
  515.     private String entryParserKey;

  516.     private FTPClientConfig configuration;

  517.     // Listener used by store/retrieve methods to handle keepalive
  518.     private CopyStreamListener copyStreamListener;

  519.     // How long to wait before sending another control keep-alive message
  520.     private Duration controlKeepAliveTimeout = Duration.ZERO;

  521.     // How long to wait for keepalive message replies before continuing
  522.     // Most FTP servers don't seem to support concurrent control and data connection usage
  523.     private Duration controlKeepAliveReplyTimeout = Duration.ofSeconds(1);

  524.     // Debug counts for NOOP acks
  525.     private int[] cslDebug;

  526.     /**
  527.      * Enable or disable replacement of internal IP in passive mode. Default enabled using {code NatServerResolverImpl}.
  528.      */
  529.     private HostnameResolver passiveNatWorkaroundStrategy = new NatServerResolverImpl(this);

  530.     /** Controls the automatic server encoding detection (only UTF-8 supported). */
  531.     private boolean autodetectEncoding;

  532.     /** Map of FEAT responses. If null, has not been initialized. */
  533.     private HashMap<String, Set<String>> featuresMap;

  534.     private boolean ipAddressFromPasvResponse = Boolean.getBoolean(FTPClient.FTP_IP_ADDRESS_FROM_PASV_RESPONSE);

  535.     /**
  536.      * Default FTPClient constructor. Creates a new FTPClient instance with the data connection mode set to <code>ACTIVE_LOCAL_DATA_CONNECTION_MODE</code>,
  537.      * the file type set to <code>FTP.ASCII_FILE_TYPE</code>, the file format set to <code>FTP.NON_PRINT_TEXT_FORMAT</code>, the file structure set to
  538.      * <code>FTP.FILE_STRUCTURE</code>, and the transfer mode set to <code>FTP.STREAM_TRANSFER_MODE</code>.
  539.      * <p>
  540.      * The list parsing auto-detect feature can be configured to use lenient future dates (short dates may be up to one day in the future) as follows:
  541.      * </p>
  542.      *
  543.      * <pre>
  544.      * FTPClient ftp = new FTPClient();
  545.      * FTPClientConfig config = new FTPClientConfig();
  546.      * config.setLenientFutureDates(true);
  547.      * ftp.configure(config);
  548.      * </pre>
  549.      */
  550.     public FTPClient() {
  551.         initDefaults();
  552.         dataTimeout = Duration.ofMillis(-1);
  553.         remoteVerificationEnabled = true;
  554.         parserFactory = new DefaultFTPFileEntryParserFactory();
  555.         configuration = null;
  556.         listHiddenFiles = false;
  557.         useEPSVwithIPv4 = false;
  558.         random = new Random();
  559.         passiveLocalHost = null;
  560.     }

  561.     @Override
  562.     protected void _connectAction_() throws IOException {
  563.         _connectAction_(null);
  564.     }

  565.     /**
  566.      * @param socketIsReader the reader to reuse (if non-null)
  567.      * @throws IOException on error
  568.      * @since 3.4
  569.      */
  570.     @Override
  571.     protected void _connectAction_(final Reader socketIsReader) throws IOException {
  572.         super._connectAction_(socketIsReader); // sets up _input_ and _output_
  573.         initDefaults();
  574.         // must be after super._connectAction_(), because otherwise we get an
  575.         // Exception claiming we're not connected
  576.         if (autodetectEncoding) {
  577.             final ArrayList<String> oldReplyLines = new ArrayList<>(_replyLines);
  578.             final int oldReplyCode = _replyCode;
  579.             // UTF-8 appears to be the default
  580.             if (hasFeature("UTF8") || hasFeature(StandardCharsets.UTF_8.name())) {
  581.                 setControlEncoding(StandardCharsets.UTF_8.name());
  582.                 _controlInput_ = new CRLFLineReader(new InputStreamReader(_input_, getControlEncoding()));
  583.                 _controlOutput_ = new BufferedWriter(new OutputStreamWriter(_output_, getControlEncoding()));
  584.             }
  585.             // restore the original reply (server greeting)
  586.             _replyLines.clear();
  587.             _replyLines.addAll(oldReplyLines);
  588.             _replyCode = oldReplyCode;
  589.             _newReplyString = true;
  590.         }
  591.     }

  592.     /**
  593.      * Establishes a data connection with the FTP server, returning a Socket for the connection if successful. If a restart offset has been set with
  594.      * {@link #setRestartOffset(long)}, a REST command is issued to the server with the offset as an argument before establishing the data connection. Active
  595.      * mode connections also cause a local PORT command to be issued.
  596.      *
  597.      * @param command The int representation of the FTP command to send.
  598.      * @param arg     The arguments to the FTP command. If this parameter is set to null, then the command is sent with no argument.
  599.      * @return A Socket corresponding to the established data connection. Null is returned if an FTP protocol error is reported at any point during the
  600.      *         establishment and initialization of the connection.
  601.      * @throws IOException If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  602.      * @since 3.3
  603.      */
  604.     protected Socket _openDataConnection_(final FTPCmd command, final String arg) throws IOException {
  605.         return _openDataConnection_(command.getCommand(), arg);
  606.     }

  607.     /**
  608.      * Establishes a data connection with the FTP server, returning a Socket for the connection if successful. If a restart offset has been set with
  609.      * {@link #setRestartOffset(long)}, a REST command is issued to the server with the offset as an argument before establishing the data connection. Active
  610.      * mode connections also cause a local PORT command to be issued.
  611.      *
  612.      * @deprecated (3.3) Use {@link #_openDataConnection_(FTPCmd, String)} instead
  613.      * @param command The int representation of the FTP command to send.
  614.      * @param arg     The arguments to the FTP command. If this parameter is set to null, then the command is sent with no argument.
  615.      * @return A Socket corresponding to the established data connection. Null is returned if an FTP protocol error is reported at any point during the
  616.      *         establishment and initialization of the connection.
  617.      * @throws IOException If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  618.      */
  619.     @Deprecated
  620.     protected Socket _openDataConnection_(final int command, final String arg) throws IOException {
  621.         return _openDataConnection_(FTPCommand.getCommand(command), arg);
  622.     }

  623.     /**
  624.      * Establishes a data connection with the FTP server, returning a Socket for the connection if successful. If a restart offset has been set with
  625.      * {@link #setRestartOffset(long)}, a REST command is issued to the server with the offset as an argument before establishing the data connection. Active
  626.      * mode connections also cause a local PORT command to be issued.
  627.      *
  628.      * @param command The text representation of the FTP command to send.
  629.      * @param arg     The arguments to the FTP command. If this parameter is set to null, then the command is sent with no argument.
  630.      * @return A Socket corresponding to the established data connection. Null is returned if an FTP protocol error is reported at any point during the
  631.      *         establishment and initialization of the connection.
  632.      * @throws IOException If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  633.      * @since 3.1
  634.      */
  635.     protected Socket _openDataConnection_(final String command, final String arg) throws IOException {
  636.         if (dataConnectionMode != ACTIVE_LOCAL_DATA_CONNECTION_MODE && dataConnectionMode != PASSIVE_LOCAL_DATA_CONNECTION_MODE) {
  637.             return null;
  638.         }
  639.         final boolean isInet6Address = getRemoteAddress() instanceof Inet6Address;
  640.         final Socket socket;
  641.         final int soTimeoutMillis = DurationUtils.toMillisInt(dataTimeout);
  642.         if (dataConnectionMode == ACTIVE_LOCAL_DATA_CONNECTION_MODE) {
  643.             // if no activePortRange was set (correctly) -> getActivePort() = 0
  644.             // -> new ServerSocket(0) -> bind to any free local port
  645.             try (final ServerSocket server = _serverSocketFactory_.createServerSocket(getActivePort(), 1, getHostAddress())) {
  646.                 // Try EPRT only if remote server is over IPv6, if not use PORT,
  647.                 // because EPRT has no advantage over PORT on IPv4.
  648.                 // It could even have the disadvantage,
  649.                 // that EPRT will make the data connection fail, because
  650.                 // today's intelligent NAT Firewalls are able to
  651.                 // substitute IP addresses in the PORT command,
  652.                 // but might not be able to recognize the EPRT command.
  653.                 if (isInet6Address) {
  654.                     if (!FTPReply.isPositiveCompletion(eprt(getReportHostAddress(), server.getLocalPort()))) {
  655.                         return null;
  656.                     }
  657.                 } else if (!FTPReply.isPositiveCompletion(port(getReportHostAddress(), server.getLocalPort()))) {
  658.                     return null;
  659.                 }
  660.                 if (restartOffset > 0 && !restart(restartOffset)) {
  661.                     return null;
  662.                 }
  663.                 if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
  664.                     return null;
  665.                 }
  666.                 // For now, let's just use the data timeout value for waiting for
  667.                 // the data connection. It may be desirable to let this be a
  668.                 // separately configurable value. In any case, we really want
  669.                 // to allow preventing the accept from blocking indefinitely.
  670.                 if (soTimeoutMillis >= 0) {
  671.                     server.setSoTimeout(soTimeoutMillis);
  672.                 }
  673.                 socket = wrapOnDeflate(server.accept());
  674.                 // Ensure the timeout is set before any commands are issued on the new socket
  675.                 if (soTimeoutMillis >= 0) {
  676.                     socket.setSoTimeout(soTimeoutMillis);
  677.                 }
  678.                 if (receiveDataSocketBufferSize > 0) {
  679.                     socket.setReceiveBufferSize(receiveDataSocketBufferSize);
  680.                 }
  681.                 if (sendDataSocketBufferSize > 0) {
  682.                     socket.setSendBufferSize(sendDataSocketBufferSize);
  683.                 }
  684.             }
  685.         } else {
  686.             // We must be in PASSIVE_LOCAL_DATA_CONNECTION_MODE
  687.             // Try EPSV command first on IPv6 - and IPv4 if enabled.
  688.             // When using IPv4 with NAT it has the advantage
  689.             // to work with more rare configurations.
  690.             // E.g. if FTP server has a static PASV address (external network)
  691.             // and the client is coming from another internal network.
  692.             // In that case the data connection after PASV command would fail,
  693.             // while EPSV would make the client succeed by taking just the port.
  694.             final boolean attemptEPSV = isUseEPSVwithIPv4() || isInet6Address;
  695.             if (attemptEPSV && epsv() == FTPReply.ENTERING_EPSV_MODE) {
  696.                 _parseExtendedPassiveModeReply(_replyLines.get(0));
  697.             } else {
  698.                 if (isInet6Address) {
  699.                     return null; // Must use EPSV for IPV6
  700.                 }
  701.                 // If EPSV failed on IPV4, revert to PASV
  702.                 if (pasv() != FTPReply.ENTERING_PASSIVE_MODE) {
  703.                     return null;
  704.                 }
  705.                 _parsePassiveModeReply(_replyLines.get(0));
  706.             }
  707.             socket = wrapOnDeflate(_socketFactory_.createSocket());
  708.             if (receiveDataSocketBufferSize > 0) {
  709.                 socket.setReceiveBufferSize(receiveDataSocketBufferSize);
  710.             }
  711.             if (sendDataSocketBufferSize > 0) {
  712.                 socket.setSendBufferSize(sendDataSocketBufferSize);
  713.             }
  714.             if (passiveLocalHost != null) {
  715.                 socket.bind(new InetSocketAddress(passiveLocalHost, 0));
  716.             }
  717.             // For now, let's just use the data timeout value for waiting for
  718.             // the data connection. It may be desirable to let this be a
  719.             // separately configurable value. In any case, we really want
  720.             // to allow preventing the accept from blocking indefinitely.
  721.             if (soTimeoutMillis >= 0) {
  722.                 socket.setSoTimeout(soTimeoutMillis);
  723.             }
  724.             socket.connect(new InetSocketAddress(passiveHost, passivePort), connectTimeout);
  725.             if (restartOffset > 0 && !restart(restartOffset)) {
  726.                 socket.close();
  727.                 return null;
  728.             }
  729.             if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
  730.                 socket.close();
  731.                 return null;
  732.             }
  733.         }
  734.         if (remoteVerificationEnabled && !verifyRemote(socket)) {
  735.             // Grab the host before we close the socket to avoid NET-663
  736.             final InetAddress socketHost = socket.getInetAddress();
  737.             socket.close();
  738.             throw new IOException(
  739.                     "Host attempting data connection " + socketHost.getHostAddress() + " is not same as server " + getRemoteAddress().getHostAddress());
  740.         }
  741.         return socket;
  742.     }

  743.     protected void _parseExtendedPassiveModeReply(String reply) throws MalformedServerReplyException {
  744.         reply = reply.substring(reply.indexOf('(') + 1, reply.indexOf(')')).trim();
  745.         final char delim1 = reply.charAt(0);
  746.         final char delim2 = reply.charAt(1);
  747.         final char delim3 = reply.charAt(2);
  748.         final char delim4 = reply.charAt(reply.length() - 1);
  749.         if (delim1 != delim2 || delim2 != delim3 || delim3 != delim4) {
  750.             throw new MalformedServerReplyException("Could not parse extended passive host information.\nServer Reply: " + reply);
  751.         }
  752.         final int port;
  753.         try {
  754.             port = Integer.parseInt(reply.substring(3, reply.length() - 1));
  755.         } catch (final NumberFormatException e) {
  756.             throw new MalformedServerReplyException("Could not parse extended passive host information.\nServer Reply: " + reply);
  757.         }
  758.         // in EPSV mode, the passive host address is implicit
  759.         this.passiveHost = getRemoteAddress().getHostAddress();
  760.         this.passivePort = port;
  761.     }

  762.     /**
  763.      * @since 3.1
  764.      * @param reply the reply to parse
  765.      * @throws MalformedServerReplyException if the server reply does not match (n,n,n,n),(n),(n)
  766.      */
  767.     protected void _parsePassiveModeReply(final String reply) throws MalformedServerReplyException {
  768.         final Matcher m = PARMS_PAT.matcher(reply);
  769.         if (!m.find()) {
  770.             throw new MalformedServerReplyException("Could not parse passive host information.\nServer Reply: " + reply);
  771.         }
  772.         int pasvPort;
  773.         // Fix up to look like IP address
  774.         String pasvHost = "0,0,0,0".equals(m.group(1)) ? _socket_.getInetAddress().getHostAddress() : m.group(1).replace(',', '.');
  775.         try {
  776.             final int oct1 = Integer.parseInt(m.group(2));
  777.             final int oct2 = Integer.parseInt(m.group(3));
  778.             pasvPort = oct1 << 8 | oct2;
  779.         } catch (final NumberFormatException e) {
  780.             throw new MalformedServerReplyException("Could not parse passive port information.\nServer Reply: " + reply);
  781.         }
  782.         if (isIpAddressFromPasvResponse()) {
  783.             // Pre-3.9.0 behavior
  784.             if (passiveNatWorkaroundStrategy != null) {
  785.                 try {
  786.                     final String newPassiveHost = passiveNatWorkaroundStrategy.resolve(pasvHost);
  787.                     if (!pasvHost.equals(newPassiveHost)) {
  788.                         fireReplyReceived(0, "[Replacing PASV mode reply address " + this.passiveHost + " with " + newPassiveHost + "]\n");
  789.                         pasvHost = newPassiveHost;
  790.                     }
  791.                 } catch (final UnknownHostException e) { // Should not happen as we are passing in an IP address
  792.                     throw new MalformedServerReplyException("Could not parse passive host information.\nServer Reply: " + reply);
  793.                 }
  794.             }
  795.         } else if (_socket_ == null) {
  796.             pasvHost = null; // For unit testing.
  797.         } else {
  798.             pasvHost = _socket_.getInetAddress().getHostAddress();
  799.         }
  800.         this.passiveHost = pasvHost;
  801.         this.passivePort = pasvPort;
  802.     }

  803.     /**
  804.      * @param command the command to get
  805.      * @param remote  the remote file name
  806.      * @param local   The local OutputStream to which to write the file.
  807.      * @return true if successful
  808.      * @throws IOException on error
  809.      * @since 3.1
  810.      */
  811.     protected boolean _retrieveFile(final String command, final String remote, final OutputStream local) throws IOException {
  812.         final Socket socket = _openDataConnection_(command, remote);
  813.         if (socket == null) {
  814.             return false;
  815.         }
  816.         InputStream input = null;
  817.         CSL csl = null;
  818.         try {
  819.             try {
  820.                 if (fileType == ASCII_FILE_TYPE) {
  821.                     input = new FromNetASCIIInputStream(getBufferedInputStream(socket.getInputStream()));
  822.                 } else {
  823.                     input = getBufferedInputStream(socket.getInputStream());
  824.                 }

  825.                 if (DurationUtils.isPositive(controlKeepAliveTimeout)) {
  826.                     csl = new CSL(this, controlKeepAliveTimeout, controlKeepAliveReplyTimeout);
  827.                 }

  828.                 // Treat everything else as binary for now
  829.                 Util.copyStream(input, local, getBufferSize(), CopyStreamEvent.UNKNOWN_STREAM_SIZE, mergeListeners(csl), false);
  830.             } finally {
  831.                 Util.closeQuietly(input);
  832.             }
  833.             // Get the transfer response
  834.             return completePendingCommand();
  835.         } finally {
  836.             Util.closeQuietly(socket);
  837.             if (csl != null) {
  838.                 cslDebug = csl.cleanUp(); // fetch any outstanding keepalive replies
  839.             }
  840.         }
  841.     }

  842.     /**
  843.      * @param command the command to send
  844.      * @param remote  the remote file name
  845.      * @return the stream from which to read the file
  846.      * @throws IOException on error
  847.      * @since 3.1
  848.      */
  849.     protected InputStream _retrieveFileStream(final String command, final String remote) throws IOException {
  850.         final Socket socket = _openDataConnection_(command, remote);
  851.         if (socket == null) {
  852.             return null;
  853.         }
  854.         final InputStream input;
  855.         if (fileType == ASCII_FILE_TYPE) {
  856.             // We buffer ascii transfers because the buffering has to
  857.             // be interposed between FromNetASCIIOutputSream and the underlying
  858.             // socket input stream. We don't buffer binary transfers
  859.             // because we don't want to impose a buffering policy on the
  860.             // programmer if possible. Programmers can decide on their
  861.             // own if they want to wrap the SocketInputStream we return
  862.             // for file types other than ASCII.
  863.             input = new FromNetASCIIInputStream(getBufferedInputStream(socket.getInputStream()));
  864.         } else {
  865.             input = socket.getInputStream();
  866.         }
  867.         return new org.apache.commons.net.io.SocketInputStream(socket, input);
  868.     }

  869.     /**
  870.      * @since 3.1
  871.      * @param command the command to send
  872.      * @param remote  the remote file name
  873.      * @param local   The local InputStream from which to read the data to be written/appended to the remote file.
  874.      * @return true if successful
  875.      * @throws IOException on error
  876.      */
  877.     protected boolean _storeFile(final String command, final String remote, final InputStream local) throws IOException {
  878.         final Socket socket = _openDataConnection_(command, remote);
  879.         if (socket == null) {
  880.             return false;
  881.         }
  882.         final OutputStream output;
  883.         if (fileType == ASCII_FILE_TYPE) {
  884.             output = new ToNetASCIIOutputStream(getBufferedOutputStream(socket.getOutputStream()));
  885.         } else {
  886.             output = getBufferedOutputStream(socket.getOutputStream());
  887.         }
  888.         CSL csl = null;
  889.         if (DurationUtils.isPositive(controlKeepAliveTimeout)) {
  890.             csl = new CSL(this, controlKeepAliveTimeout, controlKeepAliveReplyTimeout);
  891.         }
  892.         // Treat everything else as binary for now
  893.         try {
  894.             Util.copyStream(local, output, getBufferSize(), CopyStreamEvent.UNKNOWN_STREAM_SIZE, mergeListeners(csl), false);
  895.             output.close(); // ensure the file is fully written
  896.             socket.close(); // done writing the file
  897.             // Get the transfer response
  898.             return completePendingCommand();
  899.         } catch (final IOException e) {
  900.             Util.closeQuietly(output); // ignore close errors here
  901.             Util.closeQuietly(socket); // ignore close errors here
  902.             throw e;
  903.         } finally {
  904.             if (csl != null) {
  905.                 cslDebug = csl.cleanUp(); // fetch any outstanding keepalive replies
  906.             }
  907.         }
  908.     }

  909.     /**
  910.      * @param command the command to send
  911.      * @param remote  the remote file name
  912.      * @return the output stream to write to
  913.      * @throws IOException on error
  914.      * @since 3.1
  915.      */
  916.     protected OutputStream _storeFileStream(final String command, final String remote) throws IOException {
  917.         final Socket socket = _openDataConnection_(command, remote);
  918.         if (socket == null) {
  919.             return null;
  920.         }
  921.         final OutputStream output;
  922.         if (fileType == ASCII_FILE_TYPE) {
  923.             // We buffer ascii transfers because the buffering has to
  924.             // be interposed between ToNetASCIIOutputSream and the underlying
  925.             // socket output stream. We don't buffer binary transfers
  926.             // because we don't want to impose a buffering policy on the
  927.             // programmer if possible. Programmers can decide on their
  928.             // own if they want to wrap the SocketOutputStream we return
  929.             // for file types other than ASCII.
  930.             output = new ToNetASCIIOutputStream(getBufferedOutputStream(socket.getOutputStream()));
  931.         } else {
  932.             output = socket.getOutputStream();
  933.         }
  934.         return new SocketOutputStream(socket, output);
  935.     }

  936.     /**
  937.      * Abort a transfer in progress.
  938.      *
  939.      * @return True if successfully completed, false if not.
  940.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  941.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  942.      *                                      independently as itself.
  943.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  944.      */
  945.     public boolean abort() throws IOException {
  946.         return FTPReply.isPositiveCompletion(abor());
  947.     }

  948.     /**
  949.      * Reserve a number of bytes on the server for the next file transfer.
  950.      *
  951.      * @param bytes The number of bytes which the server should allocate.
  952.      * @return True if successfully completed, false if not.
  953.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  954.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  955.      *                                      independently as itself.
  956.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  957.      */
  958.     public boolean allocate(final int bytes) throws IOException {
  959.         return FTPReply.isPositiveCompletion(allo(bytes));
  960.     }

  961.     /**
  962.      * Reserve space on the server for the next file transfer.
  963.      *
  964.      * @param bytes      The number of bytes which the server should allocate.
  965.      * @param recordSize The size of a file record.
  966.      * @return True if successfully completed, false if not.
  967.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  968.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  969.      *                                      independently as itself.
  970.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  971.      */
  972.     public boolean allocate(final int bytes, final int recordSize) throws IOException {
  973.         return FTPReply.isPositiveCompletion(allo(bytes, recordSize));
  974.     }

  975.     /**
  976.      * Reserve a number of bytes on the server for the next file transfer.
  977.      *
  978.      * @param bytes The number of bytes which the server should allocate.
  979.      * @return True if successfully completed, false if not.
  980.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  981.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  982.      *                                      independently as itself.
  983.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  984.      */
  985.     public boolean allocate(final long bytes) throws IOException {
  986.         return FTPReply.isPositiveCompletion(allo(bytes));
  987.     }

  988.     /**
  989.      * Reserve space on the server for the next file transfer.
  990.      *
  991.      * @param bytes      The number of bytes which the server should allocate.
  992.      * @param recordSize The size of a file record.
  993.      * @return True if successfully completed, false if not.
  994.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  995.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  996.      *                                      independently as itself.
  997.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  998.      */
  999.     public boolean allocate(final long bytes, final int recordSize) throws IOException {
  1000.         return FTPReply.isPositiveCompletion(allo(bytes, recordSize));
  1001.     }

  1002.     /**
  1003.      * Appends to a file on the server with the given name, taking input from the given InputStream. This method does NOT close the given InputStream. If the
  1004.      * current file type is ASCII, line separators in the file are transparently converted to the NETASCII format (i.e., you should not attempt to create a
  1005.      * special InputStream to do this).
  1006.      *
  1007.      * @param remote The name of the remote file.
  1008.      * @param local  The local InputStream from which to read the data to be appended to the remote file.
  1009.      * @return True if successfully completed, false if not.
  1010.      * @throws FTPConnectionClosedException                  If the FTP server prematurely closes the connection as a result of the client being idle or some
  1011.      *                                                       other reason causing the server to send FTP reply code 421. This exception may be caught either as
  1012.      *                                                       an IOException or independently as itself.
  1013.      * @throws org.apache.commons.net.io.CopyStreamException If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to
  1014.      *                                                       determine the number of bytes transferred and the IOException causing the error. This exception may
  1015.      *                                                       be caught either as an IOException or independently as itself.
  1016.      * @throws IOException                                   If an I/O error occurs while either sending a command to the server or receiving a reply from the
  1017.      *                                                       server.
  1018.      */
  1019.     public boolean appendFile(final String remote, final InputStream local) throws IOException {
  1020.         return storeFile(FTPCmd.APPE, remote, local);
  1021.     }

  1022.     /**
  1023.      * Returns an OutputStream through which data can be written to append to a file on the server with the given name. If the current file type is ASCII, the
  1024.      * returned OutputStream will convert line separators in the file to the NETASCII format (i.e., you should not attempt to create a special OutputStream to
  1025.      * do this). You must close the OutputStream when you finish writing to it. The OutputStream itself will take care of closing the parent data connection
  1026.      * socket upon being closed.
  1027.      * <p>
  1028.      * <b>To finalize the file transfer you must call {@link #completePendingCommand completePendingCommand } and check its return value to verify success.</b>
  1029.      * If this is not done, subsequent commands may behave unexpectedly.
  1030.      * </p>
  1031.      *
  1032.      * @param remote The name of the remote file.
  1033.      * @return An OutputStream through which the remote file can be appended. If the data connection cannot be opened (e.g., the file does not exist), null is
  1034.      *         returned (in which case you may check the reply code to determine the exact reason for failure).
  1035.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  1036.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  1037.      *                                      independently as itself.
  1038.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1039.      */
  1040.     public OutputStream appendFileStream(final String remote) throws IOException {
  1041.         return storeFileStream(FTPCmd.APPE, remote);
  1042.     }

  1043.     /**
  1044.      * Change to the parent directory of the current working directory.
  1045.      *
  1046.      * @return True if successfully completed, false if not.
  1047.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  1048.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  1049.      *                                      independently as itself.
  1050.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1051.      */
  1052.     public boolean changeToParentDirectory() throws IOException {
  1053.         return FTPReply.isPositiveCompletion(cdup());
  1054.     }

  1055.     /**
  1056.      * Change the current working directory of the FTP session.
  1057.      *
  1058.      * @param pathname The new current working directory.
  1059.      * @return True if successfully completed, false if not.
  1060.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  1061.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  1062.      *                                      independently as itself.
  1063.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1064.      */
  1065.     public boolean changeWorkingDirectory(final String pathname) throws IOException {
  1066.         return FTPReply.isPositiveCompletion(cwd(pathname));
  1067.     }

  1068.     /**
  1069.      * There are a few FTPClient methods that do not complete the entire sequence of FTP commands to complete a transaction. These commands require some action
  1070.      * by the programmer after the reception of a positive intermediate command. After the programmer's code completes its actions, it must call this method to
  1071.      * receive the completion reply from the server and verify the success of the entire transaction.
  1072.      * <p>
  1073.      * For example,
  1074.      * </p>
  1075.      *
  1076.      * <pre>
  1077.      * InputStream input;
  1078.      * OutputStream output;
  1079.      * input  = new FileInputStream("foobaz.txt");
  1080.      * output = ftp.storeFileStream("foobar.txt")
  1081.      * if (!FTPReply.isPositiveIntermediate(ftp.getReplyCode())) {
  1082.      *     input.close();
  1083.      *     output.close();
  1084.      *     ftp.logout();
  1085.      *     ftp.disconnect();
  1086.      *     System.err.println("File transfer failed.");
  1087.      *     System.exit(1);
  1088.      * }
  1089.      * Util.copyStream(input, output);
  1090.      * input.close();
  1091.      * output.close();
  1092.      * // Must call completePendingCommand() to finish command.
  1093.      * if (!ftp.completePendingCommand()) {
  1094.      *     ftp.logout();
  1095.      *     ftp.disconnect();
  1096.      *     System.err.println("File transfer failed.");
  1097.      *     System.exit(1);
  1098.      * }
  1099.      * </pre>
  1100.      *
  1101.      * @return True if successfully completed, false if not.
  1102.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  1103.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  1104.      *                                      independently as itself.
  1105.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1106.      */
  1107.     public boolean completePendingCommand() throws IOException {
  1108.         return FTPReply.isPositiveCompletion(getReply());
  1109.     }

  1110.     /**
  1111.      * Implements the {@link Configurable} interface. In the case of this class, configuring merely makes the config object available for
  1112.      * the factory methods that construct parsers.
  1113.      *
  1114.      * @param config {@link FTPClientConfig} object used to provide non-standard configurations to the parser.
  1115.      * @since 1.4
  1116.      */
  1117.     @Override
  1118.     public void configure(final FTPClientConfig config) {
  1119.         this.configuration = config;
  1120.     }

  1121.     // package access for test purposes
  1122.     void createParser(final String parserKey) throws IOException {
  1123.         // We cache the value to avoid creation of a new object every
  1124.         // time a file listing is generated.
  1125.         // Note: we don't check against a null parserKey (NET-544)
  1126.         if (entryParser == null || parserKey != null && !entryParserKey.equals(parserKey)) {
  1127.             if (null != parserKey) {
  1128.                 // if a parser key was supplied in the parameters,
  1129.                 // use that to create the parser
  1130.                 entryParser = parserFactory.createFileEntryParser(parserKey);
  1131.                 entryParserKey = parserKey;

  1132.             } else // if no parserKey was supplied, check for a configuration
  1133.             // in the params, and if it has a non-empty system type, use that.
  1134.             if (null != configuration && configuration.getServerSystemKey().length() > 0) {
  1135.                 entryParser = parserFactory.createFileEntryParser(configuration);
  1136.                 entryParserKey = configuration.getServerSystemKey();
  1137.             } else {
  1138.                 // if a parserKey hasn't been supplied, and a configuration
  1139.                 // hasn't been supplied, and the override property is not set
  1140.                 // then autodetect by calling
  1141.                 // the SYST command and use that to choose the parser.
  1142.                 String systemType = System.getProperty(FTP_SYSTEM_TYPE);
  1143.                 if (systemType == null) {
  1144.                     systemType = getSystemType(); // cannot be null
  1145.                     final Properties override = getOverrideProperties();
  1146.                     if (override != null) {
  1147.                         final String newType = override.getProperty(systemType);
  1148.                         if (newType != null) {
  1149.                             systemType = newType;
  1150.                         }
  1151.                     }
  1152.                 }
  1153.                 if (null != configuration) { // system type must have been empty above
  1154.                     entryParser = parserFactory.createFileEntryParser(new FTPClientConfig(systemType, configuration));
  1155.                 } else {
  1156.                     entryParser = parserFactory.createFileEntryParser(systemType);
  1157.                 }
  1158.                 entryParserKey = systemType;
  1159.             }
  1160.         }
  1161.     }

  1162.     /**
  1163.      * Deletes a file on the FTP server.
  1164.      *
  1165.      * @param pathname The pathname of the file to be deleted.
  1166.      * @return True if successfully completed, false if not.
  1167.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  1168.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  1169.      *                                      independently as itself.
  1170.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1171.      */
  1172.     public boolean deleteFile(final String pathname) throws IOException {
  1173.         return FTPReply.isPositiveCompletion(dele(pathname));
  1174.     }

  1175.     /**
  1176.      * Closes the connection to the FTP server and restores connection parameters to the default values.
  1177.      *
  1178.      * @throws IOException If an error occurs while disconnecting.
  1179.      */
  1180.     @Override
  1181.     public void disconnect() throws IOException {
  1182.         super.disconnect();
  1183.         initDefaults();
  1184.     }

  1185.     /**
  1186.      * Issue a command and wait for the reply.
  1187.      * <p>
  1188.      * Should only be used with commands that return replies on the command channel - do not use for LIST, NLST, MLSD etc.
  1189.      *
  1190.      * @param command The command to invoke
  1191.      * @param params  The parameters string, may be {@code null}
  1192.      * @return True if successfully completed, false if not, in which case call {@link #getReplyCode()} or {@link #getReplyString()} to get the reason.
  1193.      *
  1194.      * @throws IOException If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1195.      * @since 3.0
  1196.      */
  1197.     public boolean doCommand(final String command, final String params) throws IOException {
  1198.         return FTPReply.isPositiveCompletion(sendCommand(command, params));
  1199.     }

  1200.     /**
  1201.      * Issue a command and wait for the reply, returning it as an array of strings.
  1202.      * <p>
  1203.      * Should only be used with commands that return replies on the command channel - do not use for LIST, NLST, MLSD etc.
  1204.      * </p>
  1205.      *
  1206.      * @param command The command to invoke
  1207.      * @param params  The parameters string, may be {@code null}
  1208.      * @return The array of replies, or {@code null} if the command failed, in which case call {@link #getReplyCode()} or {@link #getReplyString()} to get the
  1209.      *         reason.
  1210.      *
  1211.      * @throws IOException If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1212.      * @since 3.0
  1213.      */
  1214.     public String[] doCommandAsStrings(final String command, final String params) throws IOException {
  1215.         final boolean success = FTPReply.isPositiveCompletion(sendCommand(command, params));
  1216.         if (success) {
  1217.             return getReplyStrings();
  1218.         }
  1219.         return null;
  1220.     }

  1221.     /**
  1222.      * Sets the current data connection mode to <code>ACTIVE_LOCAL_DATA_CONNECTION_MODE</code>. No communication with the FTP server is conducted, but this
  1223.      * causes all future data transfers to require the FTP server to connect to the client's data port. Additionally, to accommodate differences between socket
  1224.      * implementations on different platforms, this method causes the client to issue a PORT command before every data transfer.
  1225.      */
  1226.     public void enterLocalActiveMode() {
  1227.         dataConnectionMode = ACTIVE_LOCAL_DATA_CONNECTION_MODE;
  1228.         passiveHost = null;
  1229.         passivePort = -1;
  1230.     }

  1231.     /**
  1232.      * Sets the current data connection mode to <code>PASSIVE_LOCAL_DATA_CONNECTION_MODE</code>. Use this method only for data transfers between the client
  1233.      * and server. This method causes a PASV (or EPSV) command to be issued to the server before the opening of every data connection, telling the server to
  1234.      * open a data port to which the client will connect to conduct data transfers. The FTPClient will stay in <code>PASSIVE_LOCAL_DATA_CONNECTION_MODE</code>
  1235.      * until the mode is changed by calling some other method such as {@link #enterLocalActiveMode enterLocalActiveMode() }
  1236.      * <p>
  1237.      * <b>N.B.</b> currently calling any connect method will reset the mode to ACTIVE_LOCAL_DATA_CONNECTION_MODE.
  1238.      * </p>
  1239.      */
  1240.     public void enterLocalPassiveMode() {
  1241.         dataConnectionMode = PASSIVE_LOCAL_DATA_CONNECTION_MODE;
  1242.         // These will be set when just before a data connection is opened
  1243.         // in _openDataConnection_()
  1244.         passiveHost = null;
  1245.         passivePort = -1;
  1246.     }

  1247.     /**
  1248.      * Sets the current data connection mode to <code>ACTIVE_REMOTE_DATA_CONNECTION</code>. Use this method only for server to server data transfers. This
  1249.      * method issues a PORT command to the server, indicating the other server and port to which it should connect for data transfers. You must call this method
  1250.      * before EVERY server to server transfer attempt. The FTPClient will NOT automatically continue to issue PORT commands. You also must remember to call
  1251.      * {@link #enterLocalActiveMode enterLocalActiveMode() } if you wish to return to the normal data connection mode.
  1252.      *
  1253.      * @param host The passive mode server accepting connections for data transfers.
  1254.      * @param port The passive mode server's data port.
  1255.      * @return True if successfully completed, false if not.
  1256.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  1257.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  1258.      *                                      independently as itself.
  1259.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1260.      */
  1261.     public boolean enterRemoteActiveMode(final InetAddress host, final int port) throws IOException {
  1262.         if (FTPReply.isPositiveCompletion(port(host, port))) {
  1263.             dataConnectionMode = ACTIVE_REMOTE_DATA_CONNECTION_MODE;
  1264.             passiveHost = null;
  1265.             passivePort = -1;
  1266.             return true;
  1267.         }
  1268.         return false;
  1269.     }

  1270.     /**
  1271.      * Sets the current data connection mode to <code>PASSIVE_REMOTE_DATA_CONNECTION_MODE</code>. Use this method only for server to server data transfers.
  1272.      * This method issues a PASV command to the server, telling it to open a data port to which the active server will connect to conduct data transfers. You
  1273.      * must call this method before EVERY server to server transfer attempt. The FTPClient will NOT automatically continue to issue PASV commands. You also must
  1274.      * remember to call {@link #enterLocalActiveMode enterLocalActiveMode() } if you wish to return to the normal data connection mode.
  1275.      *
  1276.      * @return True if successfully completed, false if not.
  1277.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  1278.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  1279.      *                                      independently as itself.
  1280.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1281.      */
  1282.     public boolean enterRemotePassiveMode() throws IOException {
  1283.         if (pasv() != FTPReply.ENTERING_PASSIVE_MODE) {
  1284.             return false;
  1285.         }
  1286.         dataConnectionMode = PASSIVE_REMOTE_DATA_CONNECTION_MODE;
  1287.         _parsePassiveModeReply(_replyLines.get(0));
  1288.         return true;
  1289.     }

  1290.     /**
  1291.      * Queries the server for supported features. The server may reply with a list of server-supported extensions. For example, a typical client-server
  1292.      * interaction might be (from RFC 2389):
  1293.      *
  1294.      * <pre>
  1295.         C&gt; feat
  1296.         S&gt; 211-Extensions supported:
  1297.         S&gt;  MLST size*;create;modify*;perm;media-type
  1298.         S&gt;  SIZE
  1299.         S&gt;  COMPRESSION
  1300.         S&gt;  MDTM
  1301.         S&gt; 211 END
  1302.      * </pre>
  1303.      *
  1304.      * @see <a href="http://www.faqs.org/rfcs/rfc2389.html">http://www.faqs.org/rfcs/rfc2389.html</a>
  1305.      * @return True if successfully completed, false if not.
  1306.      * @throws IOException on error
  1307.      * @since 2.2
  1308.      */
  1309.     public boolean features() throws IOException {
  1310.         return FTPReply.isPositiveCompletion(feat());
  1311.     }

  1312.     /**
  1313.      * Queries the server for a supported feature, and returns its value (if any). Caches the parsed response to avoid resending the command repeatedly.
  1314.      *
  1315.      * @param feature the feature to check
  1316.      *
  1317.      * @return if the feature is present, returns the feature value or the empty string if the feature exists but has no value. Returns {@code null} if the
  1318.      *         feature is not found or the command failed. Check {@link #getReplyCode()} or {@link #getReplyString()} if so.
  1319.      * @throws IOException on error
  1320.      * @since 3.0
  1321.      */
  1322.     public String featureValue(final String feature) throws IOException {
  1323.         final String[] values = featureValues(feature);
  1324.         if (values != null) {
  1325.             return values[0];
  1326.         }
  1327.         return null;
  1328.     }

  1329.     /**
  1330.      * Queries the server for a supported feature, and returns its values (if any). Caches the parsed response to avoid resending the command repeatedly.
  1331.      *
  1332.      * @param feature the feature to check
  1333.      *
  1334.      * @return if the feature is present, returns the feature values (empty array if none) Returns {@code null} if the feature is not found or the command
  1335.      *         failed. Check {@link #getReplyCode()} or {@link #getReplyString()} if so.
  1336.      * @throws IOException on error
  1337.      * @since 3.0
  1338.      */
  1339.     public String[] featureValues(final String feature) throws IOException {
  1340.         if (!initFeatureMap()) {
  1341.             return null;
  1342.         }
  1343.         final Set<String> entries = featuresMap.get(feature.toUpperCase(Locale.ENGLISH));
  1344.         if (entries != null) {
  1345.             return entries.toArray(NetConstants.EMPTY_STRING_ARRAY);
  1346.         }
  1347.         return null;
  1348.     }

  1349.     /**
  1350.      * Gets the client port for active mode.
  1351.      *
  1352.      * @return The client port for active mode.
  1353.      */
  1354.     int getActivePort() {
  1355.         if (activeMinPort > 0 && activeMaxPort >= activeMinPort) {
  1356.             if (activeMaxPort == activeMinPort) {
  1357.                 return activeMaxPort;
  1358.             }
  1359.             // Get a random port between the min and max port range
  1360.             return random.nextInt(activeMaxPort - activeMinPort + 1) + activeMinPort;
  1361.         }
  1362.         // default port
  1363.         return 0;
  1364.     }

  1365.     /**
  1366.      * Tells if automatic server encoding detection is enabled or disabled.
  1367.      *
  1368.      * @return true, if automatic server encoding detection is enabled.
  1369.      */
  1370.     public boolean getAutodetectUTF8() {
  1371.         return autodetectEncoding;
  1372.     }

  1373.     private InputStream getBufferedInputStream(final InputStream inputStream) {
  1374.         if (bufferSize > 0) {
  1375.             return new BufferedInputStream(inputStream, bufferSize);
  1376.         }
  1377.         return new BufferedInputStream(inputStream);
  1378.     }

  1379.     private OutputStream getBufferedOutputStream(final OutputStream outputStream) {
  1380.         if (bufferSize > 0) {
  1381.             return new BufferedOutputStream(outputStream, bufferSize);
  1382.         }
  1383.         return new BufferedOutputStream(outputStream);
  1384.     }

  1385.     /**
  1386.      * Retrieve the current internal buffer size for buffered data streams.
  1387.      *
  1388.      * @return The current buffer size.
  1389.      */
  1390.     public int getBufferSize() {
  1391.         return bufferSize;
  1392.     }

  1393.     /**
  1394.      * Gets how long to wait for control keep-alive message replies.
  1395.      *
  1396.      * @deprecated Use {@link #getControlKeepAliveReplyTimeoutDuration()}.
  1397.      * @return wait time in milliseconds.
  1398.      * @since 3.0
  1399.      */
  1400.     @Deprecated
  1401.     public int getControlKeepAliveReplyTimeout() {
  1402.         return DurationUtils.toMillisInt(controlKeepAliveReplyTimeout);
  1403.     }

  1404.     /**
  1405.      * Gets how long to wait for control keep-alive message replies.
  1406.      *
  1407.      * @return wait time.
  1408.      * @since 3.9.0
  1409.      */
  1410.     public Duration getControlKeepAliveReplyTimeoutDuration() {
  1411.         return controlKeepAliveReplyTimeout;
  1412.     }

  1413.     /**
  1414.      * Gets the time to wait between sending control connection keepalive messages when processing file upload or download.
  1415.      * <p>
  1416.      * See the class Javadoc section "Control channel keep-alive feature"
  1417.      * </p>
  1418.      *
  1419.      * @deprecated Use {@link #getControlKeepAliveTimeoutDuration()}.
  1420.      * @return the number of seconds between keepalive messages.
  1421.      * @since 3.0
  1422.      */
  1423.     @Deprecated
  1424.     public long getControlKeepAliveTimeout() {
  1425.         return controlKeepAliveTimeout.getSeconds();
  1426.     }

  1427.     /**
  1428.      * Gets the time to wait between sending control connection keepalive messages when processing file upload or download.
  1429.      * <p>
  1430.      * See the class Javadoc section "Control channel keep-alive feature"
  1431.      * </p>
  1432.      *
  1433.      * @return the duration between keepalive messages.
  1434.      * @since 3.9.0
  1435.      */
  1436.     public Duration getControlKeepAliveTimeoutDuration() {
  1437.         return controlKeepAliveTimeout;
  1438.     }

  1439.     /**
  1440.      * Obtain the currently active listener.
  1441.      *
  1442.      * @return the listener, may be {@code null}
  1443.      * @since 3.0
  1444.      */
  1445.     public CopyStreamListener getCopyStreamListener() {
  1446.         return copyStreamListener;
  1447.     }

  1448.     /**
  1449.      * Gets the CSL debug array.
  1450.      * <p>
  1451.      * <b>For debug use only</b>
  1452.      * </p>
  1453.      * <p>
  1454.      * Currently, it contains:
  1455.      * </p>
  1456.      * <ul>
  1457.      * <li>successfully acked NOOPs at end of transfer</li>
  1458.      * <li>unanswered NOOPs at end of transfer</li>
  1459.      * <li>unanswered NOOPs after fetching additional replies</li>
  1460.      * <li>Number of IOErrors ignored</li>
  1461.      * </ul>
  1462.      *
  1463.      * @deprecated 3.7 For testing only; may be dropped or changed at any time
  1464.      * @return the debug array
  1465.      */
  1466.     @Deprecated // only for use in testing
  1467.     public int[] getCslDebug() {
  1468.         return cslDebug;
  1469.     }

  1470.     /**
  1471.      * Returns the current data connection mode (one of the <code>_DATA_CONNECTION_MODE</code> constants).
  1472.      *
  1473.      * @return The current data connection mode (one of the <code>_DATA_CONNECTION_MODE</code> constants).
  1474.      */
  1475.     public int getDataConnectionMode() {
  1476.         return dataConnectionMode;
  1477.     }

  1478.     /**
  1479.      * Gets the timeout to use when reading from the data connection. This timeout will be set immediately after opening the data connection, provided that the
  1480.      * value is &ge; 0.
  1481.      * <p>
  1482.      * <b>Note:</b> the timeout will also be applied when calling accept() whilst establishing an active local data connection.
  1483.      * </p>
  1484.      *
  1485.      * @return The default timeout used when opening a data connection socket. The value 0 means an infinite timeout.
  1486.      * @since 3.9.0
  1487.      */
  1488.     public Duration getDataTimeout() {
  1489.         return dataTimeout;
  1490.     }

  1491.     // Method for use by unit test code only
  1492.     FTPFileEntryParser getEntryParser() {
  1493.         return entryParser;
  1494.     }

  1495.     /**
  1496.      * Gets the host address for active mode; allows the local address to be overridden.
  1497.      *
  1498.      * @return __activeExternalHost if non-null, else getLocalAddress()
  1499.      * @see #setActiveExternalIPAddress(String)
  1500.      */
  1501.     InetAddress getHostAddress() {
  1502.         if (activeExternalHost != null) {
  1503.             return activeExternalHost;
  1504.         }
  1505.         // default local address
  1506.         return getLocalAddress();
  1507.     }

  1508.     /**
  1509.      * @param pathname the initial pathname
  1510.      * @return the adjusted string with "-a" added if necessary
  1511.      * @since 2.0
  1512.      */
  1513.     protected String getListArguments(final String pathname) {
  1514.         if (getListHiddenFiles()) {
  1515.             if (pathname != null) {
  1516.                 final StringBuilder sb = new StringBuilder(pathname.length() + 3);
  1517.                 sb.append("-a ");
  1518.                 sb.append(pathname);
  1519.                 return sb.toString();
  1520.             }
  1521.             return "-a";
  1522.         }
  1523.         return pathname;
  1524.     }

  1525.     /**
  1526.      * @see #setListHiddenFiles(boolean)
  1527.      * @return the current state
  1528.      * @since 2.0
  1529.      */
  1530.     public boolean getListHiddenFiles() {
  1531.         return this.listHiddenFiles;
  1532.     }

  1533.     /**
  1534.      * Issue the FTP MDTM command (not supported by all servers) to retrieve the last modification time of a file. The modification string should be in the ISO
  1535.      * 3077 form "yyyyMMDDhhmmss(.xxx)?". The timestamp represented should also be in GMT, but not all FTP servers honor this.
  1536.      *
  1537.      * @param pathname The file path to query.
  1538.      * @return A string representing the last file modification time in <code>yyyyMMDDhhmmss</code> format.
  1539.      * @throws IOException if an I/O error occurs.
  1540.      * @since 2.0
  1541.      */
  1542.     public String getModificationTime(final String pathname) throws IOException {
  1543.         if (FTPReply.isPositiveCompletion(mdtm(pathname))) {
  1544.             // skip the return code (e.g. 213) and the space
  1545.             return getReplyString(0).substring(4);
  1546.         }
  1547.         return null;
  1548.     }

  1549.     /**
  1550.      * Returns the hostname or IP address (in the form of a string) returned by the server when entering passive mode. If not in passive mode, returns null.
  1551.      * This method only returns a valid value AFTER a data connection has been opened after a call to {@link #enterLocalPassiveMode enterLocalPassiveMode()}.
  1552.      * This is because FTPClient sends a PASV command to the server only just before opening a data connection, and not when you call
  1553.      * {@link #enterLocalPassiveMode enterLocalPassiveMode()}.
  1554.      *
  1555.      * @return The passive host name if in passive mode, otherwise null.
  1556.      */
  1557.     public String getPassiveHost() {
  1558.         return passiveHost;
  1559.     }

  1560.     /**
  1561.      * Sets the local IP address in passive mode. Useful when there are multiple network cards.
  1562.      *
  1563.      * @return The local IP address in passive mode.
  1564.      */
  1565.     public InetAddress getPassiveLocalIPAddress() {
  1566.         return this.passiveLocalHost;
  1567.     }

  1568.     /**
  1569.      * If in passive mode, returns the data port of the passive host. This method only returns a valid value AFTER a data connection has been opened after a
  1570.      * call to {@link #enterLocalPassiveMode enterLocalPassiveMode()}. This is because FTPClient sends a PASV command to the server only just before opening a
  1571.      * data connection, and not when you call {@link #enterLocalPassiveMode enterLocalPassiveMode()}.
  1572.      *
  1573.      * @return The data port of the passive server. If not in passive mode, undefined.
  1574.      */
  1575.     public int getPassivePort() {
  1576.         return passivePort;
  1577.     }

  1578.     /**
  1579.      * Retrieve the value to be used for the data socket SO_RCVBUF option.
  1580.      *
  1581.      * @return The current buffer size.
  1582.      * @since 3.3
  1583.      */
  1584.     public int getReceiveDataSocketBufferSize() {
  1585.         return receiveDataSocketBufferSize;
  1586.     }

  1587.     /**
  1588.      * Gets the reported host address for active mode EPRT/PORT commands; allows override of {@link #getHostAddress()}.
  1589.      *
  1590.      * Useful for FTP Client behind Firewall NAT.
  1591.      *
  1592.      * @return __reportActiveExternalHost if non-null, else getHostAddress();
  1593.      */
  1594.     InetAddress getReportHostAddress() {
  1595.         if (reportActiveExternalHost != null) {
  1596.             return reportActiveExternalHost;
  1597.         }
  1598.         return getHostAddress();
  1599.     }

  1600.     /**
  1601.      * Fetches the restart offset.
  1602.      *
  1603.      * @return offset The offset into the remote file at which to start the next file transfer.
  1604.      */
  1605.     public long getRestartOffset() {
  1606.         return restartOffset;
  1607.     }

  1608.     /**
  1609.      * Retrieve the value to be used for the data socket SO_SNDBUF option.
  1610.      *
  1611.      * @return The current buffer size.
  1612.      * @since 3.3
  1613.      */
  1614.     public int getSendDataSocketBufferSize() {
  1615.         return sendDataSocketBufferSize;
  1616.     }

  1617.     /**
  1618.      * Issue the FTP SIZE command to the server for a given pathname. This should produce the size of the file.
  1619.      *
  1620.      * @param pathname the file name
  1621.      *
  1622.      * @return The size information returned by the server; {@code null} if there was an error
  1623.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  1624.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  1625.      *                                      independently as itself.
  1626.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1627.      * @since 3.7
  1628.      */
  1629.     public String getSize(final String pathname) throws IOException {
  1630.         if (FTPReply.isPositiveCompletion(size(pathname))) {
  1631.             return getReplyString(0).substring(4); // skip the return code (e.g. 213) and the space
  1632.         }
  1633.         return null;
  1634.     }

  1635.     /**
  1636.      * Issue the FTP STAT command to the server.
  1637.      *
  1638.      * @return The status information returned by the server.
  1639.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  1640.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  1641.      *                                      independently as itself.
  1642.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1643.      */
  1644.     public String getStatus() throws IOException {
  1645.         if (FTPReply.isPositiveCompletion(stat())) {
  1646.             return getReplyString();
  1647.         }
  1648.         return null;
  1649.     }

  1650.     /**
  1651.      * Issue the FTP STAT command to the server for a given pathname. This should produce a listing of the file or directory.
  1652.      *
  1653.      * @param pathname the file name
  1654.      *
  1655.      * @return The status information returned by the server.
  1656.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  1657.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  1658.      *                                      independently as itself.
  1659.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1660.      */
  1661.     public String getStatus(final String pathname) throws IOException {
  1662.         if (FTPReply.isPositiveCompletion(stat(pathname))) {
  1663.             return getReplyString();
  1664.         }
  1665.         return null;
  1666.     }

  1667.     /**
  1668.      * @deprecated use {@link #getSystemType()} instead
  1669.      * @return the name
  1670.      * @throws IOException on error
  1671.      */
  1672.     @Deprecated
  1673.     public String getSystemName() throws IOException {
  1674.         if (systemName == null && FTPReply.isPositiveCompletion(syst())) {
  1675.             systemName = _replyLines.get(_replyLines.size() - 1).substring(4);
  1676.         }
  1677.         return systemName;
  1678.     }

  1679.     /**
  1680.      * Fetches the system type from the server and returns the string. This value is cached for the duration of the connection after the first call to this
  1681.      * method. In other words, only the first time that you invoke this method will it issue a SYST command to the FTP server. FTPClient will remember the value
  1682.      * and return the cached value until a call to disconnect.
  1683.      * <p>
  1684.      * If the SYST command fails, and the system property {@link #FTP_SYSTEM_TYPE_DEFAULT} is defined, then this is used instead.
  1685.      * </p>
  1686.      *
  1687.      * @return The system type obtained from the server. Never null.
  1688.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  1689.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  1690.      *                                      independently as itself.
  1691.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server (and the
  1692.      *                                      default system type property is not defined)
  1693.      * @since 2.2
  1694.      */
  1695.     public String getSystemType() throws IOException {
  1696.         // if (syst() == FTPReply.NAME_SYSTEM_TYPE)
  1697.         // Technically, we should expect a NAME_SYSTEM_TYPE response, but
  1698.         // in practice FTP servers deviate, so we soften the condition to
  1699.         // a positive completion.
  1700.         if (systemName == null) {
  1701.             if (FTPReply.isPositiveCompletion(syst())) {
  1702.                 // Assume that response is not empty here (cannot be null)
  1703.                 systemName = _replyLines.get(_replyLines.size() - 1).substring(4);
  1704.             } else {
  1705.                 // Check if the user has provided a default for when the SYST command fails
  1706.                 final String systDefault = System.getProperty(FTP_SYSTEM_TYPE_DEFAULT);
  1707.                 if (systDefault == null) {
  1708.                     throw new IOException("Unable to determine system type - response: " + getReplyString());
  1709.                 }
  1710.                 systemName = systDefault;
  1711.             }
  1712.         }
  1713.         return systemName;
  1714.     }

  1715.     /**
  1716.      * Queries the server for a supported feature. Caches the parsed response to avoid resending the command repeatedly.
  1717.      *
  1718.      * @param feature the name of the feature; it is converted to upper case.
  1719.      * @return {@code true} if the feature is present, {@code false} if the feature is not present or the {@link #feat()} command failed. Check
  1720.      *         {@link #getReplyCode()} or {@link #getReplyString()} if it is necessary to distinguish these cases.
  1721.      *
  1722.      * @throws IOException on error
  1723.      * @since 3.8.0
  1724.      */
  1725.     public boolean hasFeature(final FTPCmd feature) throws IOException {
  1726.         return hasFeature(feature.name());
  1727.     }

  1728.     /**
  1729.      * Queries the server for a supported feature. Caches the parsed response to avoid resending the command repeatedly.
  1730.      *
  1731.      * @param feature the name of the feature; it is converted to upper case.
  1732.      * @return {@code true} if the feature is present, {@code false} if the feature is not present or the {@link #feat()} command failed. Check
  1733.      *         {@link #getReplyCode()} or {@link #getReplyString()} if it is necessary to distinguish these cases.
  1734.      *
  1735.      * @throws IOException on error
  1736.      * @since 3.0
  1737.      */
  1738.     public boolean hasFeature(final String feature) throws IOException {
  1739.         if (!initFeatureMap()) {
  1740.             return false;
  1741.         }
  1742.         return featuresMap.containsKey(feature.toUpperCase(Locale.ENGLISH));
  1743.     }

  1744.     /**
  1745.      * Queries the server for a supported feature with particular value, for example "AUTH SSL" or "AUTH TLS". Caches the parsed response to avoid resending the
  1746.      * command repeatedly.
  1747.      *
  1748.      * @param feature the name of the feature; it is converted to upper case.
  1749.      * @param value   the value to find.
  1750.      *
  1751.      * @return {@code true} if the feature is present, {@code false} if the feature is not present or the {@link #feat()} command failed. Check
  1752.      *         {@link #getReplyCode()} or {@link #getReplyString()} if it is necessary to distinguish these cases.
  1753.      *
  1754.      * @throws IOException on error
  1755.      * @since 3.0
  1756.      */
  1757.     public boolean hasFeature(final String feature, final String value) throws IOException {
  1758.         if (!initFeatureMap()) {
  1759.             return false;
  1760.         }
  1761.         final Set<String> entries = featuresMap.get(feature.toUpperCase(Locale.ENGLISH));
  1762.         if (entries != null) {
  1763.             return entries.contains(value);
  1764.         }
  1765.         return false;
  1766.     }

  1767.     private void initDefaults() {
  1768.         dataConnectionMode = ACTIVE_LOCAL_DATA_CONNECTION_MODE;
  1769.         passiveHost = null;
  1770.         passivePort = -1;
  1771.         activeExternalHost = null;
  1772.         reportActiveExternalHost = null;
  1773.         activeMinPort = 0;
  1774.         activeMaxPort = 0;
  1775.         fileType = FTP.ASCII_FILE_TYPE;
  1776.         fileStructure = FTP.FILE_STRUCTURE;
  1777.         fileFormat = FTP.NON_PRINT_TEXT_FORMAT;
  1778.         fileTransferMode = FTP.STREAM_TRANSFER_MODE;
  1779.         restartOffset = 0;
  1780.         systemName = null;
  1781.         entryParser = null;
  1782.         entryParserKey = "";
  1783.         featuresMap = null;
  1784.     }

  1785.     /*
  1786.      * Create the feature map if not already created.
  1787.      */
  1788.     private boolean initFeatureMap() throws IOException {
  1789.         if (featuresMap == null) {
  1790.             // Don't create map here, because next line may throw exception
  1791.             final int replyCode = feat();
  1792.             if (replyCode == FTPReply.NOT_LOGGED_IN) { // 503
  1793.                 return false; // NET-518; don't create empty map
  1794.             }
  1795.             final boolean success = FTPReply.isPositiveCompletion(replyCode);
  1796.             // init the map here, so we don't keep trying if we know the command will fail
  1797.             featuresMap = new HashMap<>();
  1798.             if (!success) {
  1799.                 return false;
  1800.             }
  1801.             for (final String line : _replyLines) {
  1802.                 if (line.startsWith(" ")) { // it's a FEAT entry
  1803.                     String key;
  1804.                     String value = "";
  1805.                     final int varsep = line.indexOf(' ', 1);
  1806.                     if (varsep > 0) {
  1807.                         key = line.substring(1, varsep);
  1808.                         value = line.substring(varsep + 1);
  1809.                     } else {
  1810.                         key = line.substring(1);
  1811.                     }
  1812.                     key = key.toUpperCase(Locale.ENGLISH);
  1813.                     final Set<String> entries = featuresMap.computeIfAbsent(key, k -> new HashSet<>());
  1814.                     entries.add(value);
  1815.                 }
  1816.             }
  1817.         }
  1818.         return true;
  1819.     }

  1820.     /**
  1821.      * Using the default autodetect mechanism, initialize an FTPListParseEngine object containing a raw file information for the current working directory on
  1822.      * the server This information is obtained through the LIST command. This object is then capable of being iterated to return a sequence of FTPFile objects
  1823.      * with information filled in by the <code>FTPFileEntryParser</code> used.
  1824.      * <p>
  1825.      * This method differs from using the listFiles() methods in that expensive FTPFile objects are not created until needed which may be an advantage on large
  1826.      * lists.
  1827.      * </p>
  1828.      *
  1829.      * @return A FTPListParseEngine object that holds the raw information and is capable of providing parsed FTPFile objects, one for each file containing
  1830.      *         information contained in the given path in the format determined by the <code>parser</code> parameter. Null will be returned if a data
  1831.      *         connection cannot be opened. If the current working directory contains no files, an empty array will be the return.
  1832.      *
  1833.      * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
  1834.      *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
  1835.      *                                                                         This exception may be caught either as an IOException or independently as itself.
  1836.      * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
  1837.      *                                                                         a reply from the server.
  1838.      * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the autodetect mechanism cannot resolve the type of system we are
  1839.      *                                                                         connected with.
  1840.      * @see FTPListParseEngine
  1841.      */
  1842.     public FTPListParseEngine initiateListParsing() throws IOException {
  1843.         return initiateListParsing((String) null);
  1844.     }

  1845.     /**
  1846.      * private method through which all listFiles() and initiateListParsing methods pass once a parser is determined.
  1847.      *
  1848.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  1849.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  1850.      *                                      independently as itself.
  1851.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  1852.      * @see FTPListParseEngine
  1853.      */
  1854.     private FTPListParseEngine initiateListParsing(final FTPFileEntryParser parser, final String pathname) throws IOException {
  1855.         final Socket socket = _openDataConnection_(FTPCmd.LIST, getListArguments(pathname));
  1856.         final FTPListParseEngine engine = new FTPListParseEngine(parser, configuration);
  1857.         if (socket == null) {
  1858.             return engine;
  1859.         }
  1860.         try {
  1861.             engine.readServerList(socket.getInputStream(), getControlEncoding());
  1862.         } finally {
  1863.             Util.closeQuietly(socket);
  1864.         }
  1865.         completePendingCommand();
  1866.         return engine;
  1867.     }

  1868.     /**
  1869.      * Using the default autodetect mechanism, initialize an FTPListParseEngine object containing a raw file information for the supplied directory. This
  1870.      * information is obtained through the LIST command. This object is then capable of being iterated to return a sequence of FTPFile objects with information
  1871.      * filled in by the <code>FTPFileEntryParser</code> used.
  1872.      * <p>
  1873.      * The server may or may not expand glob expressions. You should avoid using glob expressions because the return format for glob listings differs from
  1874.      * server to server and will likely cause this method to fail.
  1875.      * </p>
  1876.      * <p>
  1877.      * This method differs from using the listFiles() methods in that expensive FTPFile objects are not created until needed which may be an advantage on large
  1878.      * lists.
  1879.      * </p>
  1880.      *
  1881.      * <pre>
  1882.      * FTPClient f = FTPClient();
  1883.      * f.connect(server);
  1884.      * f.login(username, password);
  1885.      * FTPListParseEngine engine = f.initiateListParsing(directory);
  1886.      *
  1887.      * while (engine.hasNext()) {
  1888.      *     FTPFile[] files = engine.getNext(25); // "page size" you want
  1889.      *     // do whatever you want with these files, display them, etc.
  1890.      *     // expensive FTPFile objects not created until needed.
  1891.      * }
  1892.      * </pre>
  1893.      *
  1894.      * @param pathname the starting directory
  1895.      *
  1896.      * @return A FTPListParseEngine object that holds the raw information and is capable of providing parsed FTPFile objects, one for each file containing
  1897.      *         information contained in the given path in the format determined by the <code>parser</code> parameter. Null will be returned if a data
  1898.      *         connection cannot be opened. If the current working directory contains no files, an empty array will be the return.
  1899.      *
  1900.      * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
  1901.      *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
  1902.      *                                                                         This exception may be caught either as an IOException or independently as itself.
  1903.      * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
  1904.      *                                                                         a reply from the server.
  1905.      * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the autodetect mechanism cannot resolve the type of system we are
  1906.      *                                                                         connected with.
  1907.      * @see FTPListParseEngine
  1908.      */
  1909.     public FTPListParseEngine initiateListParsing(final String pathname) throws IOException {
  1910.         return initiateListParsing((String) null, pathname);
  1911.     }

  1912.     /**
  1913.      * Using the supplied parser key, initialize an FTPListParseEngine object containing a raw file information for the supplied directory. This information is
  1914.      * obtained through the LIST command. This object is then capable of being iterated to return a sequence of FTPFile objects with information filled in by
  1915.      * the <code>FTPFileEntryParser</code> used.
  1916.      * <p>
  1917.      * The server may or may not expand glob expressions. You should avoid using glob expressions because the return format for glob listings differs from
  1918.      * server to server and will likely cause this method to fail.
  1919.      * </p>
  1920.      * <p>
  1921.      * This method differs from using the listFiles() methods in that expensive FTPFile objects are not created until needed which may be an advantage on large
  1922.      * lists.
  1923.      * </p>
  1924.      *
  1925.      * @param parserKey A string representing a designated code or fully-qualified class name of an <code>FTPFileEntryParser</code> that should be used to
  1926.      *                  parse each server file listing. May be {@code null}, in which case the code checks first the system property {@link #FTP_SYSTEM_TYPE},
  1927.      *                  and if that is not defined the SYST command is used to provide the value. To allow for arbitrary system types, the return from the SYST
  1928.      *                  command is used to look up an alias for the type in the {@link #SYSTEM_TYPE_PROPERTIES} properties file if it is available.
  1929.      * @param pathname  the starting directory
  1930.      *
  1931.      * @return A FTPListParseEngine object that holds the raw information and is capable of providing parsed FTPFile objects, one for each file containing
  1932.      *         information contained in the given path in the format determined by the <code>parser</code> parameter. Null will be returned if a data
  1933.      *         connection cannot be opened. If the current working directory contains no files, an empty array will be the return.
  1934.      *
  1935.      * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
  1936.      *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
  1937.      *                                                                         This exception may be caught either as an IOException or independently as itself.
  1938.      * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
  1939.      *                                                                         a reply from the server.
  1940.      * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the parserKey parameter cannot be resolved by the selected parser
  1941.      *                                                                         factory. In the DefaultFTPEntryParserFactory, this will happen when parserKey is
  1942.      *                                                                         neither the fully qualified class name of a class implementing the interface
  1943.      *                                                                         org.apache.commons.net.ftp.FTPFileEntryParser nor a string containing one of the
  1944.      *                                                                         recognized keys mapping to such a parser or if class loader security issues
  1945.      *                                                                         prevent its being loaded.
  1946.      * @see FTPListParseEngine
  1947.      */
  1948.     public FTPListParseEngine initiateListParsing(final String parserKey, final String pathname) throws IOException {
  1949.         createParser(parserKey); // create and cache parser
  1950.         return initiateListParsing(entryParser, pathname);
  1951.     }

  1952.     /**
  1953.      * Initiate list parsing for MLSD listings in the current working directory.
  1954.      *
  1955.      * @return the engine
  1956.      * @throws IOException on error
  1957.      */
  1958.     public FTPListParseEngine initiateMListParsing() throws IOException {
  1959.         return initiateMListParsing(null);
  1960.     }

  1961.     /**
  1962.      * Initiate list parsing for MLSD listings.
  1963.      *
  1964.      * @param pathname the path from where to MLSD.
  1965.      * @return the engine.
  1966.      * @throws IOException on error
  1967.      */
  1968.     public FTPListParseEngine initiateMListParsing(final String pathname) throws IOException {
  1969.         final Socket socket = _openDataConnection_(FTPCmd.MLSD, pathname);
  1970.         final FTPListParseEngine engine = new FTPListParseEngine(MLSxEntryParser.getInstance(), configuration);
  1971.         if (socket == null) {
  1972.             return engine;
  1973.         }
  1974.         try {
  1975.             engine.readServerList(socket.getInputStream(), getControlEncoding());
  1976.         } finally {
  1977.             Util.closeQuietly(socket);
  1978.             completePendingCommand();
  1979.         }
  1980.         return engine;
  1981.     }

  1982.     /**
  1983.      * Returns, whether the IP address from the server's response should be used. Until 3.9.0, this has always been the case. Beginning with 3.9.0, that IP
  1984.      * address will be silently ignored, and replaced with the remote IP address of the control connection, unless this configuration option is given, which
  1985.      * restores the old behavior. To enable this by default, use the system property {@link FTPClient#FTP_IP_ADDRESS_FROM_PASV_RESPONSE}.
  1986.      *
  1987.      * @return True, if the IP address from the server's response will be used (pre-3.9 compatible behavior), or false (ignore that IP address).
  1988.      *
  1989.      * @see FTPClient#FTP_IP_ADDRESS_FROM_PASV_RESPONSE
  1990.      * @see #setIpAddressFromPasvResponse(boolean)
  1991.      * @since 3.9.0
  1992.      */
  1993.     public boolean isIpAddressFromPasvResponse() {
  1994.         return ipAddressFromPasvResponse;
  1995.     }

  1996.     /**
  1997.      * Return whether or not verification of the remote host participating in data connections is enabled. The default behavior is for verification to be
  1998.      * enabled.
  1999.      *
  2000.      * @return True if verification is enabled, false if not.
  2001.      */
  2002.     public boolean isRemoteVerificationEnabled() {
  2003.         return remoteVerificationEnabled;
  2004.     }

  2005.     /**
  2006.      * Whether to attempt using EPSV with IPv4. Default (if not set) is {@code false}
  2007.      *
  2008.      * @return true if EPSV shall be attempted with IPv4.
  2009.      * @since 2.2
  2010.      */
  2011.     public boolean isUseEPSVwithIPv4() {
  2012.         return useEPSVwithIPv4;
  2013.     }

  2014.     /**
  2015.      * Using the default system autodetect mechanism, obtain a list of directories contained in the current working directory.
  2016.      * <p>
  2017.      * This information is obtained through the LIST command. The contents of the returned array is determined by the<code>FTPFileEntryParser</code> used.
  2018.      * </p>
  2019.      * <p>
  2020.      * N.B. the LIST command does not generally return very precise timestamps. For recent files, the response usually contains hours and minutes (not seconds).
  2021.      * For older files, the output may only contain a date. If the server supports it, the MLSD command returns timestamps with a precision of seconds, and may
  2022.      * include milliseconds. See {@link #mlistDir()}
  2023.      * </p>
  2024.      *
  2025.      * @return The list of directories contained in the current directory in the format determined by the autodetection mechanism.
  2026.      * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
  2027.      *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
  2028.      *                                                                         This exception may be caught either as an IOException or independently as itself.
  2029.      * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
  2030.      *                                                                         a reply from the server.
  2031.      * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the parserKey parameter cannot be resolved by the selected parser
  2032.      *                                                                         factory. In the DefaultFTPEntryParserFactory, this will happen when parserKey is
  2033.      *                                                                         neither the fully qualified class name of a class implementing the interface
  2034.      *                                                                         org.apache.commons.net.ftp.FTPFileEntryParser nor a string containing one of the
  2035.      *                                                                         recognized keys mapping to such a parser or if class loader security issues
  2036.      *                                                                         prevent its being loaded.
  2037.      * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
  2038.      * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
  2039.      * @see org.apache.commons.net.ftp.FTPFileEntryParser
  2040.      * @since 3.0
  2041.      */
  2042.     public FTPFile[] listDirectories() throws IOException {
  2043.         return listDirectories((String) null);
  2044.     }

  2045.     /**
  2046.      * Using the default system autodetect mechanism, obtain a list of directories contained in the specified directory.
  2047.      * <p>
  2048.      * This information is obtained through the LIST command. The contents of the returned array is determined by the<code>FTPFileEntryParser</code> used.
  2049.      * </p>
  2050.      * <p>
  2051.      * N.B. the LIST command does not generally return very precise timestamps. For recent files, the response usually contains hours and minutes (not seconds).
  2052.      * For older files, the output may only contain a date. If the server supports it, the MLSD command returns timestamps with a precision of seconds, and may
  2053.      * include milliseconds. See {@link #mlistDir()}
  2054.      * </p>
  2055.      *
  2056.      * @param parent the starting directory
  2057.      * @return The list of directories contained in the specified directory in the format determined by the autodetection mechanism.
  2058.      * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
  2059.      *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
  2060.      *                                                                         This exception may be caught either as an IOException or independently as itself.
  2061.      * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
  2062.      *                                                                         a reply from the server.
  2063.      * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the parserKey parameter cannot be resolved by the selected parser
  2064.      *                                                                         factory. In the DefaultFTPEntryParserFactory, this will happen when parserKey is
  2065.      *                                                                         neither the fully qualified class name of a class implementing the interface
  2066.      *                                                                         org.apache.commons.net.ftp.FTPFileEntryParser nor a string containing one of the
  2067.      *                                                                         recognized keys mapping to such a parser or if class loader security issues
  2068.      *                                                                         prevent its being loaded.
  2069.      * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
  2070.      * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
  2071.      * @see org.apache.commons.net.ftp.FTPFileEntryParser
  2072.      * @since 3.0
  2073.      */
  2074.     public FTPFile[] listDirectories(final String parent) throws IOException {
  2075.         return listFiles(parent, FTPFileFilters.DIRECTORIES);
  2076.     }

  2077.     /**
  2078.      * Using the default system autodetect mechanism, obtain a list of file information for the current working directory.
  2079.      * <p>
  2080.      * This information is obtained through the LIST command. The contents of the returned array is determined by the<code>FTPFileEntryParser</code> used.
  2081.      * </p>
  2082.      * <p>
  2083.      * N.B. the LIST command does not generally return very precise timestamps. For recent files, the response usually contains hours and minutes (not seconds).
  2084.      * For older files, the output may only contain a date. If the server supports it, the MLSD command returns timestamps with a precision of seconds, and may
  2085.      * include milliseconds. See {@link #mlistDir()}
  2086.      * </p>
  2087.      *
  2088.      * @return The list of file information contained in the current directory in the format determined by the autodetection mechanism.
  2089.      *         <b>NOTE:</b> This array may contain null members if any of the individual file listings failed to parse. The caller should check each entry for
  2090.      *         null before referencing it.
  2091.      * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
  2092.      *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
  2093.      *                                                                         This exception may be caught either as an IOException or independently as itself.
  2094.      * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
  2095.      *                                                                         a reply from the server.
  2096.      * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the parserKey parameter cannot be resolved by the selected parser
  2097.      *                                                                         factory. In the DefaultFTPEntryParserFactory, this will happen when parserKey is
  2098.      *                                                                         neither the fully qualified class name of a class implementing the interface
  2099.      *                                                                         org.apache.commons.net.ftp.FTPFileEntryParser nor a string containing one of the
  2100.      *                                                                         recognized keys mapping to such a parser or if class loader security issues
  2101.      *                                                                         prevent its being loaded.
  2102.      * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
  2103.      * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
  2104.      * @see org.apache.commons.net.ftp.FTPFileEntryParser
  2105.      */
  2106.     public FTPFile[] listFiles() throws IOException {
  2107.         return listFiles((String) null);
  2108.     }

  2109.     /**
  2110.      * Using the default system autodetect mechanism, obtain a list of file information for the current working directory or for just a single file.
  2111.      * <p>
  2112.      * This information is obtained through the LIST command. The contents of the returned array is determined by the<code>FTPFileEntryParser</code> used.
  2113.      * </p>
  2114.      * <p>
  2115.      * N.B. the LIST command does not generally return very precise timestamps. For recent files, the response usually contains hours and minutes (not seconds).
  2116.      * For older files, the output may only contain a date. If the server supports it, the MLSD command returns timestamps with a precision of seconds, and may
  2117.      * include milliseconds. See {@link #mlistDir()}
  2118.      * </p>
  2119.      *
  2120.      * @param pathname The file or directory to list. Since the server may or may not expand glob expressions, using them here is not recommended and may well
  2121.      *                 cause this method to fail. Also, some servers treat a leading '-' as being an option. To avoid this interpretation, use an absolute
  2122.      *                 pathname or prefix the pathname with ./ (unix style servers). Some servers may support "--" as meaning end of options, in which case "--
  2123.      *                 -xyz" should work.
  2124.      * @return The list of file information contained in the given path in the format determined by the autodetection mechanism
  2125.      * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
  2126.      *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
  2127.      *                                                                         This exception may be caught either as an IOException or independently as itself.
  2128.      * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
  2129.      *                                                                         a reply from the server.
  2130.      * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the parserKey parameter cannot be resolved by the selected parser
  2131.      *                                                                         factory. In the DefaultFTPEntryParserFactory, this will happen when parserKey is
  2132.      *                                                                         neither the fully qualified class name of a class implementing the interface
  2133.      *                                                                         org.apache.commons.net.ftp.FTPFileEntryParser nor a string containing one of the
  2134.      *                                                                         recognized keys mapping to such a parser or if class loader security issues
  2135.      *                                                                         prevent its being loaded.
  2136.      * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
  2137.      * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
  2138.      * @see org.apache.commons.net.ftp.FTPFileEntryParser
  2139.      */
  2140.     public FTPFile[] listFiles(final String pathname) throws IOException {
  2141.         return initiateListParsing((String) null, pathname).getFiles();
  2142.     }

  2143.     /**
  2144.      * Version of {@link #listFiles(String)} which allows a filter to be provided. For example: <code>listFiles("site", FTPFileFilters.DIRECTORY);</code>
  2145.      *
  2146.      * @param pathname the initial path, may be null
  2147.      * @param filter   the filter, non-null
  2148.      * @return the array of FTPFile entries.
  2149.      * @throws IOException on error
  2150.      * @since 2.2
  2151.      */
  2152.     public FTPFile[] listFiles(final String pathname, final FTPFileFilter filter) throws IOException {
  2153.         return initiateListParsing((String) null, pathname).getFiles(filter);
  2154.     }

  2155.     /**
  2156.      * Fetches the system help information from the server and returns the full string.
  2157.      *
  2158.      * @return The system help string obtained from the server. null if the information could not be obtained.
  2159.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2160.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2161.      *                                      independently as itself.
  2162.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2163.      */
  2164.     public String listHelp() throws IOException {
  2165.         return FTPReply.isPositiveCompletion(help()) ? getReplyString() : null;
  2166.     }

  2167.     /**
  2168.      * Fetches the help information for a given command from the server and returns the full string.
  2169.      *
  2170.      * @param command The command on which to ask for help.
  2171.      * @return The command help string obtained from the server. null if the information could not be obtained.
  2172.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2173.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2174.      *                                      independently as itself.
  2175.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2176.      */
  2177.     public String listHelp(final String command) throws IOException {
  2178.         return FTPReply.isPositiveCompletion(help(command)) ? getReplyString() : null;
  2179.     }

  2180.     /**
  2181.      * Obtain a list of file names in the current working directory This information is obtained through the NLST command. If the current directory contains no
  2182.      * files, a zero length array is returned only if the FTP server returned a positive completion code, otherwise, null is returned (the FTP server returned a
  2183.      * 550 error No files found.). If the directory is not empty, an array of file names in the directory is returned.
  2184.      *
  2185.      * @return The list of file names contained in the current working directory. null if the list could not be obtained. If there are no file names in the
  2186.      *         directory, a zero-length array is returned.
  2187.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2188.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2189.      *                                      independently as itself.
  2190.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2191.      */
  2192.     public String[] listNames() throws IOException {
  2193.         return listNames(null);
  2194.     }

  2195.     /**
  2196.      * Obtain a list of file names in a directory (or just the name of a given file, which is not particularly useful). This information is obtained through the
  2197.      * NLST command. If the given pathname is a directory and contains no files, a zero length array is returned only if the FTP server returned a positive
  2198.      * completion code, otherwise null is returned (the FTP server returned a 550 error No files found.). If the directory is not empty, an array of file names
  2199.      * in the directory is returned. If the pathname corresponds to a file, only that file will be listed. The server may or may not expand glob expressions.
  2200.      *
  2201.      * @param pathname The file or directory to list. Warning: the server may treat a leading '-' as an option introducer. If so, try using an absolute path, or
  2202.      *                 prefix the path with ./ (unix style servers). Some servers may support "--" as meaning end of options, in which case "-- -xyz" should
  2203.      *                 work.
  2204.      * @return The list of file names contained in the given path. null if the list could not be obtained. If there are no file names in the directory, a
  2205.      *         zero-length array is returned.
  2206.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2207.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2208.      *                                      independently as itself.
  2209.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2210.      */
  2211.     public String[] listNames(final String pathname) throws IOException {
  2212.         final ArrayList<String> results = new ArrayList<>();
  2213.         try (final Socket socket = _openDataConnection_(FTPCmd.NLST, getListArguments(pathname))) {
  2214.             if (socket == null) {
  2215.                 return null;
  2216.             }
  2217.             try (final BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), getControlEncoding()))) {
  2218.                 String line;
  2219.                 while ((line = reader.readLine()) != null) {
  2220.                     results.add(line);
  2221.                 }
  2222.             }
  2223.         }
  2224.         if (completePendingCommand()) {
  2225.             return results.toArray(NetConstants.EMPTY_STRING_ARRAY);
  2226.         }
  2227.         return null;
  2228.     }

  2229.     /**
  2230.      * Login to the FTP server using the provided user and password.
  2231.      *
  2232.      * @param user     The user name to login under.
  2233.      * @param password The password to use.
  2234.      * @return True if successfully completed, false if not.
  2235.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2236.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2237.      *                                      independently as itself.
  2238.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2239.      */
  2240.     public boolean login(final String user, final String password) throws IOException {
  2241.         user(user);
  2242.         if (FTPReply.isPositiveCompletion(_replyCode)) {
  2243.             return true;
  2244.         }
  2245.         // If we get here, we either have an error code, or an intermediate
  2246.         // reply requesting password.
  2247.         if (!FTPReply.isPositiveIntermediate(_replyCode)) {
  2248.             return false;
  2249.         }
  2250.         return FTPReply.isPositiveCompletion(pass(password));
  2251.     }

  2252.     /**
  2253.      * Login to the FTP server using the provided username, password, and account. If no account is required by the server, only the username and password, the
  2254.      * account information is not used.
  2255.      *
  2256.      * @param user     The user name to login under.
  2257.      * @param password The password to use.
  2258.      * @param account  The account to use.
  2259.      * @return True if successfully completed, false if not.
  2260.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2261.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2262.      *                                      independently as itself.
  2263.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2264.      */
  2265.     public boolean login(final String user, final String password, final String account) throws IOException {
  2266.         user(user);
  2267.         if (FTPReply.isPositiveCompletion(_replyCode)) {
  2268.             return true;
  2269.         }
  2270.         // If we get here, we either have an error code, or an intermediate
  2271.         // reply requesting password.
  2272.         if (!FTPReply.isPositiveIntermediate(_replyCode)) {
  2273.             return false;
  2274.         }
  2275.         pass(password);
  2276.         if (FTPReply.isPositiveCompletion(_replyCode)) {
  2277.             return true;
  2278.         }
  2279.         if (!FTPReply.isPositiveIntermediate(_replyCode)) {
  2280.             return false;
  2281.         }
  2282.         return FTPReply.isPositiveCompletion(acct(account));
  2283.     }

  2284.     /**
  2285.      * Logout of the FTP server by sending the QUIT command.
  2286.      *
  2287.      * @return True if successfully completed, false if not.
  2288.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2289.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2290.      *                                      independently as itself.
  2291.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2292.      */
  2293.     public boolean logout() throws IOException {
  2294.         return FTPReply.isPositiveCompletion(quit());
  2295.     }

  2296.     /**
  2297.      * Creates a new subdirectory on the FTP server in the current directory (if a relative pathname is given) or where specified (if an absolute pathname is
  2298.      * given).
  2299.      *
  2300.      * @param pathname The pathname of the directory to create.
  2301.      * @return True if successfully completed, false if not.
  2302.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2303.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2304.      *                                      independently as itself.
  2305.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2306.      */
  2307.     public boolean makeDirectory(final String pathname) throws IOException {
  2308.         return FTPReply.isPositiveCompletion(mkd(pathname));
  2309.     }

  2310.     /**
  2311.      * Issue the FTP MDTM command (not supported by all servers) to retrieve the last modification time of a file. The modification string should be in the ISO
  2312.      * 3077 form "yyyyMMDDhhmmss(.xxx)?". The timestamp represented should also be in GMT, but not all FTP servers honor this.
  2313.      *
  2314.      * @param pathname The file path to query.
  2315.      * @return A Calendar representing the last file modification time, may be {@code null}. The Calendar timestamp will be null if a parse error occurs.
  2316.      * @throws IOException if an I/O error occurs.
  2317.      * @since 3.8.0
  2318.      */
  2319.     public Calendar mdtmCalendar(final String pathname) throws IOException {
  2320.         final String modificationTime = getModificationTime(pathname);
  2321.         if (modificationTime != null) {
  2322.             return MLSxEntryParser.parseGMTdateTime(modificationTime);
  2323.         }
  2324.         return null;
  2325.     }

  2326.     /**
  2327.      * Issue the FTP MDTM command (not supported by all servers) to retrieve the last modification time of a file. The modification string should be in the ISO
  2328.      * 3077 form "yyyyMMDDhhmmss(.xxx)?". The timestamp represented should also be in GMT, but not all FTP servers honor this.
  2329.      *
  2330.      * @param pathname The file path to query.
  2331.      * @return A FTPFile representing the last file modification time, may be {@code null}. The FTPFile timestamp will be null if a parse error occurs.
  2332.      * @throws IOException if an I/O error occurs.
  2333.      * @since 3.4
  2334.      */
  2335.     public FTPFile mdtmFile(final String pathname) throws IOException {
  2336.         final String modificationTime = getModificationTime(pathname);
  2337.         if (modificationTime != null) {
  2338.             final FTPFile file = new FTPFile();
  2339.             file.setName(pathname);
  2340.             file.setRawListing(modificationTime);
  2341.             file.setTimestamp(MLSxEntryParser.parseGMTdateTime(modificationTime));
  2342.             return file;
  2343.         }
  2344.         return null;
  2345.     }

  2346.     /**
  2347.      * Issue the FTP MDTM command (not supported by all servers) to retrieve the last modification time of a file. The modification string should be in the ISO
  2348.      * 3077 form "yyyyMMDDhhmmss(.xxx)?". The timestamp represented should also be in GMT, but not all FTP servers honor this.
  2349.      *
  2350.      * @param pathname The file path to query.
  2351.      * @return An Instant representing the last file modification time, may be {@code null}. The Instant timestamp will be null if a parse error occurs.
  2352.      * @throws IOException if an I/O error occurs.
  2353.      * @since 3.9.0
  2354.      */
  2355.     public Instant mdtmInstant(final String pathname) throws IOException {
  2356.         final String modificationTime = getModificationTime(pathname);
  2357.         if (modificationTime != null) {
  2358.             return MLSxEntryParser.parseGmtInstant(modificationTime);
  2359.         }
  2360.         return null;
  2361.     }

  2362.     /**
  2363.      * Merge two copystream listeners, either or both of which may be null.
  2364.      *
  2365.      * @param local the listener used by this class, may be null
  2366.      * @return a merged listener or a single listener or null
  2367.      * @since 3.0
  2368.      */
  2369.     private CopyStreamListener mergeListeners(final CopyStreamListener local) {
  2370.         if (local == null) {
  2371.             return copyStreamListener;
  2372.         }
  2373.         if (copyStreamListener == null) {
  2374.             return local;
  2375.         }
  2376.         // Both are non-null
  2377.         final CopyStreamAdapter merged = new CopyStreamAdapter();
  2378.         merged.addCopyStreamListener(local);
  2379.         merged.addCopyStreamListener(copyStreamListener);
  2380.         return merged;
  2381.     }

  2382.     /**
  2383.      * Generate a directory listing for the current directory using the MLSD command.
  2384.      *
  2385.      * @return the array of file entries
  2386.      * @throws IOException on error
  2387.      * @since 3.0
  2388.      */
  2389.     public FTPFile[] mlistDir() throws IOException {
  2390.         return mlistDir(null);
  2391.     }

  2392.     /**
  2393.      * Generate a directory listing using the MLSD command.
  2394.      *
  2395.      * @param pathname the directory name, may be {@code null}
  2396.      * @return the array of file entries
  2397.      * @throws IOException on error
  2398.      * @since 3.0
  2399.      */
  2400.     public FTPFile[] mlistDir(final String pathname) throws IOException {
  2401.         return initiateMListParsing(pathname).getFiles();
  2402.     }

  2403.     /**
  2404.      * Generate a directory listing using the MLSD command.
  2405.      *
  2406.      * @param pathname the directory name, may be {@code null}
  2407.      * @param filter   the filter to apply to the responses
  2408.      * @return the array of file entries
  2409.      * @throws IOException on error
  2410.      * @since 3.0
  2411.      */
  2412.     public FTPFile[] mlistDir(final String pathname, final FTPFileFilter filter) throws IOException {
  2413.         return initiateMListParsing(pathname).getFiles(filter);
  2414.     }

  2415.     /**
  2416.      * Gets file details using the MLST command
  2417.      *
  2418.      * @param pathname the file or directory to list, may be {@code null}
  2419.      * @return the file details, may be {@code null}
  2420.      * @throws IOException on error
  2421.      * @since 3.0
  2422.      */
  2423.     public FTPFile mlistFile(final String pathname) throws IOException {
  2424.         final boolean success = FTPReply.isPositiveCompletion(sendCommand(FTPCmd.MLST, pathname));
  2425.         if (success) {
  2426.             String reply = getReplyString(1);
  2427.             // some FTP server reply not contains space before fact(s)
  2428.             if (reply.charAt(0) != ' ') {
  2429.                 reply = " " + reply;
  2430.             }
  2431.             /*
  2432.              * check the response makes sense. Must have space before fact(s) and between fact(s) and file name Fact(s) can be absent, so at least 3 chars are
  2433.              * needed.
  2434.              */
  2435.             if (reply.length() < 3) {
  2436.                 throw new MalformedServerReplyException("Invalid server reply (MLST): '" + reply + "'");
  2437.             }
  2438.             // some FTP server reply contains more than one space before fact(s)
  2439.             final String entry = reply.replaceAll("^\\s+", ""); // skip leading space for parser
  2440.             return MLSxEntryParser.parseEntry(entry);
  2441.         }
  2442.         return null;
  2443.     }

  2444.     /**
  2445.      * Returns the pathname of the current working directory.
  2446.      *
  2447.      * @return The pathname of the current working directory. If it cannot be obtained, returns null.
  2448.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2449.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2450.      *                                      independently as itself.
  2451.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2452.      */
  2453.     public String printWorkingDirectory() throws IOException {
  2454.         if (pwd() != FTPReply.PATHNAME_CREATED) {
  2455.             return null;
  2456.         }
  2457.         return parsePathname(_replyLines.get(_replyLines.size() - 1));
  2458.     }

  2459.     /**
  2460.      * Reinitialize the FTP session. Not all FTP servers support this command, which issues the FTP REIN command.
  2461.      *
  2462.      * @return True if successfully completed, false if not.
  2463.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2464.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2465.      *                                      independently as itself.
  2466.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2467.      * @since 3.4 (made public)
  2468.      */
  2469.     public boolean reinitialize() throws IOException {
  2470.         rein();
  2471.         if (FTPReply.isPositiveCompletion(_replyCode) || FTPReply.isPositivePreliminary(_replyCode) && FTPReply.isPositiveCompletion(getReply())) {
  2472.             initDefaults();
  2473.             return true;
  2474.         }
  2475.         return false;
  2476.     }

  2477.     // For server to server transfers
  2478.     /**
  2479.      * Initiate a server to server file transfer. This method tells the server to which the client is connected to append to a given file on the other server.
  2480.      * The other server must have had a <code>remoteRetrieve</code> issued to it by another FTPClient.
  2481.      *
  2482.      * @param fileName The name of the file to be appended to, or if the file does not exist, the name to call the file being stored.
  2483.      *
  2484.      * @return True if successfully completed, false if not.
  2485.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2486.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2487.      *                                      independently as itself.
  2488.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2489.      */
  2490.     public boolean remoteAppend(final String fileName) throws IOException {
  2491.         if (dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE || dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE) {
  2492.             return FTPReply.isPositivePreliminary(appe(fileName));
  2493.         }
  2494.         return false;
  2495.     }

  2496.     /**
  2497.      * Initiate a server to server file transfer. This method tells the server to which the client is connected to retrieve a given file from the other server.
  2498.      *
  2499.      * @param fileName The name of the file to retrieve.
  2500.      * @return True if successfully completed, false if not.
  2501.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2502.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2503.      *                                      independently as itself.
  2504.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2505.      */
  2506.     public boolean remoteRetrieve(final String fileName) throws IOException {
  2507.         if (dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE || dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE) {
  2508.             return FTPReply.isPositivePreliminary(retr(fileName));
  2509.         }
  2510.         return false;
  2511.     }

  2512.     /**
  2513.      * Initiate a server to server file transfer. This method tells the server to which the client is connected to store a file on the other server using the
  2514.      * given file name. The other server must have had a <code>remoteRetrieve</code> issued to it by another FTPClient.
  2515.      *
  2516.      * @param fileName The name to call the file that is to be stored.
  2517.      * @return True if successfully completed, false if not.
  2518.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2519.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2520.      *                                      independently as itself.
  2521.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2522.      */
  2523.     public boolean remoteStore(final String fileName) throws IOException {
  2524.         if (dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE || dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE) {
  2525.             return FTPReply.isPositivePreliminary(stor(fileName));
  2526.         }
  2527.         return false;
  2528.     }

  2529.     /**
  2530.      * Initiate a server to server file transfer. This method tells the server to which the client is connected to store a file on the other server using a
  2531.      * unique file name. The other server must have had a <code>remoteRetrieve</code> issued to it by another FTPClient. Many FTP servers require that a base
  2532.      * file name be given from which the unique file name can be derived. For those servers use the other version of <code>remoteStoreUnique</code>
  2533.      *
  2534.      * @return True if successfully completed, false if not.
  2535.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2536.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2537.      *                                      independently as itself.
  2538.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2539.      */
  2540.     public boolean remoteStoreUnique() throws IOException {
  2541.         if (dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE || dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE) {
  2542.             return FTPReply.isPositivePreliminary(stou());
  2543.         }
  2544.         return false;
  2545.     }

  2546.     /**
  2547.      * Initiate a server to server file transfer. This method tells the server to which the client is connected to store a file on the other server using a
  2548.      * unique file name based on the given file name. The other server must have had a <code>remoteRetrieve</code> issued to it by another FTPClient.
  2549.      *
  2550.      * @param fileName The name on which to base the file name of the file that is to be stored.
  2551.      * @return True if successfully completed, false if not.
  2552.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2553.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2554.      *                                      independently as itself.
  2555.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2556.      */
  2557.     public boolean remoteStoreUnique(final String fileName) throws IOException {
  2558.         if (dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE || dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE) {
  2559.             return FTPReply.isPositivePreliminary(stou(fileName));
  2560.         }
  2561.         return false;
  2562.     }

  2563.     /**
  2564.      * Removes a directory on the FTP server (if empty).
  2565.      *
  2566.      * @param pathname The pathname of the directory to remove.
  2567.      * @return True if successfully completed, false if not.
  2568.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2569.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2570.      *                                      independently as itself.
  2571.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2572.      */
  2573.     public boolean removeDirectory(final String pathname) throws IOException {
  2574.         return FTPReply.isPositiveCompletion(rmd(pathname));
  2575.     }

  2576.     /**
  2577.      * Renames a remote file.
  2578.      *
  2579.      * @param from The name of the remote file to rename.
  2580.      * @param to   The new name of the remote file.
  2581.      * @return True if successfully completed, false if not.
  2582.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2583.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2584.      *                                      independently as itself.
  2585.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2586.      */
  2587.     public boolean rename(final String from, final String to) throws IOException {
  2588.         if (!FTPReply.isPositiveIntermediate(rnfr(from))) {
  2589.             return false;
  2590.         }
  2591.         return FTPReply.isPositiveCompletion(rnto(to));
  2592.     }

  2593.     /**
  2594.      * Restart a <code>STREAM_TRANSFER_MODE</code> file transfer starting from the given offset. This will only work on FTP servers supporting the REST comand
  2595.      * for the stream transfer mode. However, most FTP servers support this. Any subsequent file transfer will start reading or writing the remote file from the
  2596.      * indicated offset.
  2597.      *
  2598.      * @param offset The offset into the remote file at which to start the next file transfer.
  2599.      * @return True if successfully completed, false if not.
  2600.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2601.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2602.      *                                      independently as itself.
  2603.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2604.      * @since 3.1 (changed from private to protected)
  2605.      */
  2606.     protected boolean restart(final long offset) throws IOException {
  2607.         restartOffset = 0;
  2608.         return FTPReply.isPositiveIntermediate(rest(Long.toString(offset)));
  2609.     }

  2610.     /**
  2611.      * Retrieves a named file from the server and writes it to the given OutputStream. This method does NOT close the given OutputStream. If the current file
  2612.      * type is ASCII, line separators in the file are converted to the local representation.
  2613.      * <p>
  2614.      * Note: if you have used {@link #setRestartOffset(long)}, the file data will start from the selected offset.
  2615.      *
  2616.      * @param remote The name of the remote file.
  2617.      * @param local  The local OutputStream to which to write the file.
  2618.      * @return True if successfully completed, false if not.
  2619.      * @throws FTPConnectionClosedException                  If the FTP server prematurely closes the connection as a result of the client being idle or some
  2620.      *                                                       other reason causing the server to send FTP reply code 421. This exception may be caught either as
  2621.      *                                                       an IOException or independently as itself.
  2622.      * @throws org.apache.commons.net.io.CopyStreamException If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to
  2623.      *                                                       determine the number of bytes transferred and the IOException causing the error. This exception may
  2624.      *                                                       be caught either as an IOException or independently as itself.
  2625.      * @throws IOException                                   If an I/O error occurs while either sending a command to the server or receiving a reply from the
  2626.      *                                                       server.
  2627.      */
  2628.     public boolean retrieveFile(final String remote, final OutputStream local) throws IOException {
  2629.         return _retrieveFile(FTPCmd.RETR.getCommand(), remote, local);
  2630.     }

  2631.     /**
  2632.      * Returns an InputStream from which a named file from the server can be read. If the current file type is ASCII, the returned InputStream will convert line
  2633.      * separators in the file to the local representation. You must close the InputStream when you finish reading from it. The InputStream itself will take care
  2634.      * of closing the parent data connection socket upon being closed.
  2635.      * <p>
  2636.      * <b>To finalize the file transfer you must call {@link #completePendingCommand completePendingCommand } and check its return value to verify success.</b>
  2637.      * If this is not done, subsequent commands may behave unexpectedly.
  2638.      * <p>
  2639.      * Note: if you have used {@link #setRestartOffset(long)}, the file data will start from the selected offset.
  2640.      *
  2641.      * @param remote The name of the remote file.
  2642.      * @return An InputStream from which the remote file can be read. If the data connection cannot be opened (e.g., the file does not exist), null is returned
  2643.      *         (in which case you may check the reply code to determine the exact reason for failure).
  2644.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2645.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2646.      *                                      independently as itself.
  2647.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2648.      */
  2649.     public InputStream retrieveFileStream(final String remote) throws IOException {
  2650.         return _retrieveFileStream(FTPCmd.RETR.getCommand(), remote);
  2651.     }

  2652.     /**
  2653.      * Sends a NOOP command to the FTP server. This is useful for preventing server timeouts.
  2654.      *
  2655.      * @return True if successfully completed, false if not.
  2656.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2657.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2658.      *                                      independently as itself.
  2659.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2660.      */
  2661.     public boolean sendNoOp() throws IOException {
  2662.         return FTPReply.isPositiveCompletion(noop());
  2663.     }

  2664.     /**
  2665.      * Send a site specific command.
  2666.      *
  2667.      * @param arguments The site specific command and arguments.
  2668.      * @return True if successfully completed, false if not.
  2669.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2670.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2671.      *                                      independently as itself.
  2672.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2673.      */
  2674.     public boolean sendSiteCommand(final String arguments) throws IOException {
  2675.         return FTPReply.isPositiveCompletion(site(arguments));
  2676.     }

  2677.     /**
  2678.      * Sets the external IP address in active mode. Useful when there are multiple network cards.
  2679.      *
  2680.      * @param ipAddress The external IP address of this machine.
  2681.      * @throws UnknownHostException if the ipAddress cannot be resolved
  2682.      * @since 2.2
  2683.      */
  2684.     public void setActiveExternalIPAddress(final String ipAddress) throws UnknownHostException {
  2685.         this.activeExternalHost = InetAddress.getByName(ipAddress);
  2686.     }

  2687.     /**
  2688.      * Sets the client side port range in active mode.
  2689.      *
  2690.      * @param minPort The lowest available port (inclusive).
  2691.      * @param maxPort The highest available port (inclusive).
  2692.      * @since 2.2
  2693.      */
  2694.     public void setActivePortRange(final int minPort, final int maxPort) {
  2695.         this.activeMinPort = minPort;
  2696.         this.activeMaxPort = maxPort;
  2697.     }

  2698.     /**
  2699.      * Enables or disables automatic server encoding detection (only UTF-8 supported).
  2700.      * <p>
  2701.      * Does not affect existing connections; must be invoked before a connection is established.
  2702.      * </p>
  2703.      *
  2704.      * @param autodetect If true, automatic server encoding detection will be enabled.
  2705.      */
  2706.     public void setAutodetectUTF8(final boolean autodetect) {
  2707.         autodetectEncoding = autodetect;
  2708.     }

  2709.     /**
  2710.      * Sets the internal buffer size for buffered data streams.
  2711.      *
  2712.      * @param bufSize The size of the buffer. Use a non-positive value to use the default.
  2713.      */
  2714.     public void setBufferSize(final int bufSize) {
  2715.         bufferSize = bufSize;
  2716.     }

  2717.     /**
  2718.      * Sets the duration to wait for control keep-alive message replies.
  2719.      *
  2720.      * @param timeout duration to wait (defaults to 1,000). Zero (or less) disables.
  2721.      * @since 3.0
  2722.      * @see #setControlKeepAliveTimeout(Duration)
  2723.      */
  2724.     public void setControlKeepAliveReplyTimeout(final Duration timeout) {
  2725.         controlKeepAliveReplyTimeout = DurationUtils.zeroIfNull(timeout);
  2726.     }

  2727.     /**
  2728.      * Sets the duration to wait for control keep-alive message replies.
  2729.      *
  2730.      * @deprecated Use {@link #setControlKeepAliveReplyTimeout(Duration)}.
  2731.      * @param timeoutMillis number of milliseconds to wait (defaults to 1,000).
  2732.      * @since 3.0
  2733.      * @see #setControlKeepAliveTimeout(long)
  2734.      */
  2735.     @Deprecated
  2736.     public void setControlKeepAliveReplyTimeout(final int timeoutMillis) {
  2737.         controlKeepAliveReplyTimeout = Duration.ofMillis(timeoutMillis);
  2738.     }

  2739.     /**
  2740.      * Sets the duration to wait between sending control connection keepalive messages when processing file upload or download.
  2741.      * <p>
  2742.      * See the class Javadoc section "Control channel keep-alive feature"
  2743.      * </p>
  2744.      *
  2745.      * @param controlIdle the duration to wait between keepalive messages. Zero (or less) disables.
  2746.      * @since 3.9.0
  2747.      * @see #setControlKeepAliveReplyTimeout(Duration)
  2748.      */
  2749.     public void setControlKeepAliveTimeout(final Duration controlIdle) {
  2750.         controlKeepAliveTimeout = DurationUtils.zeroIfNull(controlIdle);
  2751.     }

  2752.     /**
  2753.      * Sets the duration to wait between sending control connection keepalive messages when processing file upload or download.
  2754.      * <p>
  2755.      * See the class Javadoc section "Control channel keep-alive feature"
  2756.      * </p>
  2757.      *
  2758.      * @deprecated Use {@link #setControlKeepAliveTimeout(Duration)}.
  2759.      * @param controlIdleSeconds the wait in seconds between keepalive messages. Zero (or less) disables.
  2760.      * @since 3.0
  2761.      * @see #setControlKeepAliveReplyTimeout(int)
  2762.      */
  2763.     @Deprecated
  2764.     public void setControlKeepAliveTimeout(final long controlIdleSeconds) {
  2765.         controlKeepAliveTimeout = Duration.ofSeconds(controlIdleSeconds);
  2766.     }

  2767.     /**
  2768.      * Sets the listener to be used when performing store/retrieve operations. The default value (if not set) is {@code null}.
  2769.      *
  2770.      * @param listener to be used, may be {@code null} to disable
  2771.      * @since 3.0
  2772.      */
  2773.     public void setCopyStreamListener(final CopyStreamListener listener) {
  2774.         copyStreamListener = listener;
  2775.     }

  2776.     /**
  2777.      * Sets the timeout to use when reading from the data connection. This timeout will be set immediately after opening the data connection, provided that the
  2778.      * value is &ge; 0.
  2779.      * <p>
  2780.      * <b>Note:</b> the timeout will also be applied when calling accept() whilst establishing an active local data connection.
  2781.      * </p>
  2782.      *
  2783.      * @param timeout The default timeout that is used when opening a data connection socket. The value 0 (or null) means an infinite timeout.
  2784.      * @since 3.9.0
  2785.      */
  2786.     public void setDataTimeout(final Duration timeout) {
  2787.         dataTimeout = DurationUtils.zeroIfNull(timeout);
  2788.     }

  2789.     /**
  2790.      * Sets the timeout in milliseconds to use when reading from the data connection. This timeout will be set immediately after opening the data connection,
  2791.      * provided that the value is &ge; 0.
  2792.      * <p>
  2793.      * <b>Note:</b> the timeout will also be applied when calling accept() whilst establishing an active local data connection.
  2794.      * </p>
  2795.      *
  2796.      * @deprecated Use {@link #setDataTimeout(Duration)}.
  2797.      * @param timeoutMillis The default timeout in milliseconds that is used when opening a data connection socket. The value 0 means an infinite timeout.
  2798.      */
  2799.     @Deprecated
  2800.     public void setDataTimeout(final int timeoutMillis) {
  2801.         dataTimeout = Duration.ofMillis(timeoutMillis);
  2802.     }

  2803.     /**
  2804.      * Sets the file structure. The default structure is <code>FTP.FILE_STRUCTURE</code> if this method is never called or if a connect method is called.
  2805.      *
  2806.      * @param structure The structure of the file (one of the FTP class <code>_STRUCTURE</code> constants).
  2807.      * @return True if successfully completed, false if not.
  2808.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2809.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2810.      *                                      independently as itself.
  2811.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2812.      */
  2813.     public boolean setFileStructure(final int structure) throws IOException {
  2814.         if (FTPReply.isPositiveCompletion(stru(structure))) {
  2815.             fileStructure = structure;
  2816.             return true;
  2817.         }
  2818.         return false;
  2819.     }

  2820.     /**
  2821.      * Sets the transfer mode. The default transfer mode <code>FTP.STREAM_TRANSFER_MODE</code> if this method is never called or if a connect method is
  2822.      * called.
  2823.      *
  2824.      * @param mode The new transfer mode to use (one of the FTP class <code>_TRANSFER_MODE</code> constants).
  2825.      * @return True if successfully completed, false if not.
  2826.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2827.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2828.      *                                      independently as itself.
  2829.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2830.      */
  2831.     public boolean setFileTransferMode(final int mode) throws IOException {
  2832.         if (FTPReply.isPositiveCompletion(mode(mode))) {
  2833.             fileTransferMode = mode;
  2834.             return true;
  2835.         }
  2836.         return false;
  2837.     }

  2838.     /**
  2839.      * Sets the file type to be transferred. This should be one of <code>FTP.ASCII_FILE_TYPE</code>, <code>FTP.BINARY_FILE_TYPE</code>, etc. The file type
  2840.      * only needs to be set when you want to change the type. After changing it, the new type stays in effect until you change it again. The default file type
  2841.      * is <code>FTP.ASCII_FILE_TYPE</code> if this method is never called. <br>
  2842.      * The server default is supposed to be ASCII (see RFC 959), however many ftp servers default to BINARY. <b>To ensure correct operation with all servers,
  2843.      * always specify the appropriate file type after connecting to the server.</b> <br>
  2844.      * <p>
  2845.      * <b>N.B.</b> currently calling any connect method will reset the type to FTP.ASCII_FILE_TYPE.
  2846.      *
  2847.      * @param fileType The <code>_FILE_TYPE</code> constant indicating the type of file.
  2848.      * @return True if successfully completed, false if not.
  2849.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2850.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2851.      *                                      independently as itself.
  2852.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2853.      */
  2854.     public boolean setFileType(final int fileType) throws IOException {
  2855.         if (FTPReply.isPositiveCompletion(type(fileType))) {
  2856.             this.fileType = fileType;
  2857.             this.fileFormat = FTP.NON_PRINT_TEXT_FORMAT;
  2858.             return true;
  2859.         }
  2860.         return false;
  2861.     }

  2862.     /**
  2863.      * Sets the file type to be transferred and the format. The type should be one of <code>FTP.ASCII_FILE_TYPE</code>, <code>FTP.BINARY_FILE_TYPE</code>,
  2864.      * etc. The file type only needs to be set when you want to change the type. After changing it, the new type stays in effect until you change it again. The
  2865.      * default file type is <code>FTP.ASCII_FILE_TYPE</code> if this method is never called. <br>
  2866.      * The server default is supposed to be ASCII (see RFC 959), however many ftp servers default to BINARY. <b>To ensure correct operation with all servers,
  2867.      * always specify the appropriate file type after connecting to the server.</b> <br>
  2868.      * The format should be one of the FTP class <code>TEXT_FORMAT</code> constants, or if the type is <code>FTP.LOCAL_FILE_TYPE</code>, the format should
  2869.      * be the byte size for that type. The default format is <code>FTP.NON_PRINT_TEXT_FORMAT</code> if this method is never called.
  2870.      * <p>
  2871.      * <b>N.B.</b> currently calling any connect method will reset the type to FTP.ASCII_FILE_TYPE and the formatOrByteSize to FTP.NON_PRINT_TEXT_FORMAT.
  2872.      * </p>
  2873.      *
  2874.      * @param fileType         The <code>_FILE_TYPE</code> constant indicating the type of file.
  2875.      * @param formatOrByteSize The format of the file (one of the <code>_FORMAT</code> constants). In the case of <code>LOCAL_FILE_TYPE</code>, the byte size.
  2876.      * @return True if successfully completed, false if not.
  2877.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  2878.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  2879.      *                                      independently as itself.
  2880.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  2881.      */
  2882.     public boolean setFileType(final int fileType, final int formatOrByteSize) throws IOException {
  2883.         if (FTPReply.isPositiveCompletion(type(fileType, formatOrByteSize))) {
  2884.             this.fileType = fileType;
  2885.             this.fileFormat = formatOrByteSize;
  2886.             return true;
  2887.         }
  2888.         return false;
  2889.     }

  2890.     /**
  2891.      * Sets whether the IP address from the server's response should be used. Until 3.9.0, this has always been the case. Beginning with 3.9.0, that IP address
  2892.      * will be silently ignored, and replaced with the remote IP address of the control connection, unless this configuration option is given, which restores
  2893.      * the old behavior. To enable this by default, use the system property {@link FTPClient#FTP_IP_ADDRESS_FROM_PASV_RESPONSE}.
  2894.      *
  2895.      * @param usingIpAddressFromPasvResponse True, if the IP address from the server's response should be used (pre-3.9.0 compatible behavior), or false (ignore
  2896.      *                                       that IP address).
  2897.      * @see FTPClient#FTP_IP_ADDRESS_FROM_PASV_RESPONSE
  2898.      * @see #isIpAddressFromPasvResponse
  2899.      * @since 3.9.0
  2900.      */
  2901.     public void setIpAddressFromPasvResponse(final boolean usingIpAddressFromPasvResponse) {
  2902.         this.ipAddressFromPasvResponse = usingIpAddressFromPasvResponse;
  2903.     }

  2904.     /**
  2905.      * You can set this to true if you would like to get hidden files when {@link #listFiles} too. A <code>LIST -a</code> will be issued to the ftp server. It
  2906.      * depends on your ftp server if you need to call this method, also don't expect to get rid of hidden files if you call this method with "false".
  2907.      *
  2908.      * @param listHiddenFiles true if hidden files should be listed
  2909.      * @since 2.0
  2910.      */
  2911.     public void setListHiddenFiles(final boolean listHiddenFiles) {
  2912.         this.listHiddenFiles = listHiddenFiles;
  2913.     }

  2914.     /**
  2915.      * Issue the FTP MFMT command (not supported by all servers) which sets the last modified time of a file.
  2916.      *
  2917.      * The timestamp should be in the form <code>yyyyMMDDhhmmss</code>. It should also be in GMT, but not all servers honor this.
  2918.      *
  2919.      * An FTP server would indicate its support of this feature by including "MFMT" in its response to the FEAT command, which may be retrieved by
  2920.      * FTPClient.features()
  2921.      *
  2922.      * @param pathname The file path for which last modified time is to be changed.
  2923.      * @param timeval  The timestamp to set to, in <code>yyyyMMDDhhmmss</code> format.
  2924.      * @return true if successfully set, false if not
  2925.      * @throws IOException if an I/O error occurs.
  2926.      * @since 2.2
  2927.      * @see <a href="https://tools.ietf.org/html/draft-somers-ftp-mfxx-04">https://tools.ietf.org/html/draft-somers-ftp-mfxx-04</a>
  2928.      */
  2929.     public boolean setModificationTime(final String pathname, final String timeval) throws IOException {
  2930.         return FTPReply.isPositiveCompletion(mfmt(pathname, timeval));
  2931.     }

  2932.     /**
  2933.      * set the factory used for parser creation to the supplied factory object.
  2934.      *
  2935.      * @param parserFactory factory object used to create FTPFileEntryParsers
  2936.      *
  2937.      * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
  2938.      * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
  2939.      */
  2940.     public void setParserFactory(final FTPFileEntryParserFactory parserFactory) {
  2941.         this.parserFactory = parserFactory;
  2942.     }

  2943.     /**
  2944.      * Sets the local IP address to use in passive mode. Useful when there are multiple network cards.
  2945.      *
  2946.      * @param inetAddress The local IP address of this machine.
  2947.      */
  2948.     public void setPassiveLocalIPAddress(final InetAddress inetAddress) {
  2949.         this.passiveLocalHost = inetAddress;
  2950.     }

  2951.     /**
  2952.      * Sets the local IP address to use in passive mode. Useful when there are multiple network cards.
  2953.      *
  2954.      * @param ipAddress The local IP address of this machine.
  2955.      * @throws UnknownHostException if the ipAddress cannot be resolved
  2956.      */
  2957.     public void setPassiveLocalIPAddress(final String ipAddress) throws UnknownHostException {
  2958.         this.passiveLocalHost = InetAddress.getByName(ipAddress);
  2959.     }

  2960.     /**
  2961.      * Enables or disables passive mode NAT workaround. If enabled, a site-local PASV mode reply address will be replaced with the remote host address to which
  2962.      * the PASV mode request was sent (unless that is also a site local address). This gets around the problem that some NAT boxes may change the reply.
  2963.      * <p>
  2964.      * The default is true, i.e. site-local replies are replaced.
  2965.      * </p>
  2966.      *
  2967.      * @deprecated (3.6) use {@link #setPassiveNatWorkaroundStrategy(HostnameResolver)} instead
  2968.      * @param enabled true to enable replacing internal IP's in passive mode.
  2969.      */
  2970.     @Deprecated
  2971.     public void setPassiveNatWorkaround(final boolean enabled) {
  2972.         this.passiveNatWorkaroundStrategy = enabled ? new NatServerResolverImpl(this) : null;
  2973.     }

  2974.     /**
  2975.      * Sets the workaround strategy to replace the PASV mode reply addresses. This gets around the problem that some NAT boxes may change the reply.
  2976.      *
  2977.      * The default implementation is {@code NatServerResolverImpl}, i.e. site-local replies are replaced.
  2978.      *
  2979.      * @param resolver strategy to replace internal IP's in passive mode or null to disable the workaround (i.e. use PASV mode reply address.)
  2980.      * @since 3.6
  2981.      */
  2982.     public void setPassiveNatWorkaroundStrategy(final HostnameResolver resolver) {
  2983.         this.passiveNatWorkaroundStrategy = resolver;
  2984.     }

  2985.     /**
  2986.      * Sets the value to be used for the data socket SO_RCVBUF option. If the value is positive, the option will be set when the data socket has been created.
  2987.      *
  2988.      * @param bufSize The size of the buffer, zero or negative means the value is ignored.
  2989.      * @since 3.3
  2990.      */
  2991.     public void setReceieveDataSocketBufferSize(final int bufSize) {
  2992.         receiveDataSocketBufferSize = bufSize;
  2993.     }

  2994.     /**
  2995.      * Enable or disable verification that the remote host taking part of a data connection is the same as the host to which the control connection is attached.
  2996.      * The default is for verification to be enabled. You may set this value at any time, whether the FTPClient is currently connected or not.
  2997.      *
  2998.      * @param enable True to enable verification, false to disable verification.
  2999.      */
  3000.     public void setRemoteVerificationEnabled(final boolean enable) {
  3001.         remoteVerificationEnabled = enable;
  3002.     }

  3003.     /**
  3004.      * Sets the external IP address to report in EPRT/PORT commands in active mode. Useful when there are multiple network cards.
  3005.      *
  3006.      * @param ipAddress The external IP address of this machine.
  3007.      * @throws UnknownHostException if the ipAddress cannot be resolved
  3008.      * @since 3.1
  3009.      * @see #getReportHostAddress()
  3010.      */
  3011.     public void setReportActiveExternalIPAddress(final String ipAddress) throws UnknownHostException {
  3012.         this.reportActiveExternalHost = InetAddress.getByName(ipAddress);
  3013.     }

  3014.     /**
  3015.      * Sets the restart offset for file transfers.
  3016.      * <p>
  3017.      * The restart command is not sent to the server immediately. It is sent when a data connection is created as part of a subsequent command. The restart
  3018.      * marker is reset to zero after use.
  3019.      * </p>
  3020.      * <p>
  3021.      * <b>Note: This method should only be invoked immediately prior to the transfer to which it applies.</b>
  3022.      * </p>
  3023.      *
  3024.      * @param offset The offset into the remote file at which to start the next file transfer. This must be a value greater than or equal to zero.
  3025.      */
  3026.     public void setRestartOffset(final long offset) {
  3027.         if (offset >= 0) {
  3028.             restartOffset = offset;
  3029.         }
  3030.     }

  3031.     /**
  3032.      * Sets the value to be used for the data socket SO_SNDBUF option. If the value is positive, the option will be set when the data socket has been created.
  3033.      *
  3034.      * @param bufSize The size of the buffer, zero or negative means the value is ignored.
  3035.      * @since 3.3
  3036.      */
  3037.     public void setSendDataSocketBufferSize(final int bufSize) {
  3038.         sendDataSocketBufferSize = bufSize;
  3039.     }

  3040.     /**
  3041.      * Sets whether to use EPSV with IPv4. Might be worth enabling in some circumstances.
  3042.      *
  3043.      * For example, when using IPv4 with NAT it may work with some rare configurations. E.g. if FTP server has a static PASV address (external network) and the
  3044.      * client is coming from another internal network. In that case the data connection after PASV command would fail, while EPSV would make the client succeed
  3045.      * by taking just the port.
  3046.      *
  3047.      * @param selected value to set.
  3048.      * @since 2.2
  3049.      */
  3050.     public void setUseEPSVwithIPv4(final boolean selected) {
  3051.         this.useEPSVwithIPv4 = selected;
  3052.     }

  3053.     private boolean storeFile(final FTPCmd command, final String remote, final InputStream local) throws IOException {
  3054.         return _storeFile(command.getCommand(), remote, local);
  3055.     }

  3056.     /**
  3057.      * Stores a file on the server using the given name and taking input from the given InputStream. This method does NOT close the given InputStream. If the
  3058.      * current file type is ASCII, line separators in the file are transparently converted to the NETASCII format (i.e., you should not attempt to create a
  3059.      * special InputStream to do this).
  3060.      *
  3061.      * @param remote The name to give the remote file.
  3062.      * @param local  The local InputStream from which to read the file.
  3063.      * @return True if successfully completed, false if not.
  3064.      * @throws FTPConnectionClosedException                  If the FTP server prematurely closes the connection as a result of the client being idle or some
  3065.      *                                                       other reason causing the server to send FTP reply code 421. This exception may be caught either as
  3066.      *                                                       an IOException or independently as itself.
  3067.      * @throws org.apache.commons.net.io.CopyStreamException If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to
  3068.      *                                                       determine the number of bytes transferred and the IOException causing the error. This exception may
  3069.      *                                                       be caught either as an IOException or independently as itself.
  3070.      * @throws IOException                                   If an I/O error occurs while either sending a command to the server or receiving a reply from the
  3071.      *                                                       server.
  3072.      */
  3073.     public boolean storeFile(final String remote, final InputStream local) throws IOException {
  3074.         return storeFile(FTPCmd.STOR, remote, local);
  3075.     }

  3076.     private OutputStream storeFileStream(final FTPCmd command, final String remote) throws IOException {
  3077.         return _storeFileStream(command.getCommand(), remote);
  3078.     }

  3079.     /**
  3080.      * Returns an OutputStream through which data can be written to store a file on the server using the given name. If the current file type is ASCII, the
  3081.      * returned OutputStream will convert line separators in the file to the NETASCII format (i.e., you should not attempt to create a special OutputStream to
  3082.      * do this). You must close the OutputStream when you finish writing to it. The OutputStream itself will take care of closing the parent data connection
  3083.      * socket upon being closed.
  3084.      * <p>
  3085.      * <b>To finalize the file transfer you must call {@link #completePendingCommand completePendingCommand } and check its return value to verify success.</b>
  3086.      * If this is not done, subsequent commands may behave unexpectedly.
  3087.      * </p>
  3088.      *
  3089.      * @param remote The name to give the remote file.
  3090.      * @return An OutputStream through which the remote file can be written. If the data connection cannot be opened (e.g., the file does not exist), null is
  3091.      *         returned (in which case you may check the reply code to determine the exact reason for failure).
  3092.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  3093.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  3094.      *                                      independently as itself.
  3095.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  3096.      */
  3097.     public OutputStream storeFileStream(final String remote) throws IOException {
  3098.         return storeFileStream(FTPCmd.STOR, remote);
  3099.     }

  3100.     /**
  3101.      * Stores a file on the server using a unique name assigned by the server and taking input from the given InputStream. This method does NOT close the given
  3102.      * InputStream. If the current file type is ASCII, line separators in the file are transparently converted to the NETASCII format (i.e., you should not
  3103.      * attempt to create a special InputStream to do this).
  3104.      *
  3105.      * @param local The local InputStream from which to read the file.
  3106.      * @return True if successfully completed, false if not.
  3107.      * @throws FTPConnectionClosedException                  If the FTP server prematurely closes the connection as a result of the client being idle or some
  3108.      *                                                       other reason causing the server to send FTP reply code 421. This exception may be caught either as
  3109.      *                                                       an IOException or independently as itself.
  3110.      * @throws org.apache.commons.net.io.CopyStreamException If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to
  3111.      *                                                       determine the number of bytes transferred and the IOException causing the error. This exception may
  3112.      *                                                       be caught either as an IOException or independently as itself.
  3113.      * @throws IOException                                   If an I/O error occurs while either sending a command to the server or receiving a reply from the
  3114.      *                                                       server.
  3115.      */
  3116.     public boolean storeUniqueFile(final InputStream local) throws IOException {
  3117.         return storeFile(FTPCmd.STOU, null, local);
  3118.     }

  3119.     /**
  3120.      * Stores a file on the server using a unique name derived from the given name and taking input from the given InputStream. This method does NOT close the
  3121.      * given InputStream. If the current file type is ASCII, line separators in the file are transparently converted to the NETASCII format (i.e., you should
  3122.      * not attempt to create a special InputStream to do this).
  3123.      *
  3124.      * @param remote The name on which to base the unique name given to the remote file.
  3125.      * @param local  The local InputStream from which to read the file.
  3126.      * @return True if successfully completed, false if not.
  3127.      * @throws FTPConnectionClosedException                  If the FTP server prematurely closes the connection as a result of the client being idle or some
  3128.      *                                                       other reason causing the server to send FTP reply code 421. This exception may be caught either as
  3129.      *                                                       an IOException or independently as itself.
  3130.      * @throws org.apache.commons.net.io.CopyStreamException If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to
  3131.      *                                                       determine the number of bytes transferred and the IOException causing the error. This exception may
  3132.      *                                                       be caught either as an IOException or independently as itself.
  3133.      * @throws IOException                                   If an I/O error occurs while either sending a command to the server or receiving a reply from the
  3134.      *                                                       server.
  3135.      */
  3136.     public boolean storeUniqueFile(final String remote, final InputStream local) throws IOException {
  3137.         return storeFile(FTPCmd.STOU, remote, local);
  3138.     }

  3139.     /**
  3140.      * Returns an OutputStream through which data can be written to store a file on the server using a unique name assigned by the server. If the current file
  3141.      * type is ASCII, the returned OutputStream will convert line separators in the file to the NETASCII format (i.e., you should not attempt to create a
  3142.      * special OutputStream to do this). You must close the OutputStream when you finish writing to it. The OutputStream itself will take care of closing the
  3143.      * parent data connection socket upon being closed.
  3144.      * <p>
  3145.      * <b>To finalize the file transfer you must call {@link #completePendingCommand completePendingCommand } and check its return value to verify success.</b>
  3146.      * If this is not done, subsequent commands may behave unexpectedly.
  3147.      * </p>
  3148.      *
  3149.      * @return An OutputStream through which the remote file can be written. If the data connection cannot be opened (e.g., the file does not exist), null is
  3150.      *         returned (in which case you may check the reply code to determine the exact reason for failure).
  3151.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  3152.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  3153.      *                                      independently as itself.
  3154.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  3155.      */
  3156.     public OutputStream storeUniqueFileStream() throws IOException {
  3157.         return storeFileStream(FTPCmd.STOU, null);
  3158.     }

  3159.     /**
  3160.      * Returns an OutputStream through which data can be written to store a file on the server using a unique name derived from the given name. If the current
  3161.      * file type is ASCII, the returned OutputStream will convert line separators in the file to the NETASCII format (i.e., you should not attempt to create a
  3162.      * special OutputStream to do this). You must close the OutputStream when you finish writing to it. The OutputStream itself will take care of closing the
  3163.      * parent data connection socket upon being closed.
  3164.      * <p>
  3165.      * <b>To finalize the file transfer you must call {@link #completePendingCommand completePendingCommand } and check its return value to verify success.</b>
  3166.      * If this is not done, subsequent commands may behave unexpectedly.
  3167.      *
  3168.      * @param remote The name on which to base the unique name given to the remote file.
  3169.      * @return An OutputStream through which the remote file can be written. If the data connection cannot be opened (e.g., the file does not exist), null is
  3170.      *         returned (in which case you may check the reply code to determine the exact reason for failure).
  3171.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  3172.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  3173.      *                                      independently as itself.
  3174.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  3175.      */
  3176.     public OutputStream storeUniqueFileStream(final String remote) throws IOException {
  3177.         return storeFileStream(FTPCmd.STOU, remote);
  3178.     }

  3179.     /**
  3180.      * Issue the FTP SMNT command.
  3181.      *
  3182.      * @param pathname The pathname to mount.
  3183.      * @return True if successfully completed, false if not.
  3184.      * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
  3185.      *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
  3186.      *                                      independently as itself.
  3187.      * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
  3188.      */
  3189.     public boolean structureMount(final String pathname) throws IOException {
  3190.         return FTPReply.isPositiveCompletion(smnt(pathname));
  3191.     }

  3192.     private Socket wrapOnDeflate(final Socket plainSocket) {
  3193.         switch (fileTransferMode) {
  3194.         case DEFLATE_TRANSFER_MODE:
  3195.             return new DeflateSocket(plainSocket);
  3196.         // Experiment, not in an RFC?
  3197.         // case GZIP_TRANSFER_MODE:
  3198.             //return new GZIPSocket(plainSocket);
  3199.         default:
  3200.             return plainSocket;
  3201.         }
  3202.     }
  3203. }