Path: blob/master/src/java.base/share/classes/sun/net/ftp/FtpClient.java
41159 views
/*1* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.net.ftp;2526import java.net.*;27import java.io.*;28import java.util.Date;29import java.util.List;30import java.util.Iterator;3132/**33* A class that implements the FTP protocol according to34* RFCs <A href="http://www.ietf.org/rfc/rfc0959.txt">959</A>,35* <A href="http://www.ietf.org/rfc/rfc2228.txt">2228</A>,36* <A href="http://www.ietf.org/rfc/rfc2389.txt">2389</A>,37* <A href="http://www.ietf.org/rfc/rfc2428.txt">2428</A>,38* <A href="http://www.ietf.org/rfc/rfc3659.txt">3659</A>,39* <A href="http://www.ietf.org/rfc/rfc4217.txt">4217</A>.40* Which includes support for FTP over SSL/TLS (aka ftps).41*42* {@code FtpClient} provides all the functionalities of a typical FTP43* client, like storing or retrieving files, listing or creating directories.44* A typical usage would consist of connecting the client to the server,45* log in, issue a few commands then logout.46* Here is a code example:47* <pre>48* FtpClient cl = FtpClient.create();49* cl.connect("ftp.gnu.org").login("anonymous", "[email protected]".toCharArray())).changeDirectory("pub/gnu");50* Iterator<FtpDirEntry> dir = cl.listFiles();51* while (dir.hasNext()) {52* FtpDirEntry f = dir.next();53* System.err.println(f.getName());54* }55* cl.close();56* }57* </pre>58* <p><b>Error reporting:</b> There are, mostly, two families of errors that59* can occur during an FTP session. The first kind are the network related issues60* like a connection reset, and they are usually fatal to the session, meaning,61* in all likelyhood the connection to the server has been lost and the session62* should be restarted from scratch. These errors are reported by throwing an63* {@link IOException}. The second kind are the errors reported by the FTP server,64* like when trying to download a non-existing file for example. These errors65* are usually non fatal to the session, meaning more commands can be sent to the66* server. In these cases, a {@link FtpProtocolException} is thrown.</p>67* <p>68* It should be noted that this is not a thread-safe API, as it wouldn't make69* too much sense, due to the very sequential nature of FTP, to provide a70* client able to be manipulated from multiple threads.71*72* @since 1.773*/74public abstract class FtpClient implements java.io.Closeable {7576private static final int FTP_PORT = 21;7778public static enum TransferType {7980ASCII, BINARY, EBCDIC81};8283/**84* Returns the default FTP port number.85*86* @return the port number.87*/88public static final int defaultPort() {89return FTP_PORT;90}9192/**93* Creates an instance of FtpClient. The client is not connected to any94* server yet.95*96*/97protected FtpClient() {98}99100/**101* Creates an instance of {@code FtpClient}. The client is not connected to any102* server yet.103*104* @return the created {@code FtpClient}105*/106public static FtpClient create() {107FtpClientProvider provider = FtpClientProvider.provider();108return provider.createFtpClient();109}110111/**112* Creates an instance of FtpClient and connects it to the specified113* address.114*115* @param dest the {@code InetSocketAddress} to connect to.116* @return The created {@code FtpClient}117* @throws IOException if the connection fails118* @see #connect(java.net.SocketAddress)119*/120public static FtpClient create(InetSocketAddress dest) throws FtpProtocolException, IOException {121FtpClient client = create();122if (dest != null) {123client.connect(dest);124}125return client;126}127128/**129* Creates an instance of {@code FtpClient} and connects it to the130* specified host on the default FTP port.131*132* @param dest the {@code String} containing the name of the host133* to connect to.134* @return The created {@code FtpClient}135* @throws IOException if the connection fails.136* @throws FtpProtocolException if the server rejected the connection137*/138public static FtpClient create(String dest) throws FtpProtocolException, IOException {139return create(new InetSocketAddress(dest, FTP_PORT));140}141142/**143* Enables, or disables, the use of the <I>passive</I> mode. In that mode,144* data connections are established by having the client connect to the server.145* This is the recommended default mode as it will work best through146* firewalls and NATs. If set to {@code false} the mode is said to be147* <I>active</I> which means the server will connect back to the client148* after a PORT command to establish a data connection.149*150* <p><b>Note:</b> Since the passive mode might not be supported by all151* FTP servers, enabling it means the client will try to use it. If the152* server rejects it, then the client will attempt to fall back to using153* the <I>active</I> mode by issuing a {@code PORT} command instead.</p>154*155* @param passive {@code true} to force passive mode.156* @return This FtpClient157* @see #isPassiveModeEnabled()158*/159public abstract FtpClient enablePassiveMode(boolean passive);160161/**162* Tests whether passive mode is enabled.163*164* @return {@code true} if the passive mode has been enabled.165* @see #enablePassiveMode(boolean)166*/167public abstract boolean isPassiveModeEnabled();168169/**170* Sets the default timeout value to use when connecting to the server,171*172* @param timeout the timeout value, in milliseconds, to use for the connect173* operation. A value of zero or less, means use the default timeout.174*175* @return This FtpClient176*/177public abstract FtpClient setConnectTimeout(int timeout);178179/**180* Returns the current default connection timeout value.181*182* @return the value, in milliseconds, of the current connect timeout.183* @see #setConnectTimeout(int)184*/185public abstract int getConnectTimeout();186187/**188* Sets the timeout value to use when reading from the server,189*190* @param timeout the timeout value, in milliseconds, to use for the read191* operation. A value of zero or less, means use the default timeout.192* @return This FtpClient193*/194public abstract FtpClient setReadTimeout(int timeout);195196/**197* Returns the current read timeout value.198*199* @return the value, in milliseconds, of the current read timeout.200* @see #setReadTimeout(int)201*/202public abstract int getReadTimeout();203204/**205* Set the {@code Proxy} to be used for the next connection.206* If the client is already connected, it doesn't affect the current207* connection. However it is not recommended to change this during a session.208*209* @param p the {@code Proxy} to use, or {@code null} for no proxy.210* @return This FtpClient211*/212public abstract FtpClient setProxy(Proxy p);213214/**215* Get the proxy of this FtpClient216*217* @return the {@code Proxy}, this client is using, or {@code null}218* if none is used.219* @see #setProxy(Proxy)220*/221public abstract Proxy getProxy();222223/**224* Tests whether this client is connected or not to a server.225*226* @return {@code true} if the client is connected.227*/228public abstract boolean isConnected();229230/**231* Connects the {@code FtpClient} to the specified destination server.232*233* @param dest the address of the destination server234* @return this FtpClient235* @throws IOException if connection failed.236* @throws SecurityException if there is a SecurityManager installed and it237* denied the authorization to connect to the destination.238* @throws FtpProtocolException239*/240public abstract FtpClient connect(SocketAddress dest) throws FtpProtocolException, IOException;241242/**243* Connects the FtpClient to the specified destination server.244*245* @param dest the address of the destination server246* @param timeout the value, in milliseconds, to use as a connection timeout247* @return this FtpClient248* @throws IOException if connection failed.249* @throws SecurityException if there is a SecurityManager installed and it250* denied the authorization to connect to the destination.251* @throws FtpProtocolException252*/253public abstract FtpClient connect(SocketAddress dest, int timeout) throws FtpProtocolException, IOException;254255/**256* Retrieves the address of the FTP server this client is connected to.257*258* @return the {@link SocketAddress} of the server, or {@code null} if this259* client is not connected yet.260*/261public abstract SocketAddress getServerAddress();262263/**264* Attempts to log on the server with the specified user name and password.265*266* @param user The user name267* @param password The password for that user268* @return this FtpClient269* @throws IOException if an error occurred during the transmission270* @throws FtpProtocolException if the login was refused by the server271*/272public abstract FtpClient login(String user, char[] password) throws FtpProtocolException, IOException;273274/**275* Attempts to log on the server with the specified user name, password and276* account name.277*278* @param user The user name279* @param password The password for that user.280* @param account The account name for that user.281* @return this FtpClient282* @throws IOException if an error occurs during the transmission.283* @throws FtpProtocolException if the login was refused by the server284*/285public abstract FtpClient login(String user, char[] password, String account) throws FtpProtocolException, IOException;286287/**288* Closes the current connection. Logs out the current user, if any, by289* issuing the QUIT command to the server.290* This is in effect terminates the current291* session and the connection to the server will be closed.292* <p>After a close, the client can then be connected to another server293* to start an entirely different session.</P>294*295* @throws IOException if an error occurs during transmission296*/297public abstract void close() throws IOException;298299/**300* Checks whether the client is logged in to the server or not.301*302* @return {@code true} if the client has already completed a login.303*/304public abstract boolean isLoggedIn();305306/**307* Changes to a specific directory on a remote FTP server308*309* @param remoteDirectory path of the directory to CD to.310* @return this FtpClient311* @throws IOException if an error occurs during the transmission.312* @throws FtpProtocolException if the command was refused by the server313*/314public abstract FtpClient changeDirectory(String remoteDirectory) throws FtpProtocolException, IOException;315316/**317* Changes to the parent directory, sending the CDUP command to the server.318*319* @return this FtpClient320* @throws IOException if an error occurs during the transmission.321* @throws FtpProtocolException if the command was refused by the server322*/323public abstract FtpClient changeToParentDirectory() throws FtpProtocolException, IOException;324325/**326* Retrieve the server current working directory using the PWD command.327*328* @return a {@code String} containing the current working directory329* @throws IOException if an error occurs during transmission330* @throws FtpProtocolException if the command was refused by the server,331*/332public abstract String getWorkingDirectory() throws FtpProtocolException, IOException;333334/**335* Sets the restart offset to the specified value. That value will be336* sent through a {@code REST} command to server before the next file337* transfer and has the effect of resuming a file transfer from the338* specified point. After the transfer the restart offset is set back to339* zero.340*341* @param offset the offset in the remote file at which to start the next342* transfer. This must be a value greater than or equal to zero.343* @return this FtpClient344* @throws IllegalArgumentException if the offset is negative.345*/346public abstract FtpClient setRestartOffset(long offset);347348/**349* Retrieves a file from the ftp server and writes its content to the specified350* {@code OutputStream}.351* <p>If the restart offset was set, then a {@code REST} command will be352* sent before the {@code RETR} in order to restart the tranfer from the specified353* offset.</p>354* <p>The {@code OutputStream} is not closed by this method at the end355* of the transfer. </p>356* <p>This method will block until the transfer is complete or an exception357* is thrown.</p>358*359* @param name a {@code String} containing the name of the file to360* retreive from the server.361* @param local the {@code OutputStream} the file should be written to.362* @return this FtpClient363* @throws IOException if the transfer fails.364* @throws FtpProtocolException if the command was refused by the server365* @see #setRestartOffset(long)366*/367public abstract FtpClient getFile(String name, OutputStream local) throws FtpProtocolException, IOException;368369/**370* Retrieves a file from the ftp server, using the {@code RETR} command, and371* returns the InputStream from the established data connection.372* {@link #completePending()} <b>has</b> to be called once the application373* is done reading from the returned stream.374* <p>If the restart offset was set, then a {@code REST} command will be375* sent before the {@code RETR} in order to restart the tranfer from the specified376* offset.</p>377*378* @param name the name of the remote file379* @return the {@link java.io.InputStream} from the data connection380* @throws IOException if an error occurred during the transmission.381* @throws FtpProtocolException if the command was refused by the server382* @see #setRestartOffset(long)383*/384public abstract InputStream getFileStream(String name) throws FtpProtocolException, IOException;385386/**387* Transfers a file from the client to the server (aka a <I>put</I>)388* by sending the STOR command, and returns the {@code OutputStream}389* from the established data connection.390*391* A new file is created at the server site if the file specified does392* not already exist.393*394* {@link #completePending()} <b>has</b> to be called once the application395* is finished writing to the returned stream.396*397* @param name the name of the remote file to write.398* @return the {@link java.io.OutputStream} from the data connection or399* {@code null} if the command was unsuccessful.400* @throws IOException if an error occurred during the transmission.401* @throws FtpProtocolException if the command was rejected by the server402*/403public OutputStream putFileStream(String name) throws FtpProtocolException, IOException {404return putFileStream(name, false);405}406407/**408* Transfers a file from the client to the server (aka a <I>put</I>)409* by sending the STOR or STOU command, depending on the410* {@code unique} argument, and returns the {@code OutputStream}411* from the established data connection.412* {@link #completePending()} <b>has</b> to be called once the application413* is finished writing to the stream.414*415* A new file is created at the server site if the file specified does416* not already exist.417*418* If {@code unique} is set to {@code true}, the resultant file419* is to be created under a name unique to that directory, meaning420* it will not overwrite an existing file, instead the server will421* generate a new, unique, file name.422* The name of the remote file can be retrieved, after completion of the423* transfer, by calling {@link #getLastFileName()}.424*425* @param name the name of the remote file to write.426* @param unique {@code true} if the remote files should be unique,427* in which case the STOU command will be used.428* @return the {@link java.io.OutputStream} from the data connection.429* @throws IOException if an error occurred during the transmission.430* @throws FtpProtocolException if the command was rejected by the server431*/432public abstract OutputStream putFileStream(String name, boolean unique) throws FtpProtocolException, IOException;433434/**435* Transfers a file from the client to the server (aka a <I>put</I>)436* by sending the STOR or STOU command, depending on the437* {@code unique} argument. The content of the {@code InputStream}438* passed in argument is written into the remote file, overwriting any439* existing data.440*441* A new file is created at the server site if the file specified does442* not already exist.443*444* If {@code unique} is set to {@code true}, the resultant file445* is to be created under a name unique to that directory, meaning446* it will not overwrite an existing file, instead the server will447* generate a new, unique, file name.448* The name of the remote file can be retrieved, after completion of the449* transfer, by calling {@link #getLastFileName()}.450*451* <p>This method will block until the transfer is complete or an exception452* is thrown.</p>453*454* @param name the name of the remote file to write.455* @param local the {@code InputStream} that points to the data to456* transfer.457* @return this FtpClient458* @throws IOException if an error occurred during the transmission.459* @throws FtpProtocolException if the command was rejected by the server460*/461public FtpClient putFile(String name, InputStream local) throws FtpProtocolException, IOException {462return putFile(name, local, false);463}464465/**466* Transfers a file from the client to the server (aka a <I>put</I>)467* by sending the STOR command. The content of the {@code InputStream}468* passed in argument is written into the remote file, overwriting any469* existing data.470*471* A new file is created at the server site if the file specified does472* not already exist.473*474* <p>This method will block until the transfer is complete or an exception475* is thrown.</p>476*477* @param name the name of the remote file to write.478* @param local the {@code InputStream} that points to the data to479* transfer.480* @param unique {@code true} if the remote file should be unique481* (i.e. not already existing), {@code false} otherwise.482* @return this FtpClient483* @throws IOException if an error occurred during the transmission.484* @throws FtpProtocolException if the command was rejected by the server485* @see #getLastFileName()486*/487public abstract FtpClient putFile(String name, InputStream local, boolean unique) throws FtpProtocolException, IOException;488489/**490* Sends the APPE command to the server in order to transfer a data stream491* passed in argument and append it to the content of the specified remote492* file.493*494* <p>This method will block until the transfer is complete or an exception495* is thrown.</p>496*497* @param name A {@code String} containing the name of the remote file498* to append to.499* @param local The {@code InputStream} providing access to the data500* to be appended.501* @return this FtpClient502* @throws IOException if an error occurred during the transmission.503* @throws FtpProtocolException if the command was rejected by the server504*/505public abstract FtpClient appendFile(String name, InputStream local) throws FtpProtocolException, IOException;506507/**508* Renames a file on the server.509*510* @param from the name of the file being renamed511* @param to the new name for the file512* @return this FtpClient513* @throws IOException if an error occurred during the transmission.514* @throws FtpProtocolException if the command was rejected by the server515*/516public abstract FtpClient rename(String from, String to) throws FtpProtocolException, IOException;517518/**519* Deletes a file on the server.520*521* @param name a {@code String} containing the name of the file522* to delete.523* @return this FtpClient524* @throws IOException if an error occurred during the exchange525* @throws FtpProtocolException if the command was rejected by the server526*/527public abstract FtpClient deleteFile(String name) throws FtpProtocolException, IOException;528529/**530* Creates a new directory on the server.531*532* @param name a {@code String} containing the name of the directory533* to create.534* @return this FtpClient535* @throws IOException if an error occurred during the exchange536* @throws FtpProtocolException if the command was rejected by the server537*/538public abstract FtpClient makeDirectory(String name) throws FtpProtocolException, IOException;539540/**541* Removes a directory on the server.542*543* @param name a {@code String} containing the name of the directory544* to remove.545*546* @return this FtpClient547* @throws IOException if an error occurred during the exchange.548* @throws FtpProtocolException if the command was rejected by the server549*/550public abstract FtpClient removeDirectory(String name) throws FtpProtocolException, IOException;551552/**553* Sends a No-operation command. It's useful for testing the connection554* status or as a <I>keep alive</I> mechanism.555*556* @return this FtpClient557* @throws IOException if an error occurred during the transmission.558* @throws FtpProtocolException if the command was rejected by the server559*/560public abstract FtpClient noop() throws FtpProtocolException, IOException;561562/**563* Sends the {@code STAT} command to the server.564* This can be used while a data connection is open to get a status565* on the current transfer, in that case the parameter should be566* {@code null}.567* If used between file transfers, it may have a pathname as argument568* in which case it will work as the LIST command except no data569* connection will be created.570*571* @param name an optional {@code String} containing the pathname572* the STAT command should apply to.573* @return the response from the server574* @throws IOException if an error occurred during the transmission.575* @throws FtpProtocolException if the command was rejected by the server576*/577public abstract String getStatus(String name) throws FtpProtocolException, IOException;578579/**580* Sends the {@code FEAT} command to the server and returns the list of supported581* features in the form of strings.582*583* The features are the supported commands, like AUTH TLS, PROT or PASV.584* See the RFCs for a complete list.585*586* Note that not all FTP servers support that command, in which case587* a {@link FtpProtocolException} will be thrown.588*589* @return a {@code List} of {@code Strings} describing the590* supported additional features591* @throws IOException if an error occurs during the transmission.592* @throws FtpProtocolException if the command is rejected by the server593*/594public abstract List<String> getFeatures() throws FtpProtocolException, IOException;595596/**597* Sends the {@code ABOR} command to the server.598* <p>It tells the server to stop the previous command or transfer. No action599* will be taken if the previous command has already been completed.</p>600* <p>This doesn't abort the current session, more commands can be issued601* after an abort.</p>602*603* @return this FtpClient604* @throws IOException if an error occurred during the transmission.605* @throws FtpProtocolException if the command was rejected by the server606*/607public abstract FtpClient abort() throws FtpProtocolException, IOException;608609/**610* Some methods do not wait until completion before returning, so this611* method can be called to wait until completion. This is typically the case612* with commands that trigger a transfer like {@link #getFileStream(String)}.613* So this method should be called before accessing information related to614* such a command.615* <p>This method will actually block reading on the command channel for a616* notification from the server that the command is finished. Such a617* notification often carries extra information concerning the completion618* of the pending action (e.g. number of bytes transfered).</p>619* <p>Note that this will return immediately if no command or action620* is pending</p>621* <p>It should be also noted that most methods issuing commands to the ftp622* server will call this method if a previous command is pending.623* <p>Example of use:624* <pre>625* InputStream in = cl.getFileStream("file");626* ...627* cl.completePending();628* long size = cl.getLastTransferSize();629* </pre>630* On the other hand, it's not necessary in a case like:631* <pre>632* InputStream in = cl.getFileStream("file");633* // read content634* ...635* cl.close();636* </pre>637* <p>Since {@link #close()} will call completePending() if necessary.</p>638* @return this FtpClient639* @throws IOException if an error occurred during the transfer640* @throws FtpProtocolException if the command didn't complete successfully641*/642public abstract FtpClient completePending() throws FtpProtocolException, IOException;643644/**645* Reinitializes the USER parameters on the FTP server646*647* @return this FtpClient648* @throws IOException if an error occurs during transmission649* @throws FtpProtocolException if the command fails650*/651public abstract FtpClient reInit() throws FtpProtocolException, IOException;652653/**654* Changes the transfer type (binary, ascii, ebcdic) and issue the655* proper command (e.g. TYPE A) to the server.656*657* @param type the {@code TransferType} to use.658* @return This FtpClient659* @throws IOException if an error occurs during transmission.660* @throws FtpProtocolException if the command was rejected by the server661*/662public abstract FtpClient setType(TransferType type) throws FtpProtocolException, IOException;663664/**665* Changes the current transfer type to binary.666* This is a convenience method that is equivalent to667* {@code setType(TransferType.BINARY)}668*669* @return This FtpClient670* @throws IOException if an error occurs during the transmission.671* @throws FtpProtocolException if the command was rejected by the server672* @see #setType(TransferType)673*/674public FtpClient setBinaryType() throws FtpProtocolException, IOException {675setType(TransferType.BINARY);676return this;677}678679/**680* Changes the current transfer type to ascii.681* This is a convenience method that is equivalent to682* {@code setType(TransferType.ASCII)}683*684* @return This FtpClient685* @throws IOException if an error occurs during the transmission.686* @throws FtpProtocolException if the command was rejected by the server687* @see #setType(TransferType)688*/689public FtpClient setAsciiType() throws FtpProtocolException, IOException {690setType(TransferType.ASCII);691return this;692}693694/**695* Issues a {@code LIST} command to the server to get the current directory696* listing, and returns the InputStream from the data connection.697*698* <p>{@link #completePending()} <b>has</b> to be called once the application699* is finished reading from the stream.</p>700*701* @param path the pathname of the directory to list, or {@code null}702* for the current working directory.703* @return the {@code InputStream} from the resulting data connection704* @throws IOException if an error occurs during the transmission.705* @throws FtpProtocolException if the command was rejected by the server706* @see #changeDirectory(String)707* @see #listFiles(String)708*/709public abstract InputStream list(String path) throws FtpProtocolException, IOException;710711/**712* Issues a {@code NLST path} command to server to get the specified directory713* content. It differs from {@link #list(String)} method by the fact that714* it will only list the file names which would make the parsing of the715* somewhat easier.716*717* <p>{@link #completePending()} <b>has</b> to be called once the application718* is finished reading from the stream.</p>719*720* @param path a {@code String} containing the pathname of the721* directory to list or {@code null} for the current working directory.722* @return the {@code InputStream} from the resulting data connection723* @throws IOException if an error occurs during the transmission.724* @throws FtpProtocolException if the command was rejected by the server725*/726public abstract InputStream nameList(String path) throws FtpProtocolException, IOException;727728/**729* Issues the {@code SIZE [path]} command to the server to get the size of a730* specific file on the server.731* Note that this command may not be supported by the server. In which732* case -1 will be returned.733*734* @param path a {@code String} containing the pathname of the735* file.736* @return a {@code long} containing the size of the file or -1 if737* the server returned an error, which can be checked with738* {@link #getLastReplyCode()}.739* @throws IOException if an error occurs during the transmission.740* @throws FtpProtocolException if the command was rejected by the server741*/742public abstract long getSize(String path) throws FtpProtocolException, IOException;743744/**745* Issues the {@code MDTM [path]} command to the server to get the modification746* time of a specific file on the server.747* Note that this command may not be supported by the server, in which748* case {@code null} will be returned.749*750* @param path a {@code String} containing the pathname of the file.751* @return a {@code Date} representing the last modification time752* or {@code null} if the server returned an error, which753* can be checked with {@link #getLastReplyCode()}.754* @throws IOException if an error occurs during the transmission.755* @throws FtpProtocolException if the command was rejected by the server756*/757public abstract Date getLastModified(String path) throws FtpProtocolException, IOException;758759/**760* Sets the parser used to handle the directory output to the specified761* one. By default the parser is set to one that can handle most FTP762* servers output (Unix base mostly). However it may be necessary for763* and application to provide its own parser due to some uncommon764* output format.765*766* @param p The {@code FtpDirParser} to use.767* @return this FtpClient768* @see #listFiles(String)769*/770public abstract FtpClient setDirParser(FtpDirParser p);771772/**773* Issues a {@code MLSD} command to the server to get the specified directory774* listing and applies the internal parser to create an Iterator of775* {@link java.net.FtpDirEntry}. Note that the Iterator returned is also a776* {@link java.io.Closeable}.777* <p>If the server doesn't support the MLSD command, the LIST command is used778* instead and the parser set by {@link #setDirParser(java.net.FtpDirParser) }779* is used instead.</p>780*781* {@link #completePending()} <b>has</b> to be called once the application782* is finished iterating through the files.783*784* @param path the pathname of the directory to list or {@code null}785* for the current working directoty.786* @return an {@code Iterator} of files or {@code null} if the787* command failed.788* @throws IOException if an error occurred during the transmission789* @see #setDirParser(FtpDirParser)790* @see #changeDirectory(String)791* @throws FtpProtocolException if the command was rejected by the server792*/793public abstract Iterator<FtpDirEntry> listFiles(String path) throws FtpProtocolException, IOException;794795/**796* Attempts to use Kerberos GSSAPI as an authentication mechanism with the797* ftp server. This will issue an {@code AUTH GSSAPI} command, and if798* it is accepted by the server, will followup with {@code ADAT}799* command to exchange the various tokens until authentication is800* successful. This conforms to Appendix I of RFC 2228.801*802* @return this FtpClient803* @throws IOException if an error occurs during the transmission.804* @throws FtpProtocolException if the command was rejected by the server805*/806public abstract FtpClient useKerberos() throws FtpProtocolException, IOException;807808/**809* Returns the Welcome string the server sent during initial connection.810*811* @return a {@code String} containing the message the server812* returned during connection or {@code null}.813*/814public abstract String getWelcomeMsg();815816/**817* Returns the last reply code sent by the server.818*819* @return the lastReplyCode or {@code null} if none were received yet.820*/821public abstract FtpReplyCode getLastReplyCode();822823/**824* Returns the last response string sent by the server.825*826* @return the message string, which can be quite long, last returned827* by the server, or {@code null} if no response were received yet.828*/829public abstract String getLastResponseString();830831/**832* Returns, when available, the size of the latest started transfer.833* This is retreived by parsing the response string received as an initial834* response to a {@code RETR} or similar request.835*836* @return the size of the latest transfer or -1 if either there was no837* transfer or the information was unavailable.838*/839public abstract long getLastTransferSize();840841/**842* Returns, when available, the remote name of the last transfered file.843* This is mainly useful for "put" operation when the unique flag was844* set since it allows to recover the unique file name created on the845* server which may be different from the one submitted with the command.846*847* @return the name the latest transfered file remote name, or848* {@code null} if that information is unavailable.849*/850public abstract String getLastFileName();851852/**853* Attempts to switch to a secure, encrypted connection. This is done by854* sending the {@code AUTH TLS} command.855* <p>See <a href="http://www.ietf.org/rfc/rfc4217.txt">RFC 4217</a></p>856* If successful this will establish a secure command channel with the857* server, it will also make it so that all other transfers (e.g. a RETR858* command) will be done over an encrypted channel as well unless a859* {@link #reInit()} command or a {@link #endSecureSession()} command is issued.860* <p>This method should be called after a successful {@link #connect(java.net.InetSocketAddress) }861* but before calling {@link #login(java.lang.String, char[]) }.</p>862*863* @return this FtpCLient864* @throws IOException if an error occurred during the transmission.865* @throws FtpProtocolException if the command was rejected by the server866* @see #endSecureSession()867*/868public abstract FtpClient startSecureSession() throws FtpProtocolException, IOException;869870/**871* Sends a {@code CCC} command followed by a {@code PROT C}872* command to the server terminating an encrypted session and reverting873* back to a non encrypted transmission.874*875* @return this FtpClient876* @throws IOException if an error occurred during transmission.877* @throws FtpProtocolException if the command was rejected by the server878* @see #startSecureSession()879*/880public abstract FtpClient endSecureSession() throws FtpProtocolException, IOException;881882/**883* Sends the "Allocate" ({@code ALLO}) command to the server telling it to884* pre-allocate the specified number of bytes for the next transfer.885*886* @param size The number of bytes to allocate.887* @return this FtpClient888* @throws IOException if an error occurred during the transmission.889* @throws FtpProtocolException if the command was rejected by the server890*/891public abstract FtpClient allocate(long size) throws FtpProtocolException, IOException;892893/**894* Sends the "Structure Mount" ({@code SMNT}) command to the server. This let the895* user mount a different file system data structure without altering his896* login or accounting information.897*898* @param struct a {@code String} containing the name of the899* structure to mount.900* @return this FtpClient901* @throws IOException if an error occurred during the transmission.902* @throws FtpProtocolException if the command was rejected by the server903*/904public abstract FtpClient structureMount(String struct) throws FtpProtocolException, IOException;905906/**907* Sends a System ({@code SYST}) command to the server and returns the String908* sent back by the server describing the operating system at the909* server.910*911* @return a {@code String} describing the OS, or {@code null}912* if the operation was not successful.913* @throws IOException if an error occurred during the transmission.914* @throws FtpProtocolException if the command was rejected by the server915*/916public abstract String getSystem() throws FtpProtocolException, IOException;917918/**919* Sends the {@code HELP} command to the server, with an optional command, like920* SITE, and returns the text sent back by the server.921*922* @param cmd the command for which the help is requested or923* {@code null} for the general help924* @return a {@code String} containing the text sent back by the925* server, or {@code null} if the command failed.926* @throws IOException if an error occurred during transmission927* @throws FtpProtocolException if the command was rejected by the server928*/929public abstract String getHelp(String cmd) throws FtpProtocolException, IOException;930931/**932* Sends the {@code SITE} command to the server. This is used by the server933* to provide services specific to his system that are essential934* to file transfer.935*936* @param cmd the command to be sent.937* @return this FtpClient938* @throws IOException if an error occurred during transmission939* @throws FtpProtocolException if the command was rejected by the server940*/941public abstract FtpClient siteCmd(String cmd) throws FtpProtocolException, IOException;942}943944945