    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.net.ftp;
    018
    019import java.io.BufferedInputStream;
    020import java.io.BufferedOutputStream;
    021import java.io.BufferedWriter;
    022import java.io.IOException;
    023import java.io.InputStream;
    024import java.io.InputStreamReader;
    025import java.io.OutputStream;
    026import java.io.OutputStreamWriter;
    027import java.io.Reader;
    028import java.net.Inet6Address;
    029import java.net.InetAddress;
    030import java.net.InetSocketAddress;
    031import java.net.ServerSocket;
    032import java.net.Socket;
    033import java.net.SocketException;
    034import java.net.SocketTimeoutException;
    035import java.net.UnknownHostException;
    036import java.nio.charset.Charset;
    037import java.nio.charset.StandardCharsets;
    038import java.time.Duration;
    039import java.time.Instant;
    040import java.util.ArrayList;
    041import java.util.Calendar;
    042import java.util.HashMap;
    043import java.util.HashSet;
    044import java.util.List;
    045import java.util.Locale;
    046import java.util.Properties;
    047import java.util.Random;
    048import java.util.Set;
    049import java.util.regex.Matcher;
    050import java.util.regex.Pattern;
    051
    052import org.apache.commons.io.IOUtils;
    053import org.apache.commons.net.MalformedServerReplyException;
    054import org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory;
    055import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory;
    056import org.apache.commons.net.ftp.parser.MLSxEntryParser;
    057import org.apache.commons.net.io.CRLFLineReader;
    058import org.apache.commons.net.io.CopyStreamAdapter;
    059import org.apache.commons.net.io.CopyStreamEvent;
    060import org.apache.commons.net.io.CopyStreamListener;
    061import org.apache.commons.net.io.FromNetASCIIInputStream;
    062import org.apache.commons.net.io.SocketOutputStream;
    063import org.apache.commons.net.io.ToNetASCIIOutputStream;
    064import org.apache.commons.net.io.Util;
    065import org.apache.commons.net.util.NetConstants;
    066
    067/**
    068 * 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
    069 * interacting with an FTP server and provides a convenient higher level interface. As with all classes derived from
    070 * {@link org.apache.commons.net.SocketClient}, you must first connect to the server with {@link org.apache.commons.net.SocketClient#connect connect} before
    071 * doing anything, and finally {@link org.apache.commons.net.SocketClient#disconnect() disconnect} after you're completely finished interacting with the server.
    072 * Then you need to check the FTP reply code to see if the connection was successful. For example:
    073 *
    074 * <pre>
    075 *    FTPClient ftp = new FTPClient();
    076 *    FTPClientConfig config = new FTPClientConfig();
    077 *    config.setXXX(YYY); // change required options
    078 *    // for example config.setServerTimeZoneId("Pacific/Pitcairn")
    079 *    ftp.configure(config );
    080 *    boolean error = false;
    081 *    try {
    082 *      int reply;
    083 *      String server = "ftp.example.com";
    084 *      ftp.connect(server);
    085 *      System.out.println("Connected to " + server + ".");
    086 *      System.out.print(ftp.getReplyString());
    087 *
    088 *      // After connection attempt, you should check the reply code to verify
    089 *      // success.
    090 *      reply = ftp.getReplyCode();
    091 *
    092 *      if (!FTPReply.isPositiveCompletion(reply)) {
    093 *        ftp.disconnect();
    094 *        System.err.println("FTP server refused connection.");
    095 *        System.exit(1);
    096 *      }
    097 *      ... // transfer files
    098 *      ftp.logout();
    099 *    } catch (IOException e) {
    100 *      error = true;
    101 *      e.printStackTrace();
    102 *    } finally {
    103 *      if (ftp.isConnected()) {
    104 *        try {
    105 *          ftp.disconnect();
    106 *        } catch (IOException ioe) {
    107 *          // do nothing
    108 *        }
    109 *      }
    110 *      System.exit(error ? 1 : 0);
    111 *    }
    112 * </pre>
    113 * <p>
    114 * 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
    115 * 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
    116 * 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
    117 * 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
    118 * 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.
    119 * </p>
    120 * <p>
    121 * The default settings for FTPClient are for it to use {@link FTP#ASCII_FILE_TYPE}, {@link FTP#NON_PRINT_TEXT_FORMAT}, {@link FTP#STREAM_TRANSFER_MODE}, and
    122 * {@link FTP#FILE_STRUCTURE}. The only file types directly supported are {@link FTP#ASCII_FILE_TYPE} and {@link FTP#BINARY_FILE_TYPE}. Because there are at
    123 * least 4 different EBCDIC encodings, we have opted not to provide direct support for EBCDIC. To transfer EBCDIC and other unsupported file types you must
    124 * create your own filter InputStreams and OutputStreams and wrap them around the streams returned or required by the FTPClient methods. FTPClient uses the
    125 * {@link ToNetASCIIOutputStream NetASCII} filter streams to provide transparent handling of ASCII files. We will consider incorporating EBCDIC support if there
    126 * is enough demand.
    127 * </p>
    128 * <p>
    129 * {@link FTP#NON_PRINT_TEXT_FORMAT}, {@link FTP#STREAM_TRANSFER_MODE}, and {@link FTP#FILE_STRUCTURE} are the only supported formats, transfer modes, and file
    130 * structures.
    131 * </p>
    132 * <p>
    133 * Because the handling of sockets on different platforms can differ significantly, the FTPClient automatically issues a new PORT (or EPRT) command prior to
    134 * every transfer requiring that the server connect to the client's data port. This ensures identical problem-free behavior on Windows, Unix, and Macintosh
    135 * platforms. Additionally, it relieves programmers from having to issue the PORT (or EPRT) command themselves and dealing with platform dependent issues.
    136 * </p>
    137 * <p>
    138 * Additionally, for security purposes, all data connections to the client are verified to ensure that they originated from the intended party (host and port).
    139 * 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
    140 * {@link #setRemoteVerificationEnabled setRemoteVerificationEnabled()}.
    141 * </p>
    142 * <p>
    143 * 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
    144 * (usually 900 seconds). The FTPClient class will detect a premature FTP server connection closing when it receives a
    145 * {@link org.apache.commons.net.ftp.FTPReply#SERVICE_NOT_AVAILABLE FTPReply.SERVICE_NOT_AVAILABLE} response to a command. When that occurs, the FTP class
    146 * method encountering that reply will throw an {@link org.apache.commons.net.ftp.FTPConnectionClosedException}. {@link FTPConnectionClosedException} is a
    147 * subclass of {@code IOException} and therefore need not be caught separately, but if you are going to catch it separately, its catch block must appear before
    148 * the more general {@code IOException} catch block. When you encounter an {@link org.apache.commons.net.ftp.FTPConnectionClosedException} , you must disconnect
    149 * the connection with {@link #disconnect disconnect()} to properly clean up the system resources used by FTPClient. Before disconnecting, you may check the
    150 * last reply code and text with {@link org.apache.commons.net.ftp.FTP#getReplyCode getReplyCode}, {@link org.apache.commons.net.ftp.FTP#getReplyString
    151 * getReplyString }, and {@link org.apache.commons.net.ftp.FTP#getReplyStrings getReplyStrings}. You may avoid server disconnections while the client is idle by
    152 * periodically sending NOOP commands to the server.
    153 * </p>
    154 * <p>
    155 * 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
    156 * {@link org.apache.commons.net.MalformedServerReplyException} , which is a subclass of IOException. A MalformedServerReplyException will be thrown when the
    157 * 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
    158 * lenient as possible.
    159 * </p>
    160 * <p>
    161 * Listing API Examples Both paged and unpaged examples of directory listings are available, as follows:
    162 * </p>
    163 * <p>
    164 * Unpaged (whole list) access, using a parser accessible by auto-detect:
    165 * </p>
    166 *
    167 * <pre>
    168 * FTPClient f = new FTPClient();
    169 * f.connect(server);
    170 * f.login(user, password);
    171 * FTPFile[] files = f.listFiles(directory);
    172 * </pre>
    173 * <p>
    174 * Paged access, using a parser not accessible by auto-detect. The class defined in the first parameter of initateListParsing should be derived from
    175 * org.apache.commons.net.FTPFileEntryParser:
    176 * </p>
    177 *
    178 * <pre>
    179 * FTPClient f = new FTPClient();
    180 * f.connect(server);
    181 * f.login(user, password);
    182 * FTPListParseEngine engine = f.initiateListParsing("com.whatever.YourOwnParser", directory);
    183 *
    184 * while (engine.hasNext()) {
    185 *     FTPFile[] files = engine.getNext(25); // "page size" you want
    186 *     // do whatever you want with these files, display them, etc.
    187 *     // expensive FTPFile objects not created until needed.
    188 * }
    189 * </pre>
    190 * <p>
    191 * Paged access, using a parser accessible by auto-detect:
    192 * </p>
    193 *
    194 * <pre>
    195 * FTPClient f = new FTPClient();
    196 * f.connect(server);
    197 * f.login(user, password);
    198 * FTPListParseEngine engine = f.initiateListParsing(directory);
    199 *
    200 * while (engine.hasNext()) {
    201 *     FTPFile[] files = engine.getNext(25); // "page size" you want
    202 *     // do whatever you want with these files, display them, etc.
    203 *     // expensive FTPFile objects not created until needed.
    204 * }
    205 * </pre>
    206 * <p>
    207 * For examples of using FTPClient on servers whose directory listings
    208 * </p>
    209 * <ul>
    210 * <li>use languages other than English</li>
    211 * <li>use date formats other than the American English "standard" {@code MM d yyyy}</li>
    212 * <li>are in different time zones and you need accurate timestamps for dependency checking as in Ant</li>
    213 * </ul>
    214 * see {@link FTPClientConfig FTPClientConfig}.
    215 * <p>
    216 * <strong>Control channel keep-alive feature</strong>:
    217 * </p>
    218 * <p>
    219 * <strong>Please note:</strong> this does not apply to the methods where the user is responsible for writing or reading the data stream, i.e.
    220 * {@link #retrieveFileStream(String)} , {@link #storeFileStream(String)} and the other xxxFileStream methods
    221 * </p>
    222 * <p>
    223 * 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
    224 * 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.
    225 * 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
    226 * for the router.
    227 * <p>
    228 * 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:
    229 * </p>
    230 *
    231 * <pre>
    232 * // Set timeout to 5 minutes
    233 * ftpClient.setControlKeepAliveTimeout(Duration.ofMinutes(5));
    234 * </pre>
    235 *
    236 * <p>
    237 * This will cause the file upload/download methods to send a NOOP approximately every 5 minutes. The following public methods support this:
    238 * </p>
    239 * <ul>
    240 * <li>{@link #retrieveFile(String, OutputStream)}</li>
    241 * <li>{@link #appendFile(String, InputStream)}</li>
    242 * <li>{@link #storeFile(String, InputStream)}</li>
    243 * <li>{@link #storeUniqueFile(InputStream)}</li>
    244 * <li>{@link #storeUniqueFileStream(String)}</li>
    245 * </ul>
    246 * <p>
    247 * 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)} ,
    248 * {@link #storeFileStream(String)} and the other xxxFileStream methods. In such cases, the user is responsible for keeping the control connection alive if
    249 * necessary.
    250 * </p>
    251 * <p>
    252 * The implementation currently uses a {@link CopyStreamListener} which is passed to the
    253 * {@link Util#copyStream(InputStream, OutputStream, int, long, CopyStreamListener, boolean)} method, so the timing is partially dependent on how long each
    254 * block transfer takes.
    255 * </p>
    256 * <p>
    257 * <strong>This keep-alive feature is optional; if it does not help or causes problems then don't use it.</strong>
    258 * </p>
    259 *
    260 * @see #FTP_SYSTEM_TYPE
    261 * @see #SYSTEM_TYPE_PROPERTIES
    262 * @see FTP
    263 * @see FTPConnectionClosedException
    264 * @see FTPFileEntryParser
    265 * @see FTPFileEntryParserFactory
    266 * @see DefaultFTPFileEntryParserFactory
    267 * @see FTPClientConfig
    268 * @see org.apache.commons.net.MalformedServerReplyException
    269 */
    270public class FTPClient extends FTP implements Configurable {
    271
    272    private static final class CSL implements CopyStreamListener {
    273
    274        private final FTPClient parent;
    275        private final long idleMillis;
    276        private final int currentSoTimeoutMillis;
    277
    278        private long lastIdleTimeMillis = System.currentTimeMillis();
    279        private int notAcked;
    280        private int acksAcked;
    281        private int ioErrors;
    282
    283        CSL(final FTPClient parent, final Duration idleDuration, final Duration maxWaitDuration) throws SocketException {
    284            this.idleMillis = idleDuration.toMillis();
    285            this.parent = parent;
    286            this.currentSoTimeoutMillis = parent.getSoTimeout();
    287            parent.setSoTimeout(DurationUtils.toMillisInt(maxWaitDuration));
    288        }
    289
    290        @Override
    291        public void bytesTransferred(final CopyStreamEvent event) {
    292            bytesTransferred(event.getTotalBytesTransferred(), event.getBytesTransferred(), event.getStreamSize());
    293        }
    294
    295        @Override
    296        public void bytesTransferred(final long totalBytesTransferred, final int bytesTransferred, final long streamSize) {
    297            final long nowMillis = System.currentTimeMillis();
    298            if (nowMillis - lastIdleTimeMillis > idleMillis) {
    299                try {
    300                    parent.__noop();
    301                    acksAcked++;
    302                } catch (final SocketTimeoutException e) {
    303                    notAcked++;
    304                } catch (final IOException e) {
    305                    ioErrors++;
    306                    // Ignored
    307                }
    308                lastIdleTimeMillis = nowMillis;
    309            }
    310        }
    311
    312        int[] cleanUp() throws IOException {
    313            final int remain = notAcked;
    314            try {
    315                while (notAcked > 0) {
    316                    parent.getReply(); // we do want to see these
    317                    notAcked--; // only decrement if actually received
    318                }
    319            } catch (final SocketTimeoutException e) { // NET-584
    320                // ignored
    321            } finally {
    322                parent.setSoTimeout(currentSoTimeoutMillis);
    323            }
    324            return new int[] { acksAcked, remain, notAcked, ioErrors }; // debug counts
    325        }
    326
    327    }
    328
    329    /**
    330     * Strategy interface for updating host names received from FTP server for passive NAT workaround.
    331     *
    332     * @since 3.6
    333     */
    334    public interface HostnameResolver {
    335
    336        /**
    337         * Resolves a host name.
    338         *
    339         * @param hostname the hostname to resolve.
    340         * @return The resolved hostname.
    341         * @throws UnknownHostException if the host is unknown.
    342         */
    343        String resolve(String hostname) throws UnknownHostException;
    344    }
    345
    346    /**
    347     * Default strategy for passive NAT workaround (site-local replies are replaced.)
    348     *
    349     * @since 3.6
    350     */
    351    public static class NatServerResolverImpl implements HostnameResolver {
    352        private final FTPClient client;
    353
    354        /**
    355         * Constructs a new instance.
    356         *
    357         * @param client the FTP client.
    358         */
    359        public NatServerResolverImpl(final FTPClient client) {
    360            this.client = client;
    361        }
    362
    363        @Override
    364        public String resolve(final String hostname) throws UnknownHostException {
    365            String newHostname = hostname;
    366            final InetAddress host = InetAddress.getByName(newHostname);
    367            // reply is a local address, but target is not - assume NAT box changed the PASV reply
    368            if (host.isSiteLocalAddress()) {
    369                final InetAddress remote = client.getRemoteAddress();
    370                if (!remote.isSiteLocalAddress()) {
    371                    newHostname = remote.getHostAddress();
    372                }
    373            }
    374            return newHostname;
    375        }
    376    }
    377
    378    private static final class PropertiesSingleton {
    379        static final Properties PROPERTIES = loadResourceProperties(SYSTEM_TYPE_PROPERTIES);
    380    }
    381
    382    /**
    383     * The system property ({@value}) which can be used to override the system type.<br>
    384     * If defined, the value will be used to create any automatically created parsers.
    385     *
    386     * @since 3.0
    387     */
    388    public static final String FTP_SYSTEM_TYPE = "org.apache.commons.net.ftp.systemType";
    389
    390    /**
    391     * The system property ({@value}) which can be used as the default system type.<br>
    392     * If defined, the value will be used if the SYST command fails.
    393     *
    394     * @since 3.1
    395     */
    396    public static final String FTP_SYSTEM_TYPE_DEFAULT = "org.apache.commons.net.ftp.systemType.default";
    397
    398    /**
    399     * The system property that defines the default for {@link #isIpAddressFromPasvResponse()}. This property, if present, configures the default for the
    400     * 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
    401     * 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
    402     * absent, then the client will ignore that IP address, and instead use the remote address of the control connection.
    403     *
    404     * @see #isIpAddressFromPasvResponse()
    405     * @see #setIpAddressFromPasvResponse(boolean)
    406     * @since 3.9.0
    407     */
    408    public static final String FTP_IP_ADDRESS_FROM_PASV_RESPONSE = "org.apache.commons.net.ftp.ipAddressFromPasvResponse";
    409
    410    /**
    411     * The name of an optional systemType properties file ({@value}), which is loaded using {@link Class#getResourceAsStream(String)}.<br>
    412     * The entries are the systemType (as determined by {@link FTPClient#getSystemType}) and the values are the replacement type or parserClass, which is passed
    413     * to {@link FTPFileEntryParserFactory#createFileEntryParser(String)}.<br>
    414     * For example:
    415     *
    416     * <pre>
    417     * Plan 9=Unix
    418     * OS410=org.apache.commons.net.ftp.parser.OS400FTPEntryParser
    419     * </pre>
    420     *
    421     * @since 3.0
    422     */
    423    public static final String SYSTEM_TYPE_PROPERTIES = "/systemType.properties";
    424
    425    /**
    426     * 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
    427     * client's data port to initiate a data transfer. This is the default data connection mode when and FTPClient instance is created.
    428     */
    429    public static final int ACTIVE_LOCAL_DATA_CONNECTION_MODE = 0;
    430
    431    /**
    432     * 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
    433     * should connect to the other server's data port to initiate a data transfer.
    434     */
    435    public static final int ACTIVE_REMOTE_DATA_CONNECTION_MODE = 1;
    436
    437    /**
    438     * 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,
    439     * requiring the client to connect to the server's data port to initiate a transfer.
    440     */
    441    public static final int PASSIVE_LOCAL_DATA_CONNECTION_MODE = 2;
    442
    443    /**
    444     * 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
    445     * passive mode, requiring the other server to connect to the first server's data port to initiate a data transfer.
    446     */
    447    public static final int PASSIVE_REMOTE_DATA_CONNECTION_MODE = 3;
    448
    449    /** Pattern for PASV mode responses. Groups: (n,n,n,n),(n),(n) */
    450    private static final Pattern PARMS_PAT = Pattern.compile("(\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3}),(\\d{1,3}),(\\d{1,3})");
    451
    452    private static Properties getOverrideProperties() {
    453        return PropertiesSingleton.PROPERTIES;
    454    }
    455
    456    static Properties loadResourceProperties(final String systemTypeProperties) {
    457        Properties properties = null;
    458        if (systemTypeProperties != null) {
    459            try (InputStream inputStream = FTPClient.class.getResourceAsStream(systemTypeProperties)) {
    460                if (inputStream != null) {
    461                    properties = new Properties();
    462                    properties.load(inputStream);
    463                }
    464            } catch (final IOException ignore) {
    465                // ignore
    466            }
    467        }
    468        return properties;
    469    }
    470
    471    /**
    472     * Parse the path from a CWD reply.
    473     * <p>
    474     * According to <a href="https://www.ietf.org/rfc/rfc959.txt">RFC959</a>, it should be the same as for MKD i.e.
    475     * {@code 257<space>"<directory-name>"[<space>commentary]} where any double-quotes in {@code <directory-name>} are doubled. Unlike MKD, the commentary is
    476     * optional.
    477     * </p>
    478     * <p>
    479     * However, see NET-442 for an exception.
    480     * </p>
    481     *
    482     * @param reply
    483     * @return the path, without enclosing quotes, or the full string after the reply code and space if the syntax is invalid (i.e. enclosing quotes are
    484     *         missing or embedded quotes are not doubled)
    485     */
    486    // package protected for access by test cases
    487    static String parsePathname(final String reply) {
    488        final String param = reply.substring(REPLY_CODE_LEN + 1);
    489        if (param.startsWith("\"")) {
    490            final StringBuilder sb = new StringBuilder(param.length());
    491            boolean quoteSeen = false;
    492            // start after initial quote
    493            for (int i = 1; i < param.length(); i++) {
    494                final char ch = param.charAt(i);
    495                if (ch == '"') {
    496                    if (quoteSeen) {
    497                        sb.append(ch);
    498                        quoteSeen = false;
    499                    } else {
    500                        // don't output yet, in case doubled
    501                        quoteSeen = true;
    502                    }
    503                } else {
    504                    if (quoteSeen) { // found lone trailing quote within string
    505                        return sb.toString();
    506                    }
    507                    sb.append(ch); // just another character
    508                }
    509            }
    510            if (quoteSeen) { // found lone trailing quote at end of string
    511                return sb.toString();
    512            }
    513        }
    514        // malformed reply, return all after reply code and space
    515        return param;
    516    }
    517
    518    private int dataConnectionMode;
    519    private Duration dataTimeout = Duration.ofMillis(-1);
    520
    521    private int passivePort;
    522    private String passiveHost;
    523    private final Random random = new Random();
    524    private int activeMinPort;
    525    private int activeMaxPort;
    526    private InetAddress activeExternalHost;
    527
    528    /** Overrides activeExternalHost in EPRT/PORT commands. */
    529    private InetAddress reportActiveExternalHost;
    530
    531    /** The address to bind to on passive connections, if necessary. */
    532    private InetAddress passiveLocalHost;
    533    private int fileType;
    534    @SuppressWarnings("unused") // fields are written, but currently not read
    535    private int formatOrByteSize;
    536    @SuppressWarnings("unused") // field is written, but currently not read
    537    private int fileStructure;
    538    private int fileTransferMode;
    539
    540    private boolean remoteVerificationEnabled = true;
    541
    542    private long restartOffset;
    543
    544    private FTPFileEntryParserFactory parserFactory = new DefaultFTPFileEntryParserFactory();
    545
    546    /**
    547     * For buffered data streams.
    548     */
    549    private int bufferSize;
    550
    551    private int sendDataSocketBufferSize;
    552
    553    private int receiveDataSocketBufferSize;
    554
    555    private boolean listHiddenFiles;
    556
    557    /**
    558     * Whether to attempt EPSV with an IPv4 connection.
    559     */
    560    private boolean useEPSVwithIPv4;
    561
    562    /**
    563     * A cached value that should not be referenced directly except when assigned in getSystemName and initDefaults.
    564     */
    565    private String systemName;
    566
    567    /**
    568     * A cached value that should not be referenced directly except when assigned in listFiles(String, String) and initDefaults.
    569     */
    570    private FTPFileEntryParser entryParser;
    571
    572    /**
    573     * Key used to create the parser; necessary to ensure that the parser type is not ignored.
    574     */
    575    private String entryParserKey;
    576
    577    private FTPClientConfig ftpClientConfig;
    578
    579    /**
    580     * Listener used by store/retrieve methods to handle keepalive.
    581     */
    582    private CopyStreamListener copyStreamListener;
    583
    584    /**
    585     * How long to wait before sending another control keep-alive message.
    586     */
    587    private Duration controlKeepAliveTimeout = Duration.ZERO;
    588
    589    /**
    590     * How long to wait for keepalive message replies before continuing. Most FTP servers don't seem to support concurrent control and data connection usage.
    591     */
    592    private Duration controlKeepAliveReplyTimeout = Duration.ofSeconds(1);
    593
    594    /**
    595     * Debug counts for NOOP acks.
    596     */
    597    private int[] cslDebug;
    598
    599    /**
    600     * Enable or disable replacement of internal IP in passive mode. Default enabled using {code NatServerResolverImpl}.
    601     */
    602    private HostnameResolver passiveNatWorkaroundStrategy = new NatServerResolverImpl(this);
    603
    604    /** Controls the automatic server encoding detection (only UTF-8 supported). */
    605    private boolean autoDetectEncoding;
    606
    607    /** Map of FEAT responses. If null, has not been initialized. */
    608    private HashMap<String, Set<String>> featuresMap;
    609
    610    private boolean ipAddressFromPasvResponse = Boolean.getBoolean(FTP_IP_ADDRESS_FROM_PASV_RESPONSE);
    611
    612    /**
    613     * Default FTPClient constructor. Creates a new FTPClient instance with the data connection mode set to {@link #ACTIVE_LOCAL_DATA_CONNECTION_MODE}, the file
    614     * type set to {@link FTP#ASCII_FILE_TYPE}, the file format set to {@link FTP#NON_PRINT_TEXT_FORMAT}, the file structure set to {@link FTP#FILE_STRUCTURE},
    615     * and the transfer mode set to {@link FTP#STREAM_TRANSFER_MODE}.
    616     * <p>
    617     * 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:
    618     * </p>
    619     *
    620     * <pre>
    621     * FTPClient ftp = new FTPClient();
    622     * FTPClientConfig config = new FTPClientConfig();
    623     * config.setLenientFutureDates(true);
    624     * ftp.configure(config);
    625     * </pre>
    626     */
    627    public FTPClient() {
    628        initDefaults();
    629    }
    630
    631    @Override
    632    protected void _connectAction_() throws IOException {
    633        _connectAction_(null);
    634    }
    635
    636    /**
    637     * @param socketIsReader the reader to reuse (if non-null)
    638     * @throws IOException on error
    639     * @since 3.4
    640     */
    641    @Override
    642    protected void _connectAction_(final Reader socketIsReader) throws IOException {
    643        super._connectAction_(socketIsReader); // sets up _input_ and _output_
    644        initDefaults();
    645        // must be after super._connectAction_(), because otherwise we get an
    646        // Exception claiming we're not connected
    647        if (autoDetectEncoding) {
    648            final ArrayList<String> oldReplyLines = new ArrayList<>(_replyLines);
    649            final int oldReplyCode = _replyCode;
    650            // UTF-8 appears to be the default
    651            final Charset utf8 = StandardCharsets.UTF_8;
    652            if (hasFeature("UTF8") || hasFeature(utf8.name())) {
    653                setControlEncoding(utf8);
    654                _controlInput_ = new CRLFLineReader(new InputStreamReader(_input_, getControlEncoding()));
    655                _controlOutput_ = new BufferedWriter(new OutputStreamWriter(_output_, getControlEncoding()));
    656            }
    657            // restore the original reply (server greeting)
    658            _replyLines.clear();
    659            _replyLines.addAll(oldReplyLines);
    660            _replyCode = oldReplyCode;
    661            _newReplyString = true;
    662        }
    663    }
    664
    665    /**
    666     * Establishes a data connection with the FTP server, returning a Socket for the connection if successful. If a restart offset has been set with
    667     * {@link #setRestartOffset(long)}, a REST command is issued to the server with the offset as an argument before establishing the data connection. Active
    668     * mode connections also cause a local PORT command to be issued.
    669     *
    670     * @param command The int representation of the FTP command to send.
    671     * @param arg     The arguments to the FTP command. If this parameter is set to null, then the command is sent with no argument.
    672     * @return A Socket corresponding to the established data connection. Null is returned if an FTP protocol error is reported at any point during the
    673     *         establishment and initialization of the connection.
    674     * @throws IOException If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    675     * @since 3.3
    676     */
    677    protected Socket _openDataConnection_(final FTPCmd command, final String arg) throws IOException {
    678        return _openDataConnection_(command.getCommand(), arg);
    679    }
    680
    681    /**
    682     * Establishes a data connection with the FTP server, returning a Socket for the connection if successful. If a restart offset has been set with
    683     * {@link #setRestartOffset(long)}, a REST command is issued to the server with the offset as an argument before establishing the data connection. Active
    684     * mode connections also cause a local PORT command to be issued.
    685     *
    686     * @param command The int representation of the FTP command to send.
    687     * @param arg     The arguments to the FTP command. If this parameter is set to null, then the command is sent with no argument.
    688     * @return A Socket corresponding to the established data connection. Null is returned if an FTP protocol error is reported at any point during the
    689     *         establishment and initialization of the connection.
    690     * @throws IOException If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    691     * @deprecated (3.3) Use {@link #_openDataConnection_(FTPCmd, String)} instead
    692     */
    693    @Deprecated
    694    protected Socket _openDataConnection_(final int command, final String arg) throws IOException {
    695        return _openDataConnection_(FTPCommand.getCommand(command), arg);
    696    }
    697
    698    /**
    699     * Establishes a data connection with the FTP server, returning a Socket for the connection if successful. If a restart offset has been set with
    700     * {@link #setRestartOffset(long)}, a REST command is issued to the server with the offset as an argument before establishing the data connection. Active
    701     * mode connections also cause a local PORT command to be issued.
    702     *
    703     * @param command The text representation of the FTP command to send.
    704     * @param arg     The arguments to the FTP command. If this parameter is set to null, then the command is sent with no argument.
    705     * @return A Socket corresponding to the established data connection. Null is returned if an FTP protocol error is reported at any point during the
    706     *         establishment and initialization of the connection.
    707     * @throws IOException If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    708     * @since 3.1
    709     */
    710    protected Socket _openDataConnection_(final String command, final String arg) throws IOException {
    711        if (dataConnectionMode != ACTIVE_LOCAL_DATA_CONNECTION_MODE && dataConnectionMode != PASSIVE_LOCAL_DATA_CONNECTION_MODE) {
    712            return null;
    713        }
    714        final boolean isInet6Address = getRemoteAddress() instanceof Inet6Address;
    715        final Socket socket;
    716        final int soTimeoutMillis = DurationUtils.toMillisInt(dataTimeout);
    717        if (dataConnectionMode == ACTIVE_LOCAL_DATA_CONNECTION_MODE) {
    718            // if no activePortRange was set (correctly) -> getActivePort() = 0
    719            // -> new ServerSocket(0) -> bind to any free local port
    720            try (ServerSocket server = _serverSocketFactory_.createServerSocket(getActivePort(), 1, getHostAddress())) {
    721                // Try EPRT only if remote server is over IPv6, if not use PORT,
    722                // because EPRT has no advantage over PORT on IPv4.
    723                // It could even have the disadvantage,
    724                // that EPRT will make the data connection fail, because
    725                // today's intelligent NAT Firewalls are able to
    726                // substitute IP addresses in the PORT command,
    727                // but might not be able to recognize the EPRT command.
    728                if (isInet6Address) {
    729                    if (!FTPReply.isPositiveCompletion(eprt(getReportHostAddress(), server.getLocalPort()))) {
    730                        return null;
    731                    }
    732                } else if (!FTPReply.isPositiveCompletion(port(getReportHostAddress(), server.getLocalPort()))) {
    733                    return null;
    734                }
    735                if (restartOffset > 0 && !restart(restartOffset) || !FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
    736                    return null;
    737                }
    738                // For now, let's just use the data timeout value for waiting for
    739                // the data connection. It may be desirable to let this be a
    740                // separately configurable value. In any case, we really want
    741                // to allow preventing the accept from blocking indefinitely.
    742                if (soTimeoutMillis >= 0) {
    743                    server.setSoTimeout(soTimeoutMillis);
    744                }
    745                socket = wrapOnDeflate(server.accept());
    746                // Ensure the timeout is set before any commands are issued on the new socket
    747                if (soTimeoutMillis >= 0) {
    748                    socket.setSoTimeout(soTimeoutMillis);
    749                }
    750                if (receiveDataSocketBufferSize > 0) {
    751                    socket.setReceiveBufferSize(receiveDataSocketBufferSize);
    752                }
    753                if (sendDataSocketBufferSize > 0) {
    754                    socket.setSendBufferSize(sendDataSocketBufferSize);
    755                }
    756            }
    757        } else {
    758            // We must be in PASSIVE_LOCAL_DATA_CONNECTION_MODE
    759            // Try EPSV command first on IPv6 - and IPv4 if enabled.
    760            // When using IPv4 with NAT it has the advantage
    761            // to work with more rare configurations.
    762            // E.g. if FTP server has a static PASV address (external network)
    763            // and the client is coming from another internal network.
    764            // In that case the data connection after PASV command would fail,
    765            // while EPSV would make the client succeed by taking just the port.
    766            final boolean attemptEPSV = isUseEPSVwithIPv4() || isInet6Address;
    767            if (attemptEPSV && epsv() == FTPReply.ENTERING_EPSV_MODE) {
    768                _parseExtendedPassiveModeReply(_replyLines.get(0));
    769            } else {
    770                // If EPSV failed on IPV4, revert to PASV
    771                if (isInet6Address || pasv() != FTPReply.ENTERING_PASSIVE_MODE) {
    772                    return null;
    773                }
    774                _parsePassiveModeReply(_replyLines.get(0));
    775            }
    776            socket = wrapOnDeflate(_socketFactory_.createSocket());
    777            if (receiveDataSocketBufferSize > 0) {
    778                socket.setReceiveBufferSize(receiveDataSocketBufferSize);
    779            }
    780            if (sendDataSocketBufferSize > 0) {
    781                socket.setSendBufferSize(sendDataSocketBufferSize);
    782            }
    783            if (passiveLocalHost != null) {
    784                socket.bind(new InetSocketAddress(passiveLocalHost, 0));
    785            }
    786            // For now, let's just use the data timeout value for waiting for
    787            // the data connection. It may be desirable to let this be a
    788            // separately configurable value. In any case, we really want
    789            // to allow preventing the accept from blocking indefinitely.
    790            if (soTimeoutMillis >= 0) {
    791                socket.setSoTimeout(soTimeoutMillis);
    792            }
    793            socket.connect(new InetSocketAddress(passiveHost, passivePort), connectTimeout);
    794            if (restartOffset > 0 && !restart(restartOffset) || !FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
    795                socket.close();
    796                return null;
    797            }
    798        }
    799        if (remoteVerificationEnabled && !verifyRemote(socket)) {
    800            // Grab the host before we close the socket to avoid NET-663
    801            final String socketHostAddress = getHostAddress(socket);
    802            final String remoteHostAddress = getHostAddress(_socket_);
    803            IOUtils.closeQuietly(socket);
    804            throw new IOException("Host attempting data connection " + socketHostAddress + " is not same as server " + remoteHostAddress);
    805        }
    806        return socket;
    807    }
    808
    809    /**
    810     * Parses a reply.
    811     *
    812     * @param reply the reply to parse.
    813     * @throws MalformedServerReplyException if the reply is malformed.
    814     */
    815    protected void _parseExtendedPassiveModeReply(String reply) throws MalformedServerReplyException {
    816        reply = reply.substring(reply.indexOf('(') + 1, reply.indexOf(')')).trim();
    817        final char delim1 = reply.charAt(0);
    818        final char delim2 = reply.charAt(1);
    819        final char delim3 = reply.charAt(2);
    820        final char delim4 = reply.charAt(reply.length() - 1);
    821        if (delim1 != delim2 || delim2 != delim3 || delim3 != delim4) {
    822            throw new MalformedServerReplyException("Could not parse extended passive host information.\nServer Reply: " + reply);
    823        }
    824        final int port;
    825        try {
    826            port = Integer.parseInt(reply.substring(3, reply.length() - 1));
    827        } catch (final NumberFormatException e) {
    828            throw new MalformedServerReplyException("Could not parse extended passive host information.\nServer Reply: " + reply);
    829        }
    830        // in EPSV mode, the passive host address is implicit
    831        passiveHost = getRemoteAddress().getHostAddress();
    832        passivePort = port;
    833    }
    834
    835    /**
    836     * Parses a reply.
    837     *
    838     * @param reply the reply to parse
    839     * @throws MalformedServerReplyException if the server reply does not match (n,n,n,n),(n),(n)
    840     * @since 3.1
    841     */
    842    protected void _parsePassiveModeReply(final String reply) throws MalformedServerReplyException {
    843        final Matcher m = PARMS_PAT.matcher(reply);
    844        if (!m.find()) {
    845            throw new MalformedServerReplyException("Could not parse passive host information.\nServer Reply: " + reply);
    846        }
    847        final int pasvPort;
    848        // Fix up to look like IP address
    849        String pasvHost = "0,0,0,0".equals(m.group(1)) ? _socket_.getInetAddress().getHostAddress() : m.group(1).replace(',', '.');
    850        try {
    851            final int oct1 = Integer.parseInt(m.group(2));
    852            final int oct2 = Integer.parseInt(m.group(3));
    853            pasvPort = oct1 << 8 | oct2;
    854        } catch (final NumberFormatException e) {
    855            throw new MalformedServerReplyException("Could not parse passive port information.\nServer Reply: " + reply);
    856        }
    857        if (isIpAddressFromPasvResponse()) {
    858            // Pre-3.9.0 behavior
    859            if (passiveNatWorkaroundStrategy != null) {
    860                try {
    861                    final String newPassiveHost = passiveNatWorkaroundStrategy.resolve(pasvHost);
    862                    if (!pasvHost.equals(newPassiveHost)) {
    863                        fireReplyReceived(0, "[Replacing PASV mode reply address " + passiveHost + " with " + newPassiveHost + "]\n");
    864                        pasvHost = newPassiveHost;
    865                    }
    866                } catch (final UnknownHostException e) { // Should not happen as we are passing in an IP address
    867                    throw new MalformedServerReplyException("Could not parse passive host information.\nServer Reply: " + reply);
    868                }
    869            }
    870        } else if (_socket_ == null) {
    871            pasvHost = null; // For unit testing.
    872        } else {
    873            pasvHost = _socket_.getInetAddress().getHostAddress();
    874        }
    875        passiveHost = pasvHost;
    876        passivePort = pasvPort;
    877    }
    878
    879    /**
    880     * Retrieves data to an output stream for the given command.
    881     *
    882     * @param command the command to get
    883     * @param remote  the remote file name
    884     * @param local   The local OutputStream to which to write the file.
    885     * @return true if successful
    886     * @throws IOException on error
    887     * @since 3.1
    888     */
    889    protected boolean _retrieveFile(final String command, final String remote, final OutputStream local) throws IOException {
    890        final Socket socket = _openDataConnection_(command, remote);
    891        if (socket == null) {
    892            return false;
    893        }
    894        InputStream input = null;
    895        CSL csl = null;
    896        try {
    897            try {
    898                if (fileType == ASCII_FILE_TYPE) {
    899                    input = new FromNetASCIIInputStream(getBufferedInputStream(socket.getInputStream()));
    900                } else {
    901                    input = getBufferedInputStream(socket.getInputStream());
    902                }
    903                if (DurationUtils.isPositive(controlKeepAliveTimeout)) {
    904                    csl = new CSL(this, controlKeepAliveTimeout, controlKeepAliveReplyTimeout);
    905                }
    906                // Treat everything else as binary for now
    907                Util.copyStream(input, local, getBufferSize(), CopyStreamEvent.UNKNOWN_STREAM_SIZE, mergeListeners(csl), false);
    908            } finally {
    909                IOUtils.closeQuietly(input);
    910            }
    911            // Get the transfer response
    912            return completePendingCommand();
    913        } finally {
    914            IOUtils.closeQuietly(socket);
    915            if (csl != null) {
    916                cslDebug = csl.cleanUp(); // fetch any outstanding keepalive replies
    917            }
    918        }
    919    }
    920
    921    /**
    922     * Retrieves data in an input stream for the given command.
    923     *
    924     * @param command the command to send
    925     * @param remote  the remote file name
    926     * @return the stream from which to read the file
    927     * @throws IOException on error
    928     * @since 3.1
    929     */
    930    protected InputStream _retrieveFileStream(final String command, final String remote) throws IOException {
    931        final Socket socket = _openDataConnection_(command, remote);
    932        if (socket == null) {
    933            return null;
    934        }
    935        final InputStream input;
    936        if (fileType == ASCII_FILE_TYPE) {
    937            // We buffer ASCII transfers because the buffering has to
    938            // be interposed between FromNetASCIIOutputSream and the underlying
    939            // socket input stream. We don't buffer binary transfers
    940            // because we don't want to impose a buffering policy on the
    941            // programmer if possible. Programmers can decide on their
    942            // own if they want to wrap the SocketInputStream we return
    943            // for file types other than ASCII.
    944            input = new FromNetASCIIInputStream(getBufferedInputStream(socket.getInputStream()));
    945        } else {
    946            input = socket.getInputStream();
    947        }
    948        return new org.apache.commons.net.io.SocketInputStream(socket, input);
    949    }
    950
    951    /**
    952     * Stores the given stream.
    953     *
    954     * @param command the command to send
    955     * @param remote  the remote file name
    956     * @param local   The local InputStream from which to read the data to be written/appended to the remote file.
    957     * @return true if successful
    958     * @throws IOException on error
    959     * @since 3.1
    960     */
    961    protected boolean _storeFile(final String command, final String remote, final InputStream local) throws IOException {
    962        final Socket socket = _openDataConnection_(command, remote);
    963        if (socket == null) {
    964            return false;
    965        }
    966        OutputStream output = null;
    967        CSL csl = null;
    968        try {
    969            if (fileType == ASCII_FILE_TYPE) {
    970                output = new ToNetASCIIOutputStream(getBufferedOutputStream(socket.getOutputStream()));
    971            } else {
    972                output = getBufferedOutputStream(socket.getOutputStream());
    973            }
    974            if (DurationUtils.isPositive(controlKeepAliveTimeout)) {
    975                csl = new CSL(this, controlKeepAliveTimeout, controlKeepAliveReplyTimeout);
    976            }
    977            // Treat everything else as binary for now
    978            Util.copyStream(local, output, getBufferSize(), CopyStreamEvent.UNKNOWN_STREAM_SIZE, mergeListeners(csl), false);
    979            output.close(); // ensure the file is fully written
    980            socket.close(); // done writing the file
    981            // Get the transfer response
    982            return completePendingCommand();
    983        } catch (final IOException e) {
    984            IOUtils.closeQuietly(output); // ignore close errors here
    985            IOUtils.closeQuietly(socket); // ignore close errors here
    986            throw e;
    987        } finally {
    988            if (csl != null) {
    989                cslDebug = csl.cleanUp(); // fetch any outstanding keepalive replies
    990            }
    991        }
    992    }
    993
    994    /**
    995     * Gets the output stream.
    996     *
    997     * @param command the command to send
    998     * @param remote  the remote file name
    999     * @return the output stream.
    1000     * @throws IOException on error
    1001     * @since 3.1
    1002     */
    1003    protected OutputStream _storeFileStream(final String command, final String remote) throws IOException {
    1004        final Socket socket = _openDataConnection_(command, remote);
    1005        if (socket == null) {
    1006            return null;
    1007        }
    1008        final OutputStream output;
    1009        if (fileType == ASCII_FILE_TYPE) {
    1010            // We buffer ASCII transfers because the buffering has to
    1011            // be interposed between ToNetASCIIOutputSream and the underlying
    1012            // socket output stream. We don't buffer binary transfers
    1013            // because we don't want to impose a buffering policy on the
    1014            // programmer if possible. Programmers can decide on their
    1015            // own if they want to wrap the SocketOutputStream we return
    1016            // for file types other than ASCII.
    1017            output = new ToNetASCIIOutputStream(getBufferedOutputStream(socket.getOutputStream()));
    1018        } else {
    1019            output = socket.getOutputStream();
    1020        }
    1021        return new SocketOutputStream(socket, output);
    1022    }
    1023
    1024    /**
    1025     * Aborts a transfer in progress.
    1026     *
    1027     * @return True if successfully completed, false if not.
    1028     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1029     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1030     *                                      independently as itself.
    1031     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1032     */
    1033    public boolean abort() throws IOException {
    1034        return FTPReply.isPositiveCompletion(abor());
    1035    }
    1036
    1037    /**
    1038     * Allocates a number of bytes on the server for the next file transfer.
    1039     *
    1040     * @param bytes The number of bytes which the server should allocate.
    1041     * @return True if successfully completed, false if not.
    1042     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1043     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1044     *                                      independently as itself.
    1045     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1046     */
    1047    public boolean allocate(final int bytes) throws IOException {
    1048        return FTPReply.isPositiveCompletion(allo(bytes));
    1049    }
    1050
    1051    /**
    1052     * Allocates space on the server for the next file transfer.
    1053     *
    1054     * @param bytes      The number of bytes which the server should allocate.
    1055     * @param recordSize The size of a file record.
    1056     * @return True if successfully completed, false if not.
    1057     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1058     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1059     *                                      independently as itself.
    1060     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1061     */
    1062    public boolean allocate(final int bytes, final int recordSize) throws IOException {
    1063        return FTPReply.isPositiveCompletion(allo(bytes, recordSize));
    1064    }
    1065
    1066    /**
    1067     * Allocates a number of bytes on the server for the next file transfer.
    1068     *
    1069     * @param bytes The number of bytes which the server should allocate.
    1070     * @return True if successfully completed, false if not.
    1071     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1072     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1073     *                                      independently as itself.
    1074     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1075     */
    1076    public boolean allocate(final long bytes) throws IOException {
    1077        return FTPReply.isPositiveCompletion(allo(bytes));
    1078    }
    1079
    1080    /**
    1081     * Allocates space on the server for the next file transfer.
    1082     *
    1083     * @param bytes      The number of bytes which the server should allocate.
    1084     * @param recordSize The size of a file record.
    1085     * @return True if successfully completed, false if not.
    1086     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1087     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1088     *                                      independently as itself.
    1089     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1090     */
    1091    public boolean allocate(final long bytes, final int recordSize) throws IOException {
    1092        return FTPReply.isPositiveCompletion(allo(bytes, recordSize));
    1093    }
    1094
    1095    /**
    1096     * 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
    1097     * 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
    1098     * special InputStream to do this).
    1099     *
    1100     * @param remote The name of the remote file.
    1101     * @param local  The local InputStream from which to read the data to be appended to the remote file.
    1102     * @return True if successfully completed, false if not.
    1103     * @throws FTPConnectionClosedException                  If the FTP server prematurely closes the connection as a result of the client being idle or some
    1104     *                                                       other reason causing the server to send FTP reply code 421. This exception may be caught either as
    1105     *                                                       an IOException or independently as itself.
    1106     * @throws org.apache.commons.net.io.CopyStreamException If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to
    1107     *                                                       determine the number of bytes transferred and the IOException causing the error. This exception may
    1108     *                                                       be caught either as an IOException or independently as itself.
    1109     * @throws IOException                                   If an I/O error occurs while either sending a command to the server or receiving a reply from the
    1110     *                                                       server.
    1111     */
    1112    public boolean appendFile(final String remote, final InputStream local) throws IOException {
    1113        return storeFile(FTPCmd.APPE, remote, local);
    1114    }
    1115
    1116    /**
    1117     * 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
    1118     * 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
    1119     * 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
    1120     * socket upon being closed.
    1121     * <p>
    1122     * <strong>To finalize the file transfer you must call {@link #completePendingCommand completePendingCommand} and check its return value to verify
    1123     * success.</strong> If this is not done, subsequent commands may behave unexpectedly.
    1124     * </p>
    1125     *
    1126     * @param remote The name of the remote file.
    1127     * @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
    1128     *         returned (in which case you may check the reply code to determine the exact reason for failure).
    1129     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1130     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1131     *                                      independently as itself.
    1132     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1133     */
    1134    public OutputStream appendFileStream(final String remote) throws IOException {
    1135        return storeFileStream(FTPCmd.APPE, remote);
    1136    }
    1137
    1138    /**
    1139     * Change to the parent directory of the current working directory.
    1140     *
    1141     * @return True if successfully completed, false if not.
    1142     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1143     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1144     *                                      independently as itself.
    1145     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1146     */
    1147    public boolean changeToParentDirectory() throws IOException {
    1148        return FTPReply.isPositiveCompletion(cdup());
    1149    }
    1150
    1151    /**
    1152     * Changes the current working directory of the FTP session.
    1153     *
    1154     * @param path The new current working directory.
    1155     * @return True if successfully completed, false if not.
    1156     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1157     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1158     *                                      independently as itself.
    1159     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1160     */
    1161    public boolean changeWorkingDirectory(final String path) throws IOException {
    1162        return FTPReply.isPositiveCompletion(cwd(path));
    1163    }
    1164
    1165    /**
    1166     * 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
    1167     * 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
    1168     * receive the completion reply from the server and verify the success of the entire transaction.
    1169     * <p>
    1170     * For example,
    1171     * </p>
    1172     *
    1173     * <pre>
    1174     * InputStream input;
    1175     * OutputStream output;
    1176     * input  = new FileInputStream("foobaz.txt");
    1177     * output = ftp.storeFileStream("foobar.txt")
    1178     * if (!FTPReply.isPositiveIntermediate(ftp.getReplyCode())) {
    1179     *     input.close();
    1180     *     output.close();
    1181     *     ftp.logout();
    1182     *     ftp.disconnect();
    1183     *     System.err.println("File transfer failed.");
    1184     *     System.exit(1);
    1185     * }
    1186     * Util.copyStream(input, output);
    1187     * input.close();
    1188     * output.close();
    1189     * // Must call completePendingCommand() to finish command.
    1190     * if (!ftp.completePendingCommand()) {
    1191     *     ftp.logout();
    1192     *     ftp.disconnect();
    1193     *     System.err.println("File transfer failed.");
    1194     *     System.exit(1);
    1195     * }
    1196     * </pre>
    1197     *
    1198     * @return True if successfully completed, false if not.
    1199     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1200     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1201     *                                      independently as itself.
    1202     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1203     */
    1204    public boolean completePendingCommand() throws IOException {
    1205        return FTPReply.isPositiveCompletion(getReply());
    1206    }
    1207
    1208    /**
    1209     * Implements the {@link Configurable} interface. In the case of this class, configuring merely makes the config object available for the factory methods
    1210     * that construct parsers.
    1211     *
    1212     * @param ftpClientConfig {@link FTPClientConfig} object used to provide non-standard configurations to the parser.
    1213     * @since 1.4
    1214     */
    1215    @Override
    1216    public void configure(final FTPClientConfig ftpClientConfig) {
    1217        this.ftpClientConfig = ftpClientConfig;
    1218    }
    1219
    1220    // package access for test purposes
    1221    void createParser(final String parserKey) throws IOException {
    1222        // We cache the value to avoid creation of a new object every
    1223        // time a file listing is generated.
    1224        // Note: we don't check against a null parserKey (NET-544)
    1225        if (entryParser == null || parserKey != null && !entryParserKey.equals(parserKey)) {
    1226            if (null != parserKey) {
    1227                // if a parser key was supplied in the parameters,
    1228                // use that to create the parser
    1229                entryParser = parserFactory.createFileEntryParser(parserKey);
    1230                entryParserKey = parserKey;
    1231            } else if (ftpClientConfig != null && !ftpClientConfig.getServerSystemKey().isEmpty()) {
    1232                // if no parserKey was supplied, check for a configuration
    1233                // in the params, and if it has a non-empty system type, use that.
    1234                entryParser = parserFactory.createFileEntryParser(ftpClientConfig);
    1235                entryParserKey = ftpClientConfig.getServerSystemKey();
    1236            } else {
    1237                // if a parserKey hasn't been supplied, and a configuration
    1238                // hasn't been supplied, and the override property is not set
    1239                // then autodetect by calling
    1240                // the SYST command and use that to choose the parser.
    1241                final String systemType = getSystemTypeOverride();
    1242                if (ftpClientConfig != null) { // system type must have been empty above
    1243                    entryParser = parserFactory.createFileEntryParser(new FTPClientConfig(systemType, ftpClientConfig));
    1244                } else {
    1245                    entryParser = parserFactory.createFileEntryParser(systemType);
    1246                }
    1247                entryParserKey = systemType;
    1248            }
    1249        }
    1250    }
    1251
    1252    /**
    1253     * Deletes a file on the FTP server.
    1254     *
    1255     * @param path The path of the file to be deleted.
    1256     * @return True if successfully completed, false if not.
    1257     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1258     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1259     *                                      independently as itself.
    1260     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1261     */
    1262    public boolean deleteFile(final String path) throws IOException {
    1263        return FTPReply.isPositiveCompletion(dele(path));
    1264    }
    1265
    1266    /**
    1267     * Closes the connection to the FTP server and restores connection parameters to the default values.
    1268     *
    1269     * @throws IOException If an error occurs while disconnecting.
    1270     */
    1271    @Override
    1272    public void disconnect() throws IOException {
    1273        super.disconnect();
    1274        initDefaults();
    1275    }
    1276
    1277    /**
    1278     * Issue a command and wait for the reply.
    1279     * <p>
    1280     * Should only be used with commands that return replies on the command channel - do not use for LIST, NLST, MLSD etc.
    1281     * </p>
    1282     *
    1283     * @param command The command to invoke
    1284     * @param params  The parameters string, may be {@code null}
    1285     * @return True if successfully completed, false if not, in which case call {@link #getReplyCode()} or {@link #getReplyString()} to get the reason.
    1286     * @throws IOException If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1287     * @since 3.0
    1288     */
    1289    public boolean doCommand(final String command, final String params) throws IOException {
    1290        return FTPReply.isPositiveCompletion(sendCommand(command, params));
    1291    }
    1292
    1293    /**
    1294     * Issue a command and wait for the reply, returning it as an array of strings.
    1295     * <p>
    1296     * Should only be used with commands that return replies on the command channel - do not use for LIST, NLST, MLSD etc.
    1297     * </p>
    1298     *
    1299     * @param command The command to invoke
    1300     * @param params  The parameters string, may be {@code null}
    1301     * @return The array of replies, or {@code null} if the command failed, in which case call {@link #getReplyCode()} or {@link #getReplyString()} to get the
    1302     *         reason.
    1303     *
    1304     * @throws IOException If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1305     * @since 3.0
    1306     */
    1307    public String[] doCommandAsStrings(final String command, final String params) throws IOException {
    1308        final boolean success = FTPReply.isPositiveCompletion(sendCommand(command, params));
    1309        if (success) {
    1310            return getReplyStrings();
    1311        }
    1312        return null;
    1313    }
    1314
    1315    /**
    1316     * Sets the current data connection mode to {@code ACTIVE_LOCAL_DATA_CONNECTION_MODE}. No communication with the FTP server is conducted, but this causes
    1317     * all future data transfers to require the FTP server to connect to the client's data port. Additionally, to accommodate differences between socket
    1318     * implementations on different platforms, this method causes the client to issue a PORT command before every data transfer.
    1319     */
    1320    public void enterLocalActiveMode() {
    1321        dataConnectionMode = ACTIVE_LOCAL_DATA_CONNECTION_MODE;
    1322        passiveHost = null;
    1323        passivePort = -1;
    1324    }
    1325
    1326    /**
    1327     * Sets the current data connection mode to {@code PASSIVE_LOCAL_DATA_CONNECTION_MODE}. Use this method only for data transfers between the client and
    1328     * 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 open a
    1329     * data port to which the client will connect to conduct data transfers. The FTPClient will stay in {@link #PASSIVE_LOCAL_DATA_CONNECTION_MODE} until the
    1330     * mode is changed by calling some other method such as {@link #enterLocalActiveMode enterLocalActiveMode()}
    1331     * <p>
    1332     * <strong>N.B.</strong> currently calling any connect method will reset the mode to ACTIVE_LOCAL_DATA_CONNECTION_MODE.
    1333     * </p>
    1334     */
    1335    public void enterLocalPassiveMode() {
    1336        dataConnectionMode = PASSIVE_LOCAL_DATA_CONNECTION_MODE;
    1337        // These will be set when just before a data connection is opened
    1338        // in _openDataConnection_()
    1339        passiveHost = null;
    1340        passivePort = -1;
    1341    }
    1342
    1343    /**
    1344     * Sets the current data connection mode to {@code ACTIVE_REMOTE_DATA_CONNECTION}. Use this method only for server to server data transfers. This method
    1345     * 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 before
    1346     * EVERY server to server transfer attempt. The FTPClient will NOT automatically continue to issue PORT commands. You also must remember to call
    1347     * {@link #enterLocalActiveMode enterLocalActiveMode()} if you wish to return to the normal data connection mode.
    1348     *
    1349     * @param host The passive mode server accepting connections for data transfers.
    1350     * @param port The passive mode server's data port.
    1351     * @return True if successfully completed, false if not.
    1352     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1353     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1354     *                                      independently as itself.
    1355     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1356     */
    1357    public boolean enterRemoteActiveMode(final InetAddress host, final int port) throws IOException {
    1358        if (FTPReply.isPositiveCompletion(port(host, port))) {
    1359            dataConnectionMode = ACTIVE_REMOTE_DATA_CONNECTION_MODE;
    1360            passiveHost = null;
    1361            passivePort = -1;
    1362            return true;
    1363        }
    1364        return false;
    1365    }
    1366
    1367    /**
    1368     * Sets the current data connection mode to {@code PASSIVE_REMOTE_DATA_CONNECTION_MODE}. Use this method only for server to server data transfers. This
    1369     * 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 must
    1370     * call this method before EVERY server to server transfer attempt. The FTPClient will NOT automatically continue to issue PASV commands. You also must
    1371     * remember to call {@link #enterLocalActiveMode enterLocalActiveMode()} if you wish to return to the normal data connection mode.
    1372     *
    1373     * @return True if successfully completed, false if not.
    1374     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1375     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1376     *                                      independently as itself.
    1377     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1378     */
    1379    public boolean enterRemotePassiveMode() throws IOException {
    1380        if (pasv() != FTPReply.ENTERING_PASSIVE_MODE) {
    1381            return false;
    1382        }
    1383        dataConnectionMode = PASSIVE_REMOTE_DATA_CONNECTION_MODE;
    1384        _parsePassiveModeReply(_replyLines.get(0));
    1385        return true;
    1386    }
    1387
    1388    /**
    1389     * Queries the server for supported features. The server may reply with a list of server-supported extensions. For example, a typical client-server
    1390     * interaction might be (from RFC 2389):
    1391     *
    1392     * <pre>
    1393        C&gt; feat
    1394        S&gt; 211-Extensions supported:
    1395        S&gt;  MLST size*;create;modify*;perm;media-type
    1396        S&gt;  SIZE
    1397        S&gt;  COMPRESSION
    1398        S&gt;  MDTM
    1399        S&gt; 211 END
    1400     * </pre>
    1401     *
    1402     * @see <a href="https://datatracker.ietf.org/doc/html/rfc2389">RFC 2389</a>
    1403     * @return True if successfully completed, false if not.
    1404     * @throws IOException on error
    1405     * @since 2.2
    1406     */
    1407    public boolean features() throws IOException {
    1408        return FTPReply.isPositiveCompletion(feat());
    1409    }
    1410
    1411    /**
    1412     * Queries the server for a supported feature, and returns its value (if any). Caches the parsed response to avoid resending the command repeatedly.
    1413     *
    1414     * @param feature the feature to check
    1415     * @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
    1416     *         feature is not found or the command failed. Check {@link #getReplyCode()} or {@link #getReplyString()} if so.
    1417     * @throws IOException on error
    1418     * @since 3.0
    1419     */
    1420    public String featureValue(final String feature) throws IOException {
    1421        final String[] values = featureValues(feature);
    1422        if (values != null) {
    1423            return values[0];
    1424        }
    1425        return null;
    1426    }
    1427
    1428    /**
    1429     * Queries the server for a supported feature, and returns its values (if any). Caches the parsed response to avoid resending the command repeatedly.
    1430     *
    1431     * @param feature the feature to check
    1432     * @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
    1433     *         failed. Check {@link #getReplyCode()} or {@link #getReplyString()} if so.
    1434     * @throws IOException on error
    1435     * @since 3.0
    1436     */
    1437    public String[] featureValues(final String feature) throws IOException {
    1438        if (!initFeatureMap()) {
    1439            return null;
    1440        }
    1441        final Set<String> entries = featuresMap.get(feature.toUpperCase(Locale.ENGLISH));
    1442        if (entries != null) {
    1443            return entries.toArray(NetConstants.EMPTY_STRING_ARRAY);
    1444        }
    1445        return null;
    1446    }
    1447
    1448    /**
    1449     * Gets the client port for active mode.
    1450     *
    1451     * @return The client port for active mode.
    1452     */
    1453    int getActivePort() {
    1454        if (activeMinPort > 0 && activeMaxPort >= activeMinPort) {
    1455            if (activeMaxPort == activeMinPort) {
    1456                return activeMaxPort;
    1457            }
    1458            // Get a random port between the min and max port range
    1459            return random.nextInt(activeMaxPort - activeMinPort + 1) + activeMinPort;
    1460        }
    1461        // default port
    1462        return 0;
    1463    }
    1464
    1465    /**
    1466     * Gets whether automatic server encoding detection is enabled.
    1467     *
    1468     * @return true, if automatic server encoding detection is enabled.
    1469     */
    1470    public boolean getAutodetectUTF8() {
    1471        return autoDetectEncoding;
    1472    }
    1473
    1474    private InputStream getBufferedInputStream(final InputStream inputStream) {
    1475        if (bufferSize > 0) {
    1476            return new BufferedInputStream(inputStream, bufferSize);
    1477        }
    1478        return new BufferedInputStream(inputStream);
    1479    }
    1480
    1481    private OutputStream getBufferedOutputStream(final OutputStream outputStream) {
    1482        if (bufferSize > 0) {
    1483            return new BufferedOutputStream(outputStream, bufferSize);
    1484        }
    1485        return new BufferedOutputStream(outputStream);
    1486    }
    1487
    1488    /**
    1489     * Gets the current internal buffer size for buffered data streams.
    1490     *
    1491     * @return The current buffer size.
    1492     */
    1493    public int getBufferSize() {
    1494        return bufferSize;
    1495    }
    1496
    1497    /**
    1498     * Gets how long to wait for control keep-alive message replies.
    1499     *
    1500     * @return wait time in milliseconds.
    1501     * @since 3.0
    1502     * @deprecated Use {@link #getControlKeepAliveReplyTimeoutDuration()}.
    1503     */
    1504    @Deprecated
    1505    public int getControlKeepAliveReplyTimeout() {
    1506        return DurationUtils.toMillisInt(controlKeepAliveReplyTimeout);
    1507    }
    1508
    1509    /**
    1510     * Gets how long to wait for control keep-alive message replies.
    1511     *
    1512     * @return wait time.
    1513     * @since 3.9.0
    1514     */
    1515    public Duration getControlKeepAliveReplyTimeoutDuration() {
    1516        return controlKeepAliveReplyTimeout;
    1517    }
    1518
    1519    /**
    1520     * Gets the time to wait between sending control connection keepalive messages when processing file upload or download.
    1521     * <p>
    1522     * See the class Javadoc section "Control channel keep-alive feature"
    1523     * </p>
    1524     *
    1525     * @return the number of seconds between keepalive messages.
    1526     * @since 3.0
    1527     * @deprecated Use {@link #getControlKeepAliveTimeoutDuration()}.
    1528     */
    1529    @Deprecated
    1530    public long getControlKeepAliveTimeout() {
    1531        return controlKeepAliveTimeout.getSeconds();
    1532    }
    1533
    1534    /**
    1535     * Gets the time to wait between sending control connection keepalive messages when processing file upload or download.
    1536     * <p>
    1537     * See the class Javadoc section "Control channel keep-alive feature"
    1538     * </p>
    1539     *
    1540     * @return the duration between keepalive messages.
    1541     * @since 3.9.0
    1542     */
    1543    public Duration getControlKeepAliveTimeoutDuration() {
    1544        return controlKeepAliveTimeout;
    1545    }
    1546
    1547    /**
    1548     * Gets the currently active listener.
    1549     *
    1550     * @return the listener, may be {@code null}
    1551     * @since 3.0
    1552     */
    1553    public CopyStreamListener getCopyStreamListener() {
    1554        return copyStreamListener;
    1555    }
    1556
    1557    /**
    1558     * Gets the CSL debug array.
    1559     * <p>
    1560     * <strong>For debug use only</strong>
    1561     * </p>
    1562     * <p>
    1563     * Currently, it contains:
    1564     * </p>
    1565     * <ul>
    1566     * <li>successfully acked NOOPs at end of transfer</li>
    1567     * <li>unanswered NOOPs at end of transfer</li>
    1568     * <li>unanswered NOOPs after fetching additional replies</li>
    1569     * <li>Number of IOErrors ignored</li>
    1570     * </ul>
    1571     *
    1572     * @return the debug array
    1573     * @deprecated 3.7 For testing only; may be dropped or changed at any time
    1574     */
    1575    @Deprecated // only for use in testing
    1576    public int[] getCslDebug() {
    1577        return cslDebug;
    1578    }
    1579
    1580    /**
    1581     * Gets the current data connection mode (one of the {@code _DATA_CONNECTION_MODE} constants).
    1582     *
    1583     * @return The current data connection mode (one of the {@code _DATA_CONNECTION_MODE} constants).
    1584     */
    1585    public int getDataConnectionMode() {
    1586        return dataConnectionMode;
    1587    }
    1588
    1589    /**
    1590     * 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
    1591     * value is &ge; 0.
    1592     * <p>
    1593     * <strong>Note:</strong> the timeout will also be applied when calling accept() whilst establishing an active local data connection.
    1594     * </p>
    1595     *
    1596     * @return The default timeout used when opening a data connection socket. The value 0 means an infinite timeout.
    1597     * @since 3.9.0
    1598     */
    1599    public Duration getDataTimeout() {
    1600        return dataTimeout;
    1601    }
    1602
    1603    // Method for use by unit test code only
    1604    FTPFileEntryParser getEntryParser() {
    1605        return entryParser;
    1606    }
    1607
    1608    /**
    1609     * Gets the host address for active mode; allows the local address to be overridden.
    1610     *
    1611     * @return activeExternalHost if non-null, else getLocalAddress()
    1612     * @see #setActiveExternalIPAddress(String)
    1613     */
    1614    InetAddress getHostAddress() {
    1615        if (activeExternalHost != null) {
    1616            return activeExternalHost;
    1617        }
    1618        // default local address
    1619        return getLocalAddress();
    1620    }
    1621
    1622    /**
    1623     * Gets the adjusted string with "-a" added if necessary.
    1624     *
    1625     * @param pathName the initial path
    1626     * @return the adjusted string with "-a" added if necessary.
    1627     * @since 2.0
    1628     */
    1629    protected String getListArguments(final String pathName) {
    1630        if (getListHiddenFiles()) {
    1631            if (pathName != null) {
    1632                final StringBuilder sb = new StringBuilder(pathName.length() + 3);
    1633                sb.append("-a ");
    1634                sb.append(pathName);
    1635                return sb.toString();
    1636            }
    1637            return "-a";
    1638        }
    1639        return pathName;
    1640    }
    1641
    1642    /**
    1643     * Gets whether to list hidden files.
    1644     *
    1645     * @see #setListHiddenFiles(boolean)
    1646     * @return whether to list hidden files.
    1647     * @since 2.0
    1648     */
    1649    public boolean getListHiddenFiles() {
    1650        return listHiddenFiles;
    1651    }
    1652
    1653    /**
    1654     * Gets a file modification time.
    1655     * <p>
    1656     * 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
    1657     * 3077 form "yyyyMMDDhhmmss(.xxx)?". The timestamp represented should also be in GMT, but not all FTP servers honor this.
    1658     * </p>
    1659     *
    1660     * @param path The file path to query.
    1661     * @return A string representing the last file modification time in {@code yyyyMMDDhhmmss} format.
    1662     * @throws IOException if an I/O error occurs.
    1663     * @since 2.0
    1664     */
    1665    public String getModificationTime(final String path) throws IOException {
    1666        if (FTPReply.isPositiveCompletion(mdtm(path))) {
    1667            // skip the return code (e.g. 213) and the space
    1668            return getReplyString(0).substring(4);
    1669        }
    1670        return null;
    1671    }
    1672
    1673    /**
    1674     * Gets 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.
    1675     * This method only returns a valid value AFTER a data connection has been opened after a call to {@link #enterLocalPassiveMode enterLocalPassiveMode()}.
    1676     * This is because FTPClient sends a PASV command to the server only just before opening a data connection, and not when you call
    1677     * {@link #enterLocalPassiveMode enterLocalPassiveMode()}.
    1678     *
    1679     * @return The passive host name if in passive mode, otherwise null.
    1680     */
    1681    public String getPassiveHost() {
    1682        return passiveHost;
    1683    }
    1684
    1685    /**
    1686     * Gets the local IP address in passive mode. Useful when there are multiple network cards.
    1687     *
    1688     * @return The local IP address in passive mode.
    1689     */
    1690    public InetAddress getPassiveLocalIPAddress() {
    1691        return passiveLocalHost;
    1692    }
    1693
    1694    /**
    1695     * Gets the data port of the passive host if we are in passive mode. This method only returns a valid value AFTER a data connection has been opened after a
    1696     * call to {@link #enterLocalPassiveMode enterLocalPassiveMode()}. This is because FTPClient sends a PASV command to the server only just before opening a
    1697     * data connection, and not when you call {@link #enterLocalPassiveMode enterLocalPassiveMode()}.
    1698     *
    1699     * @return The data port of the passive server. If not in passive mode, undefined.
    1700     */
    1701    public int getPassivePort() {
    1702        return passivePort;
    1703    }
    1704
    1705    /**
    1706     * Gets the value to be used for the data socket SO_RCVBUF option.
    1707     *
    1708     * @return The current buffer size.
    1709     * @since 3.3
    1710     */
    1711    public int getReceiveDataSocketBufferSize() {
    1712        return receiveDataSocketBufferSize;
    1713    }
    1714
    1715    /**
    1716     * Gets the reported host address for active mode EPRT/PORT commands; allows override of {@link #getHostAddress()}.
    1717     *
    1718     * Useful for FTP Client behind Firewall NAT.
    1719     *
    1720     * @return reportActiveExternalHost if non-null, else getHostAddress();
    1721     */
    1722    InetAddress getReportHostAddress() {
    1723        if (reportActiveExternalHost != null) {
    1724            return reportActiveExternalHost;
    1725        }
    1726        return getHostAddress();
    1727    }
    1728
    1729    /**
    1730     * Gets the restart offset.
    1731     *
    1732     * @return offset The offset into the remote file at which to start the next file transfer.
    1733     */
    1734    public long getRestartOffset() {
    1735        return restartOffset;
    1736    }
    1737
    1738    /**
    1739     * Gets the value to be used for the data socket SO_SNDBUF option.
    1740     *
    1741     * @return The current buffer size.
    1742     * @since 3.3
    1743     */
    1744    public int getSendDataSocketBufferSize() {
    1745        return sendDataSocketBufferSize;
    1746    }
    1747
    1748    /**
    1749     * Gets the size for a path.
    1750     * <p>
    1751     * Issue the FTP SIZE command to the server for a given path. This should produce the size of the file.
    1752     * </p>
    1753     *
    1754     * @param path the file name
    1755     * @return The size information returned by the server; {@code null} if there was an error
    1756     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1757     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1758     *                                      independently as itself.
    1759     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1760     * @since 3.7
    1761     */
    1762    public String getSize(final String path) throws IOException {
    1763        if (FTPReply.isPositiveCompletion(size(path))) {
    1764            return getReplyString(0).substring(4); // skip the return code (e.g. 213) and the space
    1765        }
    1766        return null;
    1767    }
    1768
    1769    /**
    1770     * Gets the status of the server.
    1771     * <p>
    1772     * Issue the FTP STAT command to the server.
    1773     * </p>
    1774     *
    1775     * @return The status information returned by the server.
    1776     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1777     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1778     *                                      independently as itself.
    1779     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1780     */
    1781    public String getStatus() throws IOException {
    1782        if (FTPReply.isPositiveCompletion(stat())) {
    1783            return getReplyString();
    1784        }
    1785        return null;
    1786    }
    1787
    1788    /**
    1789     * Gets the status of the server for a given path.
    1790     * <p>
    1791     * Issue the FTP STAT command to the server for a given path. This should produce a listing of the file or directory.
    1792     * </p>
    1793     *
    1794     * @param path the file name
    1795     * @return The status information returned by the server.
    1796     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1797     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1798     *                                      independently as itself.
    1799     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    1800     */
    1801    public String getStatus(final String path) throws IOException {
    1802        if (FTPReply.isPositiveCompletion(stat(path))) {
    1803            return getReplyString();
    1804        }
    1805        return null;
    1806    }
    1807
    1808    /**
    1809     * Gets the system name.
    1810     *
    1811     * @return the name
    1812     * @throws IOException on error
    1813     * @deprecated Use {@link #getSystemType()} instead
    1814     */
    1815    @Deprecated
    1816    public String getSystemName() throws IOException {
    1817        if (systemName == null && FTPReply.isPositiveCompletion(syst())) {
    1818            systemName = _replyLines.get(_replyLines.size() - 1).substring(4);
    1819        }
    1820        return systemName;
    1821    }
    1822
    1823    /**
    1824     * Gets 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
    1825     * 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
    1826     * and return the cached value until a call to disconnect.
    1827     * <p>
    1828     * If the SYST command fails, and the system property {@link #FTP_SYSTEM_TYPE_DEFAULT} is defined, then this is used instead.
    1829     * </p>
    1830     *
    1831     * @return The system type obtained from the server. Never null.
    1832     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    1833     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    1834     *                                      independently as itself.
    1835     * @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
    1836     *                                      default system type property is not defined)
    1837     * @since 2.2
    1838     */
    1839    public String getSystemType() throws IOException {
    1840        // if (syst() == FTPReply.NAME_SYSTEM_TYPE)
    1841        // Technically, we should expect a NAME_SYSTEM_TYPE response, but
    1842        // in practice FTP servers deviate, so we soften the condition to
    1843        // a positive completion.
    1844        if (systemName == null) {
    1845            if (FTPReply.isPositiveCompletion(syst())) {
    1846                // Assume that response is not empty here (cannot be null)
    1847                systemName = _replyLines.get(_replyLines.size() - 1).substring(4);
    1848            } else {
    1849                // Check if the user has provided a default for when the SYST command fails
    1850                final String systDefault = System.getProperty(FTP_SYSTEM_TYPE_DEFAULT);
    1851                if (systDefault == null) {
    1852                    throw new IOException("Unable to determine system type - response: " + getReplyString());
    1853                }
    1854                systemName = systDefault;
    1855            }
    1856        }
    1857        return systemName;
    1858    }
    1859
    1860    /**
    1861     * Gets the system type from the {@link #FTP_SYSTEM_TYPE} system property, or the server {@link #getSystemType()}, or the {@link #SYSTEM_TYPE_PROPERTIES}
    1862     * property file.
    1863     *
    1864     * @return The system type obtained from the server.
    1865     * @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 default system type
    1866     *                     property is not defined)
    1867     * @since 3.12.0
    1868     */
    1869    public String getSystemTypeOverride() throws IOException {
    1870        String systemType = System.getProperty(FTP_SYSTEM_TYPE);
    1871        if (systemType == null) {
    1872            systemType = getSystemType(); // cannot be null
    1873            final Properties override = getOverrideProperties();
    1874            if (override != null) {
    1875                final String newType = override.getProperty(systemType);
    1876                if (newType != null) {
    1877                    systemType = newType;
    1878                }
    1879            }
    1880        }
    1881        return systemType;
    1882    }
    1883
    1884    /**
    1885     * Queries the server for a supported feature. Caches the parsed response to avoid resending the command repeatedly.
    1886     *
    1887     * @param feature the name of the feature; it is converted to upper case.
    1888     * @return {@code true} if the feature is present, {@code false} if the feature is not present or the {@link #feat()} command failed. Check
    1889     *         {@link #getReplyCode()} or {@link #getReplyString()} if it is necessary to distinguish these cases.
    1890     *
    1891     * @throws IOException on error
    1892     * @since 3.8.0
    1893     */
    1894    public boolean hasFeature(final FTPCmd feature) throws IOException {
    1895        return hasFeature(feature.name());
    1896    }
    1897
    1898    /**
    1899     * Queries the server for a supported feature. Caches the parsed response to avoid resending the command repeatedly.
    1900     *
    1901     * @param feature the name of the feature; it is converted to upper case.
    1902     * @return {@code true} if the feature is present, {@code false} if the feature is not present or the {@link #feat()} command failed. Check
    1903     *         {@link #getReplyCode()} or {@link #getReplyString()} if it is necessary to distinguish these cases.
    1904     *
    1905     * @throws IOException on error
    1906     * @since 3.0
    1907     */
    1908    public boolean hasFeature(final String feature) throws IOException {
    1909        if (!initFeatureMap()) {
    1910            return false;
    1911        }
    1912        return featuresMap.containsKey(feature.toUpperCase(Locale.ENGLISH));
    1913    }
    1914
    1915    /**
    1916     * 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
    1917     * command repeatedly.
    1918     *
    1919     * @param feature the name of the feature; it is converted to upper case.
    1920     * @param value   the value to find.
    1921     * @return {@code true} if the feature is present, {@code false} if the feature is not present or the {@link #feat()} command failed. Check
    1922     *         {@link #getReplyCode()} or {@link #getReplyString()} if it is necessary to distinguish these cases.
    1923     *
    1924     * @throws IOException on error
    1925     * @since 3.0
    1926     */
    1927    public boolean hasFeature(final String feature, final String value) throws IOException {
    1928        if (!initFeatureMap()) {
    1929            return false;
    1930        }
    1931        final Set<String> entries = featuresMap.get(feature.toUpperCase(Locale.ENGLISH));
    1932        if (entries != null) {
    1933            return entries.contains(value);
    1934        }
    1935        return false;
    1936    }
    1937
    1938    private void initDefaults() {
    1939        dataConnectionMode = ACTIVE_LOCAL_DATA_CONNECTION_MODE;
    1940        passiveHost = null;
    1941        passivePort = -1;
    1942        activeExternalHost = null;
    1943        reportActiveExternalHost = null;
    1944        activeMinPort = 0;
    1945        activeMaxPort = 0;
    1946        fileType = ASCII_FILE_TYPE;
    1947        fileStructure = FILE_STRUCTURE;
    1948        formatOrByteSize = NON_PRINT_TEXT_FORMAT;
    1949        fileTransferMode = STREAM_TRANSFER_MODE;
    1950        restartOffset = 0;
    1951        systemName = null;
    1952        entryParser = null;
    1953        entryParserKey = "";
    1954        featuresMap = null;
    1955    }
    1956
    1957    /*
    1958     * Initializes the feature map if not already created.
    1959     */
    1960    private boolean initFeatureMap() throws IOException {
    1961        if (featuresMap == null) {
    1962            // Don't create map here, because next line may throw exception
    1963            final int replyCode = feat();
    1964            if (replyCode == FTPReply.NOT_LOGGED_IN) { // 503
    1965                return false; // NET-518; don't create empty map
    1966            }
    1967            final boolean success = FTPReply.isPositiveCompletion(replyCode);
    1968            // init the map here, so we don't keep trying if we know the command will fail
    1969            featuresMap = new HashMap<>();
    1970            if (!success) {
    1971                return false;
    1972            }
    1973            for (final String line : _replyLines) {
    1974                if (line.startsWith(" ")) { // it's a FEAT entry
    1975                    String key;
    1976                    String value = "";
    1977                    final int varsep = line.indexOf(' ', 1);
    1978                    if (varsep > 0) {
    1979                        key = line.substring(1, varsep);
    1980                        value = line.substring(varsep + 1);
    1981                    } else {
    1982                        key = line.substring(1);
    1983                    }
    1984                    key = key.toUpperCase(Locale.ENGLISH);
    1985                    final Set<String> entries = featuresMap.computeIfAbsent(key, k -> new HashSet<>());
    1986                    entries.add(value);
    1987                }
    1988            }
    1989        }
    1990        return true;
    1991    }
    1992
    1993    /**
    1994     * Using the default autodetect mechanism, initialize an FTPListParseEngine object containing a raw file information for the current working directory on
    1995     * 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
    1996     * with information filled in by the {@code FTPFileEntryParser} used.
    1997     * <p>
    1998     * 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
    1999     * lists.
    2000     * </p>
    2001     *
    2002     * @return A FTPListParseEngine object that holds the raw information and is capable of providing parsed FTPFile objects, one for each file containing
    2003     *         information contained in the given path in the format determined by the {@code parser} parameter. Null will be returned if a data connection
    2004     *         cannot be opened. If the current working directory contains no files, an empty array will be the return.
    2005     *
    2006     * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
    2007     *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
    2008     *                                                                         This exception may be caught either as an IOException or independently as itself.
    2009     * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
    2010     *                                                                         a reply from the server.
    2011     * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the autodetect mechanism cannot resolve the type of system we are
    2012     *                                                                         connected with.
    2013     * @see FTPListParseEngine
    2014     */
    2015    public FTPListParseEngine initiateListParsing() throws IOException {
    2016        return initiateListParsing((String) null);
    2017    }
    2018
    2019    /**
    2020     * private method through which all listFiles() and initiateListParsing methods pass once a parser is determined.
    2021     *
    2022     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2023     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2024     *                                      independently as itself.
    2025     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2026     * @see FTPListParseEngine
    2027     */
    2028    private FTPListParseEngine initiateListParsing(final FTPFileEntryParser parser, final String path) throws IOException {
    2029        final Socket socket = _openDataConnection_(FTPCmd.LIST, getListArguments(path));
    2030        final FTPListParseEngine engine = new FTPListParseEngine(parser, ftpClientConfig);
    2031        if (socket == null) {
    2032            return engine;
    2033        }
    2034        try {
    2035            engine.readServerList(socket.getInputStream(), getControlEncoding());
    2036        } finally {
    2037            IOUtils.closeQuietly(socket);
    2038        }
    2039        completePendingCommand();
    2040        return engine;
    2041    }
    2042
    2043    /**
    2044     * Using the default autodetect mechanism, initialize an FTPListParseEngine object containing a raw file information for the supplied directory. This
    2045     * information is obtained through the LIST command. This object is then capable of being iterated to return a sequence of FTPFile objects with information
    2046     * filled in by the {@link FTPFileEntryParser} used.
    2047     * <p>
    2048     * The server may or may not expand glob expressions. You should avoid using glob expressions because the return format for glob listings differs from
    2049     * server to server and will likely cause this method to fail.
    2050     * </p>
    2051     * <p>
    2052     * 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
    2053     * lists.
    2054     * </p>
    2055     *
    2056     * <pre>
    2057     * FTPClient f = FTPClient();
    2058     * f.connect(server);
    2059     * f.login(username, password);
    2060     * FTPListParseEngine engine = f.initiateListParsing(directory);
    2061     *
    2062     * while (engine.hasNext()) {
    2063     *     FTPFile[] files = engine.getNext(25); // "page size" you want
    2064     *     // do whatever you want with these files, display them, etc.
    2065     *     // expensive FTPFile objects not created until needed.
    2066     * }
    2067     * </pre>
    2068     *
    2069     * @param path the starting directory
    2070     * @return A FTPListParseEngine object that holds the raw information and is capable of providing parsed FTPFile objects, one for each file containing
    2071     *         information contained in the given path in the format determined by the {@code parser} parameter. Null will be returned if a data connection
    2072     *         cannot be opened. If the current working directory contains no files, an empty array will be the return.
    2073     *
    2074     * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
    2075     *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
    2076     *                                                                         This exception may be caught either as an IOException or independently as itself.
    2077     * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
    2078     *                                                                         a reply from the server.
    2079     * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the autodetect mechanism cannot resolve the type of system we are
    2080     *                                                                         connected with.
    2081     * @see FTPListParseEngine
    2082     */
    2083    public FTPListParseEngine initiateListParsing(final String path) throws IOException {
    2084        return initiateListParsing((String) null, path);
    2085    }
    2086
    2087    /**
    2088     * Using the supplied parser key, initialize an FTPListParseEngine object containing a raw file information for the supplied directory. This information is
    2089     * 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
    2090     * the {@link FTPFileEntryParser} used.
    2091     * <p>
    2092     * The server may or may not expand glob expressions. You should avoid using glob expressions because the return format for glob listings differs from
    2093     * server to server and will likely cause this method to fail.
    2094     * </p>
    2095     * <p>
    2096     * 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
    2097     * lists.
    2098     * </p>
    2099     *
    2100     * @param parserKey A string representing a designated code or fully-qualified class name of an {@link FTPFileEntryParser} that should be used to parse each
    2101     *                  server file listing. May be {@code null}, in which case the code checks first the system property {@link #FTP_SYSTEM_TYPE}, and if that
    2102     *                  is not defined the SYST command is used to provide the value. To allow for arbitrary system types, the return from the SYST command is
    2103     *                  used to look up an alias for the type in the {@link #SYSTEM_TYPE_PROPERTIES} properties file if it is available.
    2104     * @param path  the starting directory
    2105     * @return A FTPListParseEngine object that holds the raw information and is capable of providing parsed FTPFile objects, one for each file containing
    2106     *         information contained in the given path in the format determined by the {@code parser} parameter. Null will be returned if a data connection
    2107     *         cannot be opened. If the current working directory contains no files, an empty array will be the return.
    2108     *
    2109     * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
    2110     *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
    2111     *                                                                         This exception may be caught either as an IOException or independently as itself.
    2112     * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
    2113     *                                                                         a reply from the server.
    2114     * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the parserKey parameter cannot be resolved by the selected parser
    2115     *                                                                         factory. In the DefaultFTPEntryParserFactory, this will happen when parserKey is
    2116     *                                                                         neither the fully qualified class name of a class implementing the interface
    2117     *                                                                         org.apache.commons.net.ftp.FTPFileEntryParser nor a string containing one of the
    2118     *                                                                         recognized keys mapping to such a parser or if class loader security issues
    2119     *                                                                         prevent its being loaded.
    2120     * @see FTPListParseEngine
    2121     */
    2122    public FTPListParseEngine initiateListParsing(final String parserKey, final String path) throws IOException {
    2123        createParser(parserKey); // create and cache parser
    2124        return initiateListParsing(entryParser, path);
    2125    }
    2126
    2127    /**
    2128     * Initiate list parsing for MLSD listings in the current working directory.
    2129     *
    2130     * @return the engine
    2131     * @throws IOException on error
    2132     */
    2133    public FTPListParseEngine initiateMListParsing() throws IOException {
    2134        return initiateMListParsing(null);
    2135    }
    2136
    2137    /**
    2138     * Initiate list parsing for MLSD listings.
    2139     *
    2140     * @param path the path from where to MLSD.
    2141     * @return the engine.
    2142     * @throws IOException on error
    2143     */
    2144    public FTPListParseEngine initiateMListParsing(final String path) throws IOException {
    2145        final Socket socket = _openDataConnection_(FTPCmd.MLSD, path);
    2146        final FTPListParseEngine engine = new FTPListParseEngine(MLSxEntryParser.getInstance(), ftpClientConfig);
    2147        if (socket == null) {
    2148            return engine;
    2149        }
    2150        try {
    2151            engine.readServerList(socket.getInputStream(), getControlEncoding());
    2152        } finally {
    2153            IOUtils.closeQuietly(socket);
    2154            completePendingCommand();
    2155        }
    2156        return engine;
    2157    }
    2158
    2159    /**
    2160     * Tests 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
    2161     * address will be silently ignored, and replaced with the remote IP address of the control connection, unless this configuration option is given, which
    2162     * restores the old behavior. To enable this by default, use the system property {@link FTPClient#FTP_IP_ADDRESS_FROM_PASV_RESPONSE}.
    2163     *
    2164     * @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).
    2165     * @see FTPClient#FTP_IP_ADDRESS_FROM_PASV_RESPONSE
    2166     * @see #setIpAddressFromPasvResponse(boolean)
    2167     * @since 3.9.0
    2168     */
    2169    public boolean isIpAddressFromPasvResponse() {
    2170        return ipAddressFromPasvResponse;
    2171    }
    2172
    2173    /**
    2174     * Tests whether or not verification of the remote host participating in data connections is enabled. The default behavior is for verification to be
    2175     * enabled.
    2176     *
    2177     * @return True if verification is enabled, false if not.
    2178     */
    2179    public boolean isRemoteVerificationEnabled() {
    2180        return remoteVerificationEnabled;
    2181    }
    2182
    2183    /**
    2184     * Tests whether to attempt using EPSV with IPv4. Default (if not set) is {@code false}
    2185     *
    2186     * @return true if EPSV shall be attempted with IPv4.
    2187     * @since 2.2
    2188     */
    2189    public boolean isUseEPSVwithIPv4() {
    2190        return useEPSVwithIPv4;
    2191    }
    2192
    2193    /**
    2194     * Using the default system autodetect mechanism, obtain a list of directories contained in the current working directory.
    2195     * <p>
    2196     * This information is obtained through the LIST command. The contents of the returned array is determined by the{@link FTPFileEntryParser} used.
    2197     * </p>
    2198     * <p>
    2199     * The LIST command does not generally return very precise timestamps. For recent files, the response usually contains hours and minutes (not seconds).
    2200     * 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
    2201     * include milliseconds. See {@link #mlistDir()}
    2202     * </p>
    2203     *
    2204     * @return The list of directories contained in the current directory in the format determined by the autodetection mechanism.
    2205     * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
    2206     *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
    2207     *                                                                         This exception may be caught either as an IOException or independently as itself.
    2208     * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
    2209     *                                                                         a reply from the server.
    2210     * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the parserKey parameter cannot be resolved by the selected parser
    2211     *                                                                         factory. In the DefaultFTPEntryParserFactory, this will happen when parserKey is
    2212     *                                                                         neither the fully qualified class name of a class implementing the interface
    2213     *                                                                         org.apache.commons.net.ftp.FTPFileEntryParser nor a string containing one of the
    2214     *                                                                         recognized keys mapping to such a parser or if class loader security issues
    2215     *                                                                         prevent its being loaded.
    2216     * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
    2217     * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
    2218     * @see org.apache.commons.net.ftp.FTPFileEntryParser
    2219     * @since 3.0
    2220     */
    2221    public FTPFile[] listDirectories() throws IOException {
    2222        return listDirectories((String) null);
    2223    }
    2224
    2225    /**
    2226     * Using the default system autodetect mechanism, obtain a list of directories contained in the specified directory.
    2227     * <p>
    2228     * This information is obtained through the LIST command. The contents of the returned array is determined by the{@link FTPFileEntryParser} used.
    2229     * </p>
    2230     * <p>
    2231     * The LIST command does not generally return very precise timestamps. For recent files, the response usually contains hours and minutes (not seconds).
    2232     * 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
    2233     * include milliseconds. See {@link #mlistDir()}
    2234     * </p>
    2235     *
    2236     * @param parent the starting directory
    2237     * @return The list of directories contained in the specified directory in the format determined by the autodetection mechanism.
    2238     * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
    2239     *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
    2240     *                                                                         This exception may be caught either as an IOException or independently as itself.
    2241     * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
    2242     *                                                                         a reply from the server.
    2243     * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the parserKey parameter cannot be resolved by the selected parser
    2244     *                                                                         factory. In the DefaultFTPEntryParserFactory, this will happen when parserKey is
    2245     *                                                                         neither the fully qualified class name of a class implementing the interface
    2246     *                                                                         org.apache.commons.net.ftp.FTPFileEntryParser nor a string containing one of the
    2247     *                                                                         recognized keys mapping to such a parser or if class loader security issues
    2248     *                                                                         prevent its being loaded.
    2249     * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
    2250     * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
    2251     * @see org.apache.commons.net.ftp.FTPFileEntryParser
    2252     * @since 3.0
    2253     */
    2254    public FTPFile[] listDirectories(final String parent) throws IOException {
    2255        return listFiles(parent, FTPFileFilters.DIRECTORIES);
    2256    }
    2257
    2258    /**
    2259     * Using the default system autodetect mechanism, obtain a list of file information for the current working directory.
    2260     * <p>
    2261     * This information is obtained through the LIST command. The contents of the returned array is determined by the {@link FTPFileEntryParser} used.
    2262     * </p>
    2263     * <p>
    2264     * The LIST command does not generally return very precise timestamps. For recent files, the response usually contains hours and minutes (not seconds).
    2265     * 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
    2266     * include milliseconds. See {@link #mlistDir()}
    2267     * </p>
    2268     *
    2269     * @return The list of file information contained in the current directory in the format determined by the autodetection mechanism. <strong>NOTE:</strong>
    2270     *         This array may contain null members if any of the individual file listings failed to parse. The caller should check each entry for null before
    2271     *         referencing it.
    2272     * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
    2273     *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
    2274     *                                                                         This exception may be caught either as an IOException or independently as itself.
    2275     * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
    2276     *                                                                         a reply from the server.
    2277     * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the parserKey parameter cannot be resolved by the selected parser
    2278     *                                                                         factory. In the DefaultFTPEntryParserFactory, this will happen when parserKey is
    2279     *                                                                         neither the fully qualified class name of a class implementing the interface
    2280     *                                                                         org.apache.commons.net.ftp.FTPFileEntryParser nor a string containing one of the
    2281     *                                                                         recognized keys mapping to such a parser or if class loader security issues
    2282     *                                                                         prevent its being loaded.
    2283     * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
    2284     * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
    2285     * @see org.apache.commons.net.ftp.FTPFileEntryParser
    2286     */
    2287    public FTPFile[] listFiles() throws IOException {
    2288        return listFiles((String) null);
    2289    }
    2290
    2291    /**
    2292     * Using the default system autodetect mechanism, obtain a list of file information for the current working directory or for just a single file.
    2293     * <p>
    2294     * This information is obtained through the LIST command. The contents of the returned array is determined by the {@link FTPFileEntryParser} used.
    2295     * </p>
    2296     * <p>
    2297     * The LIST command does not generally return very precise timestamps. For recent files, the response usually contains hours and minutes (not seconds).
    2298     * 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
    2299     * include milliseconds. See {@link #mlistDir()}
    2300     * </p>
    2301     *
    2302     * @param path 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
    2303     *                 cause this method to fail. Also, some servers treat a leading '-' as being an option. To avoid this interpretation, use an absolute
    2304     *                 path or prefix the path with ./ (Unix style servers). Some servers may support "--" as meaning end of options, in which case "--
    2305     *                 -xyz" should work.
    2306     * @return The list of file information contained in the given path in the format determined by the autodetection mechanism
    2307     * @throws FTPConnectionClosedException                                    If the FTP server prematurely closes the connection as a result of the client
    2308     *                                                                         being idle or some other reason causing the server to send FTP reply code 421.
    2309     *                                                                         This exception may be caught either as an IOException or independently as itself.
    2310     * @throws IOException                                                     If an I/O error occurs while either sending a command to the server or receiving
    2311     *                                                                         a reply from the server.
    2312     * @throws org.apache.commons.net.ftp.parser.ParserInitializationException Thrown if the parserKey parameter cannot be resolved by the selected parser
    2313     *                                                                         factory. In the DefaultFTPEntryParserFactory, this will happen when parserKey is
    2314     *                                                                         neither the fully qualified class name of a class implementing the interface
    2315     *                                                                         org.apache.commons.net.ftp.FTPFileEntryParser nor a string containing one of the
    2316     *                                                                         recognized keys mapping to such a parser or if class loader security issues
    2317     *                                                                         prevent its being loaded.
    2318     * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
    2319     * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
    2320     * @see org.apache.commons.net.ftp.FTPFileEntryParser
    2321     */
    2322    public FTPFile[] listFiles(final String path) throws IOException {
    2323        return initiateListParsing((String) null, path).getFiles();
    2324    }
    2325
    2326    /**
    2327     * Version of {@link #listFiles(String)} which allows a filter to be provided. For example: {@code listFiles("site", FTPFileFilters.DIRECTORY);}
    2328     *
    2329     * @param path the initial path, may be null
    2330     * @param filter   the filter, non-null
    2331     * @return the array of FTPFile entries.
    2332     * @throws IOException on error
    2333     * @since 2.2
    2334     */
    2335    public FTPFile[] listFiles(final String path, final FTPFileFilter filter) throws IOException {
    2336        return initiateListParsing((String) null, path).getFiles(filter);
    2337    }
    2338
    2339    /**
    2340     * Fetches the system help information from the server and returns the full string.
    2341     *
    2342     * @return The system help string obtained from the server. null if the information could not be obtained.
    2343     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2344     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2345     *                                      independently as itself.
    2346     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2347     */
    2348    public String listHelp() throws IOException {
    2349        return FTPReply.isPositiveCompletion(help()) ? getReplyString() : null;
    2350    }
    2351
    2352    /**
    2353     * Fetches the help information for a given command from the server and returns the full string.
    2354     *
    2355     * @param command The command on which to ask for help.
    2356     * @return The command help string obtained from the server. null if the information could not be obtained.
    2357     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2358     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2359     *                                      independently as itself.
    2360     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2361     */
    2362    public String listHelp(final String command) throws IOException {
    2363        return FTPReply.isPositiveCompletion(help(command)) ? getReplyString() : null;
    2364    }
    2365
    2366    /**
    2367     * 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
    2368     * 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
    2369     * 550 error No files found.). If the directory is not empty, an array of file names in the directory is returned.
    2370     *
    2371     * @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
    2372     *         directory, a zero-length array is returned.
    2373     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2374     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2375     *                                      independently as itself.
    2376     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2377     */
    2378    public String[] listNames() throws IOException {
    2379        return listNames(null);
    2380    }
    2381
    2382    /**
    2383     * 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
    2384     * NLST command. If the given path is a directory and contains no files, a zero length array is returned only if the FTP server returned a positive
    2385     * 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
    2386     * in the directory is returned. If the path corresponds to a file, only that file will be listed. The server may or may not expand glob expressions.
    2387     *
    2388     * @param path 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
    2389     *                 prefix the path with ./ (Unix style servers). Some servers may support "--" as meaning end of options, in which case "-- -xyz" should
    2390     *                 work.
    2391     * @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
    2392     *         zero-length array is returned.
    2393     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2394     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2395     *                                      independently as itself.
    2396     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2397     */
    2398    public String[] listNames(final String path) throws IOException {
    2399        final List<String> results;
    2400        try (Socket socket = _openDataConnection_(FTPCmd.NLST, getListArguments(path))) {
    2401            if (socket == null) {
    2402                return null;
    2403            }
    2404            try (InputStream in = socket.getInputStream()) {
    2405                results = IOUtils.readLines(in, getControlEncoding());
    2406            }
    2407        }
    2408        if (completePendingCommand()) {
    2409            return results.toArray(NetConstants.EMPTY_STRING_ARRAY);
    2410        }
    2411        return null;
    2412    }
    2413
    2414    /**
    2415     * Login to the FTP server using the provided user and password.
    2416     *
    2417     * @param user     The user name to login under.
    2418     * @param password The password to use.
    2419     * @return True if successfully completed, false if not.
    2420     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2421     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2422     *                                      independently as itself.
    2423     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2424     */
    2425    public boolean login(final String user, final String password) throws IOException {
    2426        user(user);
    2427        if (FTPReply.isPositiveCompletion(_replyCode)) {
    2428            return true;
    2429        }
    2430        // If we get here, we either have an error code, or an intermediate
    2431        // reply requesting password.
    2432        if (!FTPReply.isPositiveIntermediate(_replyCode)) {
    2433            return false;
    2434        }
    2435        return FTPReply.isPositiveCompletion(pass(password));
    2436    }
    2437
    2438    /**
    2439     * 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
    2440     * account information is not used.
    2441     *
    2442     * @param user     The user name to login under.
    2443     * @param password The password to use.
    2444     * @param account  The account to use.
    2445     * @return True if successfully completed, false if not.
    2446     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2447     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2448     *                                      independently as itself.
    2449     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2450     */
    2451    public boolean login(final String user, final String password, final String account) throws IOException {
    2452        user(user);
    2453        if (FTPReply.isPositiveCompletion(_replyCode)) {
    2454            return true;
    2455        }
    2456        // If we get here, we either have an error code, or an intermediate
    2457        // reply requesting password.
    2458        if (!FTPReply.isPositiveIntermediate(_replyCode)) {
    2459            return false;
    2460        }
    2461        pass(password);
    2462        if (FTPReply.isPositiveCompletion(_replyCode)) {
    2463            return true;
    2464        }
    2465        if (!FTPReply.isPositiveIntermediate(_replyCode)) {
    2466            return false;
    2467        }
    2468        return FTPReply.isPositiveCompletion(acct(account));
    2469    }
    2470
    2471    /**
    2472     * Logout of the FTP server by sending the QUIT command.
    2473     *
    2474     * @return True if successfully completed, false if not.
    2475     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2476     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2477     *                                      independently as itself.
    2478     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2479     */
    2480    public boolean logout() throws IOException {
    2481        return FTPReply.isPositiveCompletion(quit());
    2482    }
    2483
    2484    /**
    2485     * Creates a new subdirectory on the FTP server in the current directory (if a relative path is given) or where specified (if an absolute path is
    2486     * given).
    2487     *
    2488     * @param path The path of the directory to create.
    2489     * @return True if successfully completed, false if not.
    2490     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2491     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2492     *                                      independently as itself.
    2493     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2494     */
    2495    public boolean makeDirectory(final String path) throws IOException {
    2496        return FTPReply.isPositiveCompletion(mkd(path));
    2497    }
    2498
    2499    /**
    2500     * 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
    2501     * 3077 form "yyyyMMDDhhmmss(.xxx)?". The timestamp represented should also be in GMT, but not all FTP servers honor this.
    2502     *
    2503     * @param path The file path to query.
    2504     * @return A Calendar representing the last file modification time, may be {@code null}. The Calendar timestamp will be null if a parse error occurs.
    2505     * @throws IOException if an I/O error occurs.
    2506     * @since 3.8.0
    2507     */
    2508    public Calendar mdtmCalendar(final String path) throws IOException {
    2509        final String modificationTime = getModificationTime(path);
    2510        if (modificationTime != null) {
    2511            return MLSxEntryParser.parseGMTdateTime(modificationTime);
    2512        }
    2513        return null;
    2514    }
    2515
    2516    /**
    2517     * 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
    2518     * 3077 form "yyyyMMDDhhmmss(.xxx)?". The timestamp represented should also be in GMT, but not all FTP servers honor this.
    2519     *
    2520     * @param path The file path to query.
    2521     * @return A FTPFile representing the last file modification time, may be {@code null}. The FTPFile timestamp will be null if a parse error occurs.
    2522     * @throws IOException if an I/O error occurs.
    2523     * @since 3.4
    2524     */
    2525    public FTPFile mdtmFile(final String path) throws IOException {
    2526        final String modificationTime = getModificationTime(path);
    2527        if (modificationTime != null) {
    2528            final FTPFile file = new FTPFile();
    2529            file.setName(path);
    2530            file.setRawListing(modificationTime);
    2531            file.setTimestamp(MLSxEntryParser.parseGMTdateTime(modificationTime));
    2532            return file;
    2533        }
    2534        return null;
    2535    }
    2536
    2537    /**
    2538     * 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
    2539     * 3077 form "yyyyMMDDhhmmss(.xxx)?". The timestamp represented should also be in GMT, but not all FTP servers honor this.
    2540     *
    2541     * @param path The file path to query.
    2542     * @return An Instant representing the last file modification time, may be {@code null}. The Instant timestamp will be null if a parse error occurs.
    2543     * @throws IOException if an I/O error occurs.
    2544     * @since 3.9.0
    2545     */
    2546    public Instant mdtmInstant(final String path) throws IOException {
    2547        final String modificationTime = getModificationTime(path);
    2548        if (modificationTime != null) {
    2549            return MLSxEntryParser.parseGmtInstant(modificationTime);
    2550        }
    2551        return null;
    2552    }
    2553
    2554    /**
    2555     * Merge two copystream listeners, either or both of which may be null.
    2556     *
    2557     * @param local the listener used by this class, may be null
    2558     * @return a merged listener or a single listener or null
    2559     * @since 3.0
    2560     */
    2561    private CopyStreamListener mergeListeners(final CopyStreamListener local) {
    2562        if (local == null) {
    2563            return copyStreamListener;
    2564        }
    2565        if (copyStreamListener == null) {
    2566            return local;
    2567        }
    2568        // Both are non-null
    2569        final CopyStreamAdapter merged = new CopyStreamAdapter();
    2570        merged.addCopyStreamListener(local);
    2571        merged.addCopyStreamListener(copyStreamListener);
    2572        return merged;
    2573    }
    2574
    2575    /**
    2576     * Generate a directory listing for the current directory using the MLSD command.
    2577     *
    2578     * @return the array of file entries
    2579     * @throws IOException on error
    2580     * @since 3.0
    2581     */
    2582    public FTPFile[] mlistDir() throws IOException {
    2583        return mlistDir(null);
    2584    }
    2585
    2586    /**
    2587     * Generate a directory listing using the MLSD command.
    2588     *
    2589     * @param path the directory name, may be {@code null}
    2590     * @return the array of file entries
    2591     * @throws IOException on error
    2592     * @since 3.0
    2593     */
    2594    public FTPFile[] mlistDir(final String path) throws IOException {
    2595        return initiateMListParsing(path).getFiles();
    2596    }
    2597
    2598    /**
    2599     * Generate a directory listing using the MLSD command.
    2600     *
    2601     * @param path the directory name, may be {@code null}
    2602     * @param filter   the filter to apply to the responses
    2603     * @return the array of file entries
    2604     * @throws IOException on error
    2605     * @since 3.0
    2606     */
    2607    public FTPFile[] mlistDir(final String path, final FTPFileFilter filter) throws IOException {
    2608        return initiateMListParsing(path).getFiles(filter);
    2609    }
    2610
    2611    /**
    2612     * Gets file details using the MLST command
    2613     *
    2614     * @param path the file or directory to list, may be {@code null}
    2615     * @return the file details, may be {@code null}
    2616     * @throws IOException on error
    2617     * @since 3.0
    2618     */
    2619    public FTPFile mlistFile(final String path) throws IOException {
    2620        final boolean success = FTPReply.isPositiveCompletion(sendCommand(FTPCmd.MLST, path));
    2621        if (success) {
    2622            String reply = getReplyString(1);
    2623            // some FTP server reply not contains space before fact(s)
    2624            if (reply.charAt(0) != ' ') {
    2625                reply = " " + reply;
    2626            }
    2627            /*
    2628             * 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
    2629             * needed.
    2630             */
    2631            if (reply.length() < 3) {
    2632                throw new MalformedServerReplyException("Invalid server reply (MLST): '" + reply + "'");
    2633            }
    2634            // some FTP server reply contains more than one space before fact(s)
    2635            final String entry = reply.replaceAll("^\\s+", ""); // skip leading space for parser
    2636            return MLSxEntryParser.parseEntry(entry);
    2637        }
    2638        return null;
    2639    }
    2640
    2641    /**
    2642     * Returns the path of the current working directory.
    2643     *
    2644     * @return The path of the current working directory. If it cannot be obtained, returns null.
    2645     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2646     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2647     *                                      independently as itself.
    2648     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2649     */
    2650    public String printWorkingDirectory() throws IOException {
    2651        if (pwd() != FTPReply.PATHNAME_CREATED) {
    2652            return null;
    2653        }
    2654        return parsePathname(_replyLines.get(_replyLines.size() - 1));
    2655    }
    2656
    2657    /**
    2658     * Reinitialize the FTP session. Not all FTP servers support this command, which issues the FTP REIN command.
    2659     *
    2660     * @return True if successfully completed, false if not.
    2661     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2662     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2663     *                                      independently as itself.
    2664     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2665     * @since 3.4 (made public)
    2666     */
    2667    public boolean reinitialize() throws IOException {
    2668        rein();
    2669        if (FTPReply.isPositiveCompletion(_replyCode) || FTPReply.isPositivePreliminary(_replyCode) && FTPReply.isPositiveCompletion(getReply())) {
    2670            initDefaults();
    2671            return true;
    2672        }
    2673        return false;
    2674    }
    2675
    2676    /**
    2677     * 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.
    2678     * The other server must have had a {@code remoteRetrieve} issued to it by another FTPClient.
    2679     *
    2680     * @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.
    2681     * @return True if successfully completed, false if not.
    2682     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2683     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2684     *                                      independently as itself.
    2685     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2686     */
    2687    public boolean remoteAppend(final String fileName) throws IOException {
    2688        if (dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE || dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE) {
    2689            return FTPReply.isPositivePreliminary(appe(fileName));
    2690        }
    2691        return false;
    2692    }
    2693
    2694    /**
    2695     * 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.
    2696     *
    2697     * @param fileName The name of the file to retrieve.
    2698     * @return True if successfully completed, false if not.
    2699     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2700     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2701     *                                      independently as itself.
    2702     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2703     */
    2704    public boolean remoteRetrieve(final String fileName) throws IOException {
    2705        if (dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE || dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE) {
    2706            return FTPReply.isPositivePreliminary(retr(fileName));
    2707        }
    2708        return false;
    2709    }
    2710
    2711    /**
    2712     * 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
    2713     * given file name. The other server must have had a {@code remoteRetrieve} issued to it by another FTPClient.
    2714     *
    2715     * @param fileName The name to call the file that is to be stored.
    2716     * @return True if successfully completed, false if not.
    2717     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2718     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2719     *                                      independently as itself.
    2720     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2721     */
    2722    public boolean remoteStore(final String fileName) throws IOException {
    2723        if (dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE || dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE) {
    2724            return FTPReply.isPositivePreliminary(stor(fileName));
    2725        }
    2726        return false;
    2727    }
    2728
    2729    /**
    2730     * 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
    2731     * unique file name. The other server must have had a {@code remoteRetrieve} issued to it by another FTPClient. Many FTP servers require that a base file
    2732     * name be given from which the unique file name can be derived. For those servers use the other version of {@code remoteStoreUnique}
    2733     *
    2734     * @return True if successfully completed, false if not.
    2735     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2736     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2737     *                                      independently as itself.
    2738     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2739     */
    2740    public boolean remoteStoreUnique() throws IOException {
    2741        if (dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE || dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE) {
    2742            return FTPReply.isPositivePreliminary(stou());
    2743        }
    2744        return false;
    2745    }
    2746
    2747    /**
    2748     * 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
    2749     * unique file name based on the given file name. The other server must have had a {@code remoteRetrieve} issued to it by another FTPClient.
    2750     *
    2751     * @param fileName The name on which to base the file name of the file that is to be stored.
    2752     * @return True if successfully completed, false if not.
    2753     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2754     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2755     *                                      independently as itself.
    2756     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2757     */
    2758    public boolean remoteStoreUnique(final String fileName) throws IOException {
    2759        if (dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE || dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE) {
    2760            return FTPReply.isPositivePreliminary(stou(fileName));
    2761        }
    2762        return false;
    2763    }
    2764
    2765    /**
    2766     * Removes a directory on the FTP server (if empty).
    2767     *
    2768     * @param path The path of the directory to remove.
    2769     * @return True if successfully completed, false if not.
    2770     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2771     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2772     *                                      independently as itself.
    2773     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2774     */
    2775    public boolean removeDirectory(final String path) throws IOException {
    2776        return FTPReply.isPositiveCompletion(rmd(path));
    2777    }
    2778
    2779    /**
    2780     * Renames a remote file.
    2781     *
    2782     * @param from The name of the remote file to rename.
    2783     * @param to   The new name of the remote file.
    2784     * @return True if successfully completed, false if not.
    2785     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2786     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2787     *                                      independently as itself.
    2788     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2789     */
    2790    public boolean rename(final String from, final String to) throws IOException {
    2791        if (!FTPReply.isPositiveIntermediate(rnfr(from))) {
    2792            return false;
    2793        }
    2794        return FTPReply.isPositiveCompletion(rnto(to));
    2795    }
    2796
    2797    /**
    2798     * Restart a {@code STREAM_TRANSFER_MODE} file transfer starting from the given offset. This will only work on FTP servers supporting the REST comand for
    2799     * the stream transfer mode. However, most FTP servers support this. Any subsequent file transfer will start reading or writing the remote file from the
    2800     * indicated offset.
    2801     *
    2802     * @param offset The offset into the remote file at which to start the next file transfer.
    2803     * @return True if successfully completed, false if not.
    2804     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2805     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2806     *                                      independently as itself.
    2807     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2808     * @since 3.1 (changed from private to protected)
    2809     */
    2810    protected boolean restart(final long offset) throws IOException {
    2811        restartOffset = 0;
    2812        return FTPReply.isPositiveIntermediate(rest(Long.toString(offset)));
    2813    }
    2814
    2815    /**
    2816     * 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
    2817     * type is ASCII, line separators in the file are converted to the local representation.
    2818     * <p>
    2819     * Note: if you have used {@link #setRestartOffset(long)}, the file data will start from the selected offset.
    2820     *
    2821     * @param remote The name of the remote file.
    2822     * @param local  The local OutputStream to which to write the file.
    2823     * @return True if successfully completed, false if not.
    2824     * @throws FTPConnectionClosedException                  If the FTP server prematurely closes the connection as a result of the client being idle or some
    2825     *                                                       other reason causing the server to send FTP reply code 421. This exception may be caught either as
    2826     *                                                       an IOException or independently as itself.
    2827     * @throws org.apache.commons.net.io.CopyStreamException If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to
    2828     *                                                       determine the number of bytes transferred and the IOException causing the error. This exception may
    2829     *                                                       be caught either as an IOException or independently as itself.
    2830     * @throws IOException                                   If an I/O error occurs while either sending a command to the server or receiving a reply from the
    2831     *                                                       server.
    2832     */
    2833    public boolean retrieveFile(final String remote, final OutputStream local) throws IOException {
    2834        return _retrieveFile(FTPCmd.RETR.getCommand(), remote, local);
    2835    }
    2836
    2837    /**
    2838     * 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
    2839     * 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
    2840     * of closing the parent data connection socket upon being closed.
    2841     * <p>
    2842     * <strong>To finalize the file transfer you must call {@link #completePendingCommand completePendingCommand} and check its return value to verify
    2843     * success.</strong> If this is not done, subsequent commands may behave unexpectedly.
    2844     * <p>
    2845     * Note: if you have used {@link #setRestartOffset(long)}, the file data will start from the selected offset.
    2846     *
    2847     * @param remote The name of the remote file.
    2848     * @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
    2849     *         (in which case you may check the reply code to determine the exact reason for failure).
    2850     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2851     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2852     *                                      independently as itself.
    2853     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2854     */
    2855    public InputStream retrieveFileStream(final String remote) throws IOException {
    2856        return _retrieveFileStream(FTPCmd.RETR.getCommand(), remote);
    2857    }
    2858
    2859    /**
    2860     * Sends a NOOP command to the FTP server. This is useful for preventing server timeouts.
    2861     *
    2862     * @return True if successfully completed, false if not.
    2863     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    2864     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    2865     *                                      independently as itself.
    2866     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    2867     */
    2868    public boolean sendNoOp() throws IOException {
    2869        return FTPReply.isPositiveCompletion(noop());
    2870    }
    2871
    2872    /**
    2873     * Send a site specific command.
    2874     *
    2875     * @param arguments The site specific command and arguments.
    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 sendSiteCommand(final String arguments) throws IOException {
    2883        return FTPReply.isPositiveCompletion(site(arguments));
    2884    }
    2885
    2886    /**
    2887     * Sets the external IP address in active mode. Useful when there are multiple network cards.
    2888     *
    2889     * @param ipAddress The external IP address of this machine.
    2890     * @throws UnknownHostException if the ipAddress cannot be resolved
    2891     * @since 2.2
    2892     */
    2893    public void setActiveExternalIPAddress(final String ipAddress) throws UnknownHostException {
    2894        activeExternalHost = InetAddress.getByName(ipAddress);
    2895    }
    2896
    2897    /**
    2898     * Sets the client side port range in active mode.
    2899     *
    2900     * @param activeMinPort The lowest available port (inclusive).
    2901     * @param activeMaxPort The highest available port (inclusive).
    2902     * @since 2.2
    2903     */
    2904    public void setActivePortRange(final int activeMinPort, final int activeMaxPort) {
    2905        this.activeMinPort = activeMinPort;
    2906        this.activeMaxPort = activeMaxPort;
    2907    }
    2908
    2909    /**
    2910     * Sets automatic server encoding detection (only UTF-8 supported).
    2911     * <p>
    2912     * Does not affect existing connections; must be invoked before a connection is established.
    2913     * </p>
    2914     *
    2915     * @param autoDetectEncoding If true, automatic server encoding detection will be enabled.
    2916     */
    2917    public void setAutodetectUTF8(final boolean autoDetectEncoding) {
    2918        this.autoDetectEncoding = autoDetectEncoding;
    2919    }
    2920
    2921    /**
    2922     * Sets the internal buffer size for buffered data streams.
    2923     *
    2924     * @param bufferSize The size of the buffer. Use a non-positive value to use the default.
    2925     */
    2926    public void setBufferSize(final int bufferSize) {
    2927        this.bufferSize = bufferSize;
    2928    }
    2929
    2930    /**
    2931     * Sets the duration to wait for control keep-alive message replies.
    2932     *
    2933     * @param timeout duration to wait (defaults to 1,000). Zero (or less) disables.
    2934     * @since 3.0
    2935     * @see #setControlKeepAliveTimeout(Duration)
    2936     */
    2937    public void setControlKeepAliveReplyTimeout(final Duration timeout) {
    2938        controlKeepAliveReplyTimeout = DurationUtils.zeroIfNull(timeout);
    2939    }
    2940
    2941    /**
    2942     * Sets the duration to wait for control keep-alive message replies.
    2943     *
    2944     * @param timeoutMillis number of milliseconds to wait (defaults to 1,000).
    2945     * @since 3.0
    2946     * @see #setControlKeepAliveTimeout(long)
    2947     * @deprecated Use {@link #setControlKeepAliveReplyTimeout(Duration)}.
    2948     */
    2949    @Deprecated
    2950    public void setControlKeepAliveReplyTimeout(final int timeoutMillis) {
    2951        controlKeepAliveReplyTimeout = Duration.ofMillis(timeoutMillis);
    2952    }
    2953
    2954    /**
    2955     * Sets the duration to wait between sending control connection keepalive messages when processing file upload or download.
    2956     * <p>
    2957     * See the class Javadoc section "Control channel keep-alive feature"
    2958     * </p>
    2959     *
    2960     * @param controlIdle the duration to wait between keepalive messages. Zero (or less) disables.
    2961     * @since 3.9.0
    2962     * @see #setControlKeepAliveReplyTimeout(Duration)
    2963     */
    2964    public void setControlKeepAliveTimeout(final Duration controlIdle) {
    2965        controlKeepAliveTimeout = DurationUtils.zeroIfNull(controlIdle);
    2966    }
    2967
    2968    /**
    2969     * Sets the duration to wait between sending control connection keepalive messages when processing file upload or download.
    2970     * <p>
    2971     * See the class Javadoc section "Control channel keep-alive feature"
    2972     * </p>
    2973     *
    2974     * @param controlIdleSeconds the wait in seconds between keepalive messages. Zero (or less) disables.
    2975     * @since 3.0
    2976     * @see #setControlKeepAliveReplyTimeout(int)
    2977     * @deprecated Use {@link #setControlKeepAliveTimeout(Duration)}.
    2978     */
    2979    @Deprecated
    2980    public void setControlKeepAliveTimeout(final long controlIdleSeconds) {
    2981        controlKeepAliveTimeout = Duration.ofSeconds(controlIdleSeconds);
    2982    }
    2983
    2984    /**
    2985     * Sets the listener to be used when performing store/retrieve operations. The default value (if not set) is {@code null}.
    2986     *
    2987     * @param copyStreamListener to be used, may be {@code null} to disable
    2988     * @since 3.0
    2989     */
    2990    public void setCopyStreamListener(final CopyStreamListener copyStreamListener) {
    2991        this.copyStreamListener = copyStreamListener;
    2992    }
    2993
    2994    /**
    2995     * 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
    2996     * value is &ge; 0.
    2997     * <p>
    2998     * <strong>Note:</strong> the timeout will also be applied when calling accept() whilst establishing an active local data connection.
    2999     * </p>
    3000     *
    3001     * @param timeout The default timeout that is used when opening a data connection socket. The value 0 (or null) means an infinite timeout.
    3002     * @since 3.9.0
    3003     */
    3004    public void setDataTimeout(final Duration timeout) {
    3005        dataTimeout = DurationUtils.zeroIfNull(timeout);
    3006    }
    3007
    3008    /**
    3009     * Sets the timeout in milliseconds to use when reading from the data connection. This timeout will be set immediately after opening the data connection,
    3010     * provided that the value is &ge; 0.
    3011     * <p>
    3012     * <strong>Note:</strong> the timeout will also be applied when calling accept() whilst establishing an active local data connection.
    3013     * </p>
    3014     *
    3015     * @param timeoutMillis The default timeout in milliseconds that is used when opening a data connection socket. The value 0 means an infinite timeout.
    3016     * @deprecated Use {@link #setDataTimeout(Duration)}.
    3017     */
    3018    @Deprecated
    3019    public void setDataTimeout(final int timeoutMillis) {
    3020        dataTimeout = Duration.ofMillis(timeoutMillis);
    3021    }
    3022
    3023    /**
    3024     * Sets the file structure. The default structure is {@link FTP#FILE_STRUCTURE} if this method is never called or if a connect method is called.
    3025     *
    3026     * @param fileStructure The structure of the file (one of the FTP class {@code _STRUCTURE} constants).
    3027     * @return True if successfully completed, false if not.
    3028     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    3029     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    3030     *                                      independently as itself.
    3031     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    3032     */
    3033    public boolean setFileStructure(final int fileStructure) throws IOException {
    3034        if (FTPReply.isPositiveCompletion(stru(fileStructure))) {
    3035            this.fileStructure = fileStructure;
    3036            return true;
    3037        }
    3038        return false;
    3039    }
    3040
    3041    /**
    3042     * Sets the transfer mode. The default transfer mode {@link FTP#STREAM_TRANSFER_MODE} if this method is never called or if a connect method is called.
    3043     *
    3044     * @param fileTransferMode The new transfer mode to use (one of the FTP class {@code _TRANSFER_MODE} constants).
    3045     * @return True if successfully completed, false if not.
    3046     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    3047     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    3048     *                                      independently as itself.
    3049     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    3050     */
    3051    public boolean setFileTransferMode(final int fileTransferMode) throws IOException {
    3052        if (FTPReply.isPositiveCompletion(mode(fileTransferMode))) {
    3053            this.fileTransferMode = fileTransferMode;
    3054            return true;
    3055        }
    3056        return false;
    3057    }
    3058
    3059    /**
    3060     * Sets the file type to be transferred. This should be one of {@link FTP#ASCII_FILE_TYPE}, {@link FTP#BINARY_FILE_TYPE}, etc. The file type only needs to
    3061     * 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 is
    3062     * {@link FTP#ASCII_FILE_TYPE} if this method is never called.
    3063     * <p>
    3064     * 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,
    3065     * always specify the appropriate file type after connecting to the server.</b>
    3066     * </p>
    3067     * <p>
    3068     * <strong>N.B.</strong> currently calling any connect method will reset the type to {@link FTP#ASCII_FILE_TYPE}.
    3069     * </p>
    3070     *
    3071     * @param fileType The {@code _FILE_TYPE} constant indicating the type of file.
    3072     * @return True if successfully completed, false if not.
    3073     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    3074     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    3075     *                                      independently as itself.
    3076     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    3077     */
    3078    public boolean setFileType(final int fileType) throws IOException {
    3079        if (FTPReply.isPositiveCompletion(type(fileType))) {
    3080            this.fileType = fileType;
    3081            this.formatOrByteSize = NON_PRINT_TEXT_FORMAT;
    3082            return true;
    3083        }
    3084        return false;
    3085    }
    3086
    3087    /**
    3088     * Sets the file type to be transferred and the format. The type should be one of {@link FTP#ASCII_FILE_TYPE}, {@link FTP#BINARY_FILE_TYPE}, etc. The file
    3089     * 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 default file
    3090     * type is {@link FTP#ASCII_FILE_TYPE} if this method is never called.
    3091     * <p>
    3092     * 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,
    3093     * always specify the appropriate file type after connecting to the server.</b>
    3094     * </p>
    3095     * <p>
    3096     * The format should be one of the FTP class {@code TEXT_FORMAT} constants, or if the type is {@link FTP#LOCAL_FILE_TYPE}, the format should be the byte
    3097     * size for that type. The default format is {@link FTP#NON_PRINT_TEXT_FORMAT} if this method is never called.
    3098     * </p>
    3099     * <p>
    3100     * <strong>N.B.</strong> currently calling any connect method will reset the type to {@link FTP#ASCII_FILE_TYPE} and the formatOrByteSize to
    3101     * {@link FTP#NON_PRINT_TEXT_FORMAT}.
    3102     * </p>
    3103     *
    3104     * @param fileType         The {@code _FILE_TYPE} constant indicating the type of file.
    3105     * @param formatOrByteSize The format of the file (one of the {@code _FORMAT} constants). In the case of {@code LOCAL_FILE_TYPE}, the byte size.
    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 other reason
    3108     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    3109     *                                      independently as itself.
    3110     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    3111     */
    3112    public boolean setFileType(final int fileType, final int formatOrByteSize) throws IOException {
    3113        if (FTPReply.isPositiveCompletion(type(fileType, formatOrByteSize))) {
    3114            this.fileType = fileType;
    3115            this.formatOrByteSize = formatOrByteSize;
    3116            return true;
    3117        }
    3118        return false;
    3119    }
    3120
    3121    /**
    3122     * 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
    3123     * will be silently ignored, and replaced with the remote IP address of the control connection, unless this configuration option is given, which restores
    3124     * the old behavior. To enable this by default, use the system property {@link FTPClient#FTP_IP_ADDRESS_FROM_PASV_RESPONSE}.
    3125     *
    3126     * @param ipAddressFromPasvResponse True, if the IP address from the server's response should be used (pre-3.9.0 compatible behavior), or false (ignore that
    3127     *                                  IP address).
    3128     * @see FTPClient#FTP_IP_ADDRESS_FROM_PASV_RESPONSE
    3129     * @see #isIpAddressFromPasvResponse
    3130     * @since 3.9.0
    3131     */
    3132    public void setIpAddressFromPasvResponse(final boolean ipAddressFromPasvResponse) {
    3133        this.ipAddressFromPasvResponse = ipAddressFromPasvResponse;
    3134    }
    3135
    3136    /**
    3137     * Sets whether to get hidden files when {@link #listFiles} too. A {@code LIST -a} will be issued to the ftp server. It
    3138     * 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".
    3139     *
    3140     * @param listHiddenFiles true if hidden files should be listed
    3141     * @since 2.0
    3142     */
    3143    public void setListHiddenFiles(final boolean listHiddenFiles) {
    3144        this.listHiddenFiles = listHiddenFiles;
    3145    }
    3146
    3147    /**
    3148     * Sets the last modified time of a file.
    3149     * <p>
    3150     * Issue the FTP MFMT command (not supported by all servers) which
    3151     * The timestamp should be in the form {@code yyyyMMDDhhmmss}. It should also be in GMT, but not all servers honor this.
    3152     * </p>
    3153     * <p>
    3154     * 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
    3155     * FTPClient.features()
    3156     * </p>
    3157     *
    3158     * @param path    The file path for which last modified time is to be changed.
    3159     * @param timeval The timestamp to set to, in {@code yyyyMMDDhhmmss} format.
    3160     * @return true if successfully set, false if not
    3161     * @throws IOException if an I/O error occurs.
    3162     * @since 2.2
    3163     * @see <a href="https://tools.ietf.org/html/draft-somers-ftp-mfxx-04">https://tools.ietf.org/html/draft-somers-ftp-mfxx-04</a>
    3164     */
    3165    public boolean setModificationTime(final String path, final String timeval) throws IOException {
    3166        return FTPReply.isPositiveCompletion(mfmt(path, timeval));
    3167    }
    3168
    3169    /**
    3170     * Sets the factory used for parser creation to the supplied factory object.
    3171     *
    3172     * @param parserFactory factory object used to create FTPFileEntryParsers
    3173     * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
    3174     * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
    3175     */
    3176    public void setParserFactory(final FTPFileEntryParserFactory parserFactory) {
    3177        this.parserFactory = parserFactory;
    3178    }
    3179
    3180    /**
    3181     * Sets the local IP address to use in passive mode. Useful when there are multiple network cards.
    3182     *
    3183     * @param passiveLocalHost The local IP address of this machine.
    3184     */
    3185    public void setPassiveLocalIPAddress(final InetAddress passiveLocalHost) {
    3186        this.passiveLocalHost = passiveLocalHost;
    3187    }
    3188
    3189    /**
    3190     * Sets the local IP address to use in passive mode. Useful when there are multiple network cards.
    3191     *
    3192     * @param ipAddress The local IP address of this machine.
    3193     * @throws UnknownHostException if the ipAddress cannot be resolved
    3194     */
    3195    public void setPassiveLocalIPAddress(final String ipAddress) throws UnknownHostException {
    3196        this.passiveLocalHost = InetAddress.getByName(ipAddress);
    3197    }
    3198
    3199    /**
    3200     * Sets the passive mode NAT workaround. If enabled, a site-local PASV mode reply address will be replaced with the remote host address to which
    3201     * 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.
    3202     * <p>
    3203     * The default is true, i.e. site-local replies are replaced.
    3204     * </p>
    3205     *
    3206     * @param enabled true to enable replacing internal IP's in passive mode.
    3207     * @deprecated (3.6) use {@link #setPassiveNatWorkaroundStrategy(HostnameResolver)} instead
    3208     */
    3209    @Deprecated
    3210    public void setPassiveNatWorkaround(final boolean enabled) {
    3211        this.passiveNatWorkaroundStrategy = enabled ? new NatServerResolverImpl(this) : null;
    3212    }
    3213
    3214    /**
    3215     * Sets the workaround strategy to replace the PASV mode reply addresses. This gets around the problem that some NAT boxes may change the reply.
    3216     *
    3217     * The default implementation is {@link NatServerResolverImpl}, i.e. site-local replies are replaced.
    3218     *
    3219     * @param passiveNatWorkaroundStrategy strategy to replace internal IP's in passive mode or null to disable the workaround (i.e. use PASV mode reply
    3220     *                                     address.)
    3221     * @since 3.6
    3222     */
    3223    public void setPassiveNatWorkaroundStrategy(final HostnameResolver passiveNatWorkaroundStrategy) {
    3224        this.passiveNatWorkaroundStrategy = passiveNatWorkaroundStrategy;
    3225    }
    3226
    3227    /**
    3228     * 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.
    3229     *
    3230     * @param receiveDataSocketBufferSize The size of the buffer, zero or negative means the value is ignored.
    3231     * @since 3.3
    3232     */
    3233    public void setReceieveDataSocketBufferSize(final int receiveDataSocketBufferSize) {
    3234        this.receiveDataSocketBufferSize = receiveDataSocketBufferSize;
    3235    }
    3236
    3237    /**
    3238     * 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.
    3239     * The default is for verification to be enabled. You may set this value at any time, whether the FTPClient is currently connected or not.
    3240     *
    3241     * @param remoteVerificationEnabled True to enable verification, false to disable verification.
    3242     */
    3243    public void setRemoteVerificationEnabled(final boolean remoteVerificationEnabled) {
    3244        this.remoteVerificationEnabled = remoteVerificationEnabled;
    3245    }
    3246
    3247    /**
    3248     * Sets the external IP address to report in EPRT/PORT commands in active mode. Useful when there are multiple network cards.
    3249     *
    3250     * @param ipAddress The external IP address of this machine.
    3251     * @throws UnknownHostException if the ipAddress cannot be resolved
    3252     * @since 3.1
    3253     * @see #getReportHostAddress()
    3254     */
    3255    public void setReportActiveExternalIPAddress(final String ipAddress) throws UnknownHostException {
    3256        this.reportActiveExternalHost = InetAddress.getByName(ipAddress);
    3257    }
    3258
    3259    /**
    3260     * Sets the restart offset for file transfers.
    3261     * <p>
    3262     * 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
    3263     * marker is reset to zero after use.
    3264     * </p>
    3265     * <p>
    3266     * <strong>Note: This method should only be invoked immediately prior to the transfer to which it applies.</strong>
    3267     * </p>
    3268     *
    3269     * @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.
    3270     */
    3271    public void setRestartOffset(final long offset) {
    3272        if (offset >= 0) {
    3273            restartOffset = offset;
    3274        }
    3275    }
    3276
    3277    /**
    3278     * 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.
    3279     *
    3280     * @param sendDataSocketBufferSize The size of the buffer, zero or negative means the value is ignored.
    3281     * @since 3.3
    3282     */
    3283    public void setSendDataSocketBufferSize(final int sendDataSocketBufferSize) {
    3284        this.sendDataSocketBufferSize = sendDataSocketBufferSize;
    3285    }
    3286
    3287    /**
    3288     * Sets whether to use EPSV with IPv4. Might be worth enabling in some circumstances.
    3289     *
    3290     * 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
    3291     * 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
    3292     * by taking just the port.
    3293     *
    3294     * @param useEPSVwithIPv4 value to set.
    3295     * @since 2.2
    3296     */
    3297    public void setUseEPSVwithIPv4(final boolean useEPSVwithIPv4) {
    3298        this.useEPSVwithIPv4 = useEPSVwithIPv4;
    3299    }
    3300
    3301    private boolean storeFile(final FTPCmd command, final String remote, final InputStream local) throws IOException {
    3302        return _storeFile(command.getCommand(), remote, local);
    3303    }
    3304
    3305    /**
    3306     * 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
    3307     * 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
    3308     * special InputStream to do this).
    3309     *
    3310     * @param remote The name to give the remote file.
    3311     * @param local  The local InputStream from which to read the file.
    3312     * @return True if successfully completed, false if not.
    3313     * @throws FTPConnectionClosedException                  If the FTP server prematurely closes the connection as a result of the client being idle or some
    3314     *                                                       other reason causing the server to send FTP reply code 421. This exception may be caught either as
    3315     *                                                       an IOException or independently as itself.
    3316     * @throws org.apache.commons.net.io.CopyStreamException If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to
    3317     *                                                       determine the number of bytes transferred and the IOException causing the error. This exception may
    3318     *                                                       be caught either as an IOException or independently as itself.
    3319     * @throws IOException                                   If an I/O error occurs while either sending a command to the server or receiving a reply from the
    3320     *                                                       server.
    3321     */
    3322    public boolean storeFile(final String remote, final InputStream local) throws IOException {
    3323        return storeFile(FTPCmd.STOR, remote, local);
    3324    }
    3325
    3326    private OutputStream storeFileStream(final FTPCmd command, final String remote) throws IOException {
    3327        return _storeFileStream(command.getCommand(), remote);
    3328    }
    3329
    3330    /**
    3331     * 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
    3332     * 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
    3333     * 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
    3334     * socket upon being closed.
    3335     * <p>
    3336     * <strong>To finalize the file transfer you must call {@link #completePendingCommand completePendingCommand} and check its return value to verify
    3337     * success.</strong> If this is not done, subsequent commands may behave unexpectedly.
    3338     * </p>
    3339     *
    3340     * @param remote The name to give the remote file.
    3341     * @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
    3342     *         returned (in which case you may check the reply code to determine the exact reason for failure).
    3343     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    3344     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    3345     *                                      independently as itself.
    3346     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    3347     */
    3348    public OutputStream storeFileStream(final String remote) throws IOException {
    3349        return storeFileStream(FTPCmd.STOR, remote);
    3350    }
    3351
    3352    /**
    3353     * 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
    3354     * 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
    3355     * attempt to create a special InputStream to do this).
    3356     *
    3357     * @param local The local InputStream from which to read the file.
    3358     * @return True if successfully completed, false if not.
    3359     * @throws FTPConnectionClosedException                  If the FTP server prematurely closes the connection as a result of the client being idle or some
    3360     *                                                       other reason causing the server to send FTP reply code 421. This exception may be caught either as
    3361     *                                                       an IOException or independently as itself.
    3362     * @throws org.apache.commons.net.io.CopyStreamException If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to
    3363     *                                                       determine the number of bytes transferred and the IOException causing the error. This exception may
    3364     *                                                       be caught either as an IOException or independently as itself.
    3365     * @throws IOException                                   If an I/O error occurs while either sending a command to the server or receiving a reply from the
    3366     *                                                       server.
    3367     */
    3368    public boolean storeUniqueFile(final InputStream local) throws IOException {
    3369        return storeFile(FTPCmd.STOU, null, local);
    3370    }
    3371
    3372    /**
    3373     * 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
    3374     * 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
    3375     * not attempt to create a special InputStream to do this).
    3376     *
    3377     * @param remote The name on which to base the unique name given to the remote file.
    3378     * @param local  The local InputStream from which to read the file.
    3379     * @return True if successfully completed, false if not.
    3380     * @throws FTPConnectionClosedException                  If the FTP server prematurely closes the connection as a result of the client being idle or some
    3381     *                                                       other reason causing the server to send FTP reply code 421. This exception may be caught either as
    3382     *                                                       an IOException or independently as itself.
    3383     * @throws org.apache.commons.net.io.CopyStreamException If an I/O error occurs while actually transferring the file. The CopyStreamException allows you to
    3384     *                                                       determine the number of bytes transferred and the IOException causing the error. This exception may
    3385     *                                                       be caught either as an IOException or independently as itself.
    3386     * @throws IOException                                   If an I/O error occurs while either sending a command to the server or receiving a reply from the
    3387     *                                                       server.
    3388     */
    3389    public boolean storeUniqueFile(final String remote, final InputStream local) throws IOException {
    3390        return storeFile(FTPCmd.STOU, remote, local);
    3391    }
    3392
    3393    /**
    3394     * 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
    3395     * 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
    3396     * 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
    3397     * parent data connection socket upon being closed.
    3398     * <p>
    3399     * <strong>To finalize the file transfer you must call {@link #completePendingCommand completePendingCommand} and check its return value to verify
    3400     * success.</strong> If this is not done, subsequent commands may behave unexpectedly.
    3401     * </p>
    3402     *
    3403     * @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
    3404     *         returned (in which case you may check the reply code to determine the exact reason for failure).
    3405     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    3406     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    3407     *                                      independently as itself.
    3408     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    3409     */
    3410    public OutputStream storeUniqueFileStream() throws IOException {
    3411        return storeFileStream(FTPCmd.STOU, null);
    3412    }
    3413
    3414    /**
    3415     * 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
    3416     * 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
    3417     * 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
    3418     * parent data connection socket upon being closed.
    3419     * <p>
    3420     * <strong>To finalize the file transfer you must call {@link #completePendingCommand completePendingCommand} and check its return value to verify
    3421     * success.</strong> If this is not done, subsequent commands may behave unexpectedly.
    3422     * </p>
    3423     *
    3424     * @param remote The name on which to base the unique name given to the remote file.
    3425     * @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
    3426     *         returned (in which case you may check the reply code to determine the exact reason for failure).
    3427     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    3428     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    3429     *                                      independently as itself.
    3430     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    3431     */
    3432    public OutputStream storeUniqueFileStream(final String remote) throws IOException {
    3433        return storeFileStream(FTPCmd.STOU, remote);
    3434    }
    3435
    3436    /**
    3437     * Issue the FTP SMNT command.
    3438     *
    3439     * @param path The path to mount.
    3440     * @return True if successfully completed, false if not.
    3441     * @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
    3442     *                                      causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
    3443     *                                      independently as itself.
    3444     * @throws IOException                  If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
    3445     */
    3446    public boolean structureMount(final String path) throws IOException {
    3447        return FTPReply.isPositiveCompletion(smnt(path));
    3448    }
    3449
    3450    private Socket wrapOnDeflate(final Socket plainSocket) {
    3451        switch (fileTransferMode) {
    3452        case DEFLATE_TRANSFER_MODE:
    3453            return new DeflateSocket(plainSocket);
    3454        // Experiment, not in an RFC?
    3455        // case GZIP_TRANSFER_MODE:
    3456        // return new GZIPSocket(plainSocket);
    3457        default:
    3458            return plainSocket;
    3459        }
    3460    }
    3461}