KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > net > ftp > FTP


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

16 package org.apache.commons.net.ftp;
17 import java.io.BufferedReader JavaDoc;
18 import java.io.BufferedWriter JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.net.Socket JavaDoc;
26 import java.net.SocketException JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import org.apache.commons.net.MalformedServerReplyException;
31 import org.apache.commons.net.ProtocolCommandListener;
32 import org.apache.commons.net.ProtocolCommandSupport;
33 import org.apache.commons.net.SocketClient;
34 import org.apache.commons.net.telnet.TelnetClient;
35
36 /***
37  * FTP provides the basic the functionality necessary to implement your
38  * own FTP client. It extends org.apache.commons.net.TelnetClient
39  * simply because it saves the writing of extra code to handle the FTP
40  * control connection which always remains open during an FTP session and
41  * uses the Telnet protocol. Aggregation would require writing new
42  * wrapper methods and wouldn't leverage the functionality already
43  * present in org.apache.commons.net.SocketClient.
44  * <p>
45  * To derive the full benefits of the FTP class requires some knowledge
46  * of the FTP protocol defined in RFC 959. However, there is no reason
47  * why you should have to use the FTP class. The
48  * {@link org.apache.commons.net.ftp.FTPClient} class,
49  * derived from FTP,
50  * implements all the functionality required of an FTP client. The
51  * FTP class is made public to provide access to various FTP constants
52  * and to make it easier for adventurous programmers (or those with
53  * special needs) to interact with the FTP protocol and implement their
54  * own clients. A set of methods with names corresponding to the FTP
55  * command names are provided to facilitate this interaction.
56  * <p>
57  * You should keep in mind that the FTP server may choose to prematurely
58  * close a connection if the client has been idle for longer than a
59  * given time period (usually 900 seconds). The FTP class will detect a
60  * premature FTP server connection closing when it receives a
61  * {@link org.apache.commons.net.ftp.FTPReply#SERVICE_NOT_AVAILABLE FTPReply.SERVICE_NOT_AVAILABLE }
62  * response to a command.
63  * When that occurs, the FTP class method encountering that reply will throw
64  * an {@link org.apache.commons.net.ftp.FTPConnectionClosedException}
65  * . <code>FTPConectionClosedException</code>
66  * is a subclass of <code> IOException </code> and therefore need not be
67  * caught separately, but if you are going to catch it separately, its
68  * catch block must appear before the more general <code> IOException </code>
69  * catch block. When you encounter an
70  * {@link org.apache.commons.net.ftp.FTPConnectionClosedException}
71  * , you must disconnect the connection with
72  * {@link #disconnect disconnect() } to properly clean up the
73  * system resources used by FTP. Before disconnecting, you may check the
74  * last reply code and text with
75  * {@link #getReplyCode getReplyCode },
76  * {@link #getReplyString getReplyString },
77  * and {@link #getReplyStrings getReplyStrings}.
78  * You may avoid server disconnections while the client is idle by
79  * periodicaly sending NOOP commands to the server.
80  * <p>
81  * Rather than list it separately for each method, we mention here that
82  * every method communicating with the server and throwing an IOException
83  * can also throw a
84  * {@link org.apache.commons.net.MalformedServerReplyException}
85  * , which is a subclass
86  * of IOException. A MalformedServerReplyException will be thrown when
87  * the reply received from the server deviates enough from the protocol
88  * specification that it cannot be interpreted in a useful manner despite
89  * attempts to be as lenient as possible.
90  * <p>
91  * <p>
92  * @author Daniel F. Savarese
93  * @see FTPClient
94  * @see FTPConnectionClosedException
95  * @see org.apache.commons.net.MalformedServerReplyException
96  ***/

97
98 public class FTP extends TelnetClient
99 {
100     /*** The default FTP data port (20). ***/
101     public static final int DEFAULT_DATA_PORT = 20;
102     /*** The default FTP control port (21). ***/
103     public static final int DEFAULT_PORT = 21;
104
105     /***
106      * A constant used to indicate the file(s) being transfered should
107      * be treated as ASCII. This is the default file type. All constants
108      * ending in <code>FILE_TYPE</code> are used to indicate file types.
109      ***/

110     public static final int ASCII_FILE_TYPE = 0;
111
112     /***
113      * A constant used to indicate the file(s) being transfered should
114      * be treated as EBCDIC. Note however that there are several different
115      * EBCDIC formats. All constants ending in <code>FILE_TYPE</code>
116      * are used to indicate file types.
117      ***/

118     public static final int EBCDIC_FILE_TYPE = 1;
119
120     /***
121      * A constant used to indicate the file(s) being transfered should
122      * be treated as a binary image, i.e., no translations should be
123      * performed. All constants ending in <code>FILE_TYPE</code> are used to
124      * indicate file types.
125      ***/

126     public static final int IMAGE_FILE_TYPE = 2;
127
128     /***
129      * A constant used to indicate the file(s) being transfered should
130      * be treated as a binary image, i.e., no translations should be
131      * performed. All constants ending in <code>FILE_TYPE</code> are used to
132      * indicate file types.
133      ***/

134     public static final int BINARY_FILE_TYPE = 2;
135
136     /***
137      * A constant used to indicate the file(s) being transfered should
138      * be treated as a local type. All constants ending in
139      * <code>FILE_TYPE</code> are used to indicate file types.
140      ***/

141     public static final int LOCAL_FILE_TYPE = 3;
142
143     /***
144      * A constant used for text files to indicate a non-print text format.
145      * This is the default format.
146      * All constants ending in <code>TEXT_FORMAT</code> are used to indicate
147      * text formatting for text transfers (both ASCII and EBCDIC).
148      ***/

149     public static final int NON_PRINT_TEXT_FORMAT = 4;
150
151     /***
152      * A constant used to indicate a text file contains format vertical format
153      * control characters.
154      * All constants ending in <code>TEXT_FORMAT</code> are used to indicate
155      * text formatting for text transfers (both ASCII and EBCDIC).
156      ***/

157     public static final int TELNET_TEXT_FORMAT = 5;
158
159     /***
160      * A constant used to indicate a text file contains ASA vertical format
161      * control characters.
162      * All constants ending in <code>TEXT_FORMAT</code> are used to indicate
163      * text formatting for text transfers (both ASCII and EBCDIC).
164      ***/

165     public static final int CARRIAGE_CONTROL_TEXT_FORMAT = 6;
166
167     /***
168      * A constant used to indicate a file is to be treated as a continuous
169      * sequence of bytes. This is the default structure. All constants ending
170      * in <code>_STRUCTURE</code> are used to indicate file structure for
171      * file transfers.
172      ***/

173     public static final int FILE_STRUCTURE = 7;
174
175     /***
176      * A constant used to indicate a file is to be treated as a sequence
177      * of records. All constants ending in <code>_STRUCTURE</code>
178      * are used to indicate file structure for file transfers.
179      ***/

180     public static final int RECORD_STRUCTURE = 8;
181
182     /***
183      * A constant used to indicate a file is to be treated as a set of
184      * independent indexed pages. All constants ending in
185      * <code>_STRUCTURE</code> are used to indicate file structure for file
186      * transfers.
187      ***/

188     public static final int PAGE_STRUCTURE = 9;
189
190     /***
191      * A constant used to indicate a file is to be transfered as a stream
192      * of bytes. This is the default transfer mode. All constants ending
193      * in <code>TRANSFER_MODE</code> are used to indicate file transfer
194      * modes.
195      ***/

196     public static final int STREAM_TRANSFER_MODE = 10;
197
198     /***
199      * A constant used to indicate a file is to be transfered as a series
200      * of blocks. All constants ending in <code>TRANSFER_MODE</code> are used
201      * to indicate file transfer modes.
202      ***/

203     public static final int BLOCK_TRANSFER_MODE = 11;
204
205     /***
206      * A constant used to indicate a file is to be transfered as FTP
207      * compressed data. All constants ending in <code>TRANSFER_MODE</code>
208      * are used to indicate file transfer modes.
209      ***/

210     public static final int COMPRESSED_TRANSFER_MODE = 12;
211
212     // We have to ensure that the protocol communication is in ASCII
213
// but we use ISO-8859-1 just in case 8-bit characters cross
214
// the wire.
215
/**
216      * The default character encoding used for communicating over an
217      * FTP control connection. The default encoding is an
218      * ASCII-compatible encoding. Some FTP servers expect other
219      * encodings. You can change the encoding used by an FTP instance
220      * with {@link #setControlEncoding setControlEncoding}.
221      */

222     public static final String JavaDoc DEFAULT_CONTROL_ENCODING = "ISO-8859-1";
223     private static final String JavaDoc __modes = "ABILNTCFRPSBC";
224
225     private StringBuffer JavaDoc __commandBuffer;
226
227     BufferedReader JavaDoc _controlInput;
228     BufferedWriter JavaDoc _controlOutput;
229     int _replyCode;
230     Vector JavaDoc _replyLines;
231     boolean _newReplyString;
232     String JavaDoc _replyString;
233     String JavaDoc _controlEncoding;
234
235     /***
236      * A ProtocolCommandSupport object used to manage the registering of
237      * ProtocolCommandListeners and te firing of ProtocolCommandEvents.
238      ***/

239     protected ProtocolCommandSupport _commandSupport_;
240
241     /***
242      * The default FTP constructor. Sets the default port to
243      * <code>DEFAULT_PORT</code> and initializes internal data structures
244      * for saving FTP reply information.
245      ***/

246     public FTP()
247     {
248         setDefaultPort(DEFAULT_PORT);
249         __commandBuffer = new StringBuffer JavaDoc();
250         _replyLines = new Vector JavaDoc();
251         _newReplyString = false;
252         _replyString = null;
253         _commandSupport_ = new ProtocolCommandSupport(this);
254         _controlEncoding = DEFAULT_CONTROL_ENCODING;
255     }
256
257     private void __getReply() throws IOException JavaDoc
258     {
259         int length;
260
261         _newReplyString = true;
262         _replyLines.setSize(0);
263
264         String JavaDoc line = _controlInput.readLine();
265
266         if (line == null)
267             throw new FTPConnectionClosedException(
268                 "Connection closed without indication.");
269
270         // In case we run into an anomaly we don't want fatal index exceptions
271
// to be thrown.
272
length = line.length();
273         if (length < 3)
274             throw new MalformedServerReplyException(
275                 "Truncated server reply: " + line);
276
277         try
278         {
279             String JavaDoc code = line.substring(0, 3);
280             _replyCode = Integer.parseInt(code);
281         }
282         catch (NumberFormatException JavaDoc e)
283         {
284             throw new MalformedServerReplyException(
285                 "Could not parse response code.\nServer Reply: " + line);
286         }
287
288         _replyLines.addElement(line);
289
290         // Get extra lines if message continues.
291
if (length > 3 && line.charAt(3) == '-')
292         {
293             do
294             {
295                 line = _controlInput.readLine();
296
297                 if (line == null)
298                     throw new FTPConnectionClosedException(
299                         "Connection closed without indication.");
300
301                 _replyLines.addElement(line);
302
303                 // The length() check handles problems that could arise from readLine()
304
// returning too soon after encountering a naked CR or some other
305
// anomaly.
306
}
307             while (!(line.length() >= 4 && line.charAt(3) != '-' &&
308                      Character.isDigit(line.charAt(0))));
309             // This is too strong a condition because of non-conforming ftp
310
// servers like ftp.funet.fi which sent 226 as the last line of a
311
// 426 multi-line reply in response to ls /. We relax the condition to
312
// test that the line starts with a digit rather than starting with
313
// the code.
314
// line.startsWith(code)));
315
}
316
317         if (_commandSupport_.getListenerCount() > 0)
318             _commandSupport_.fireReplyReceived(_replyCode, getReplyString());
319
320         if (_replyCode == FTPReply.SERVICE_NOT_AVAILABLE)
321             throw new FTPConnectionClosedException(
322                 "FTP response 421 received. Server closed connection.");
323     }
324
325     // initiates control connections and gets initial reply
326
protected void _connectAction_() throws IOException JavaDoc
327     {
328         super._connectAction_();
329         _controlInput =
330             new BufferedReader JavaDoc(new InputStreamReader JavaDoc(getInputStream(),
331                                                      getControlEncoding()));
332         _controlOutput =
333             new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(getOutputStream(),
334                                                       getControlEncoding()));
335         __getReply();
336         // If we received code 120, we have to fetch completion reply.
337
if (FTPReply.isPositivePreliminary(_replyCode))
338             __getReply();
339     }
340
341
342     /**
343      * Sets the character encoding used by the FTP control connection.
344      * Some FTP servers require that commands be issued in a non-ASCII
345      * encoding like UTF-8 so that filenames with multi-byte character
346      * representations (e.g, Big 8) can be specified.
347      *
348      * @param encoding The new character encoding for the control connection.
349      */

350     public void setControlEncoding(String JavaDoc encoding) {
351         _controlEncoding = encoding;
352     }
353
354
355     /**
356      * @return The character encoding used to communicate over the
357      * control connection.
358      */

359     public String JavaDoc getControlEncoding() {
360         return _controlEncoding;
361     }
362
363
364     /***
365      * Adds a ProtocolCommandListener. Delegates this task to
366      * {@link #_commandSupport_ _commandSupport_ }.
367      * <p>
368      * @param listener The ProtocolCommandListener to add.
369      ***/

370     public void addProtocolCommandListener(ProtocolCommandListener listener)
371     {
372         _commandSupport_.addProtocolCommandListener(listener);
373     }
374
375     /***
376      * Removes a ProtocolCommandListener. Delegates this task to
377      * {@link #_commandSupport_ _commandSupport_ }.
378      * <p>
379      * @param listener The ProtocolCommandListener to remove.
380      ***/

381     public void removeProtocolCommandListener(ProtocolCommandListener listener)
382     {
383         _commandSupport_.removeProtocolCommandListener(listener);
384     }
385
386
387     /***
388      * Closes the control connection to the FTP server and sets to null
389      * some internal data so that the memory may be reclaimed by the
390      * garbage collector. The reply text and code information from the
391      * last command is voided so that the memory it used may be reclaimed.
392      * <p>
393      * @exception IOException If an error occurs while disconnecting.
394      ***/

395     public void disconnect() throws IOException JavaDoc
396     {
397         super.disconnect();
398         _controlInput = null;
399         _controlOutput = null;
400         _replyLines.setSize(0);
401         _newReplyString = false;
402         _replyString = null;
403     }
404
405
406     /***
407      * Sends an FTP command to the server, waits for a reply and returns the
408      * numerical response code. After invocation, for more detailed
409      * information, the actual reply text can be accessed by calling
410      * {@link #getReplyString getReplyString } or
411      * {@link #getReplyStrings getReplyStrings }.
412      * <p>
413      * @param command The text representation of the FTP command to send.
414      * @param args The arguments to the FTP command. If this parameter is
415      * set to null, then the command is sent with no argument.
416      * @return The integer value of the FTP reply code returned by the server
417      * in response to the command.
418      * @exception FTPConnectionClosedException
419      * If the FTP server prematurely closes the connection as a result
420      * of the client being idle or some other reason causing the server
421      * to send FTP reply code 421. This exception may be caught either
422      * as an IOException or independently as itself.
423      * @exception IOException If an I/O error occurs while either sending the
424      * command or receiving the server reply.
425      ***/

426     public int sendCommand(String JavaDoc command, String JavaDoc args) throws IOException JavaDoc
427     {
428         String JavaDoc message;
429
430         __commandBuffer.setLength(0);
431         __commandBuffer.append(command);
432
433         if (args != null)
434         {
435             __commandBuffer.append(' ');
436             __commandBuffer.append(args);
437         }
438         __commandBuffer.append(SocketClient.NETASCII_EOL);
439
440         try{
441         _controlOutput.write(message = __commandBuffer.toString());
442         _controlOutput.flush();
443         }
444         catch (SocketException JavaDoc e)
445         {
446             if (!isConnected() || !socketIsConnected(_socket_))
447             {
448                 throw new FTPConnectionClosedException("Connection unexpectedly closed.");
449             }
450             else
451             {
452                 throw e;
453             }
454         }
455     
456
457         if (_commandSupport_.getListenerCount() > 0)
458             _commandSupport_.fireCommandSent(command, message);
459
460         __getReply();
461         return _replyCode;
462     }
463
464     /**
465      * Checks if the socket is connected using reflection to be backward compatible.
466      * The return value of this method is only meaningful in an java 1.4 environment.
467      *
468      * @param socket
469      * @return true if connected or pre java 1.4
470      */

471     private boolean socketIsConnected(Socket JavaDoc socket)
472     {
473         if (socket == null)
474         {
475             return false;
476         }
477
478         try
479         {
480             Method JavaDoc isConnected = socket.getClass().getMethod("isConnected", null);
481             return ((Boolean JavaDoc) isConnected.invoke(socket, null)).booleanValue();
482         }
483         catch (NoSuchMethodException JavaDoc e)
484         {
485             return true;
486         }
487         catch (IllegalAccessException JavaDoc e)
488         {
489             return true;
490         }
491         catch (InvocationTargetException JavaDoc e)
492         {
493             return true;
494         }
495     }
496
497     /***
498      * Sends an FTP command to the server, waits for a reply and returns the
499      * numerical response code. After invocation, for more detailed
500      * information, the actual reply text can be accessed by calling
501      * {@link #getReplyString getReplyString } or
502      * {@link #getReplyStrings getReplyStrings }.
503      * <p>
504      * @param command The FTPCommand constant corresponding to the FTP command
505      * to send.
506      * @param args The arguments to the FTP command. If this parameter is
507      * set to null, then the command is sent with no argument.
508      * @return The integer value of the FTP reply code returned by the server
509      * in response to the command.
510      * @exception FTPConnectionClosedException
511      * If the FTP server prematurely closes the connection as a result
512      * of the client being idle or some other reason causing the server
513      * to send FTP reply code 421. This exception may be caught either
514      * as an IOException or independently as itself.
515      * @exception IOException If an I/O error occurs while either sending the
516      * command or receiving the server reply.
517      ***/

518     public int sendCommand(int command, String JavaDoc args) throws IOException JavaDoc
519     {
520         return sendCommand(FTPCommand._commands[command], args);
521     }
522
523
524     /***
525      * Sends an FTP command with no arguments to the server, waits for a
526      * reply and returns the numerical response code. After invocation, for
527      * more detailed information, the actual reply text can be accessed by
528      * calling {@link #getReplyString getReplyString } or
529      * {@link #getReplyStrings getReplyStrings }.
530      * <p>
531      * @param command The text representation of the FTP command to send.
532      * @return The integer value of the FTP reply code returned by the server
533      * in response to the command.
534      * @exception FTPConnectionClosedException
535      * If the FTP server prematurely closes the connection as a result
536      * of the client being idle or some other reason causing the server
537      * to send FTP reply code 421. This exception may be caught either
538      * as an IOException or independently as itself.
539      * @exception IOException If an I/O error occurs while either sending the
540      * command or receiving the server reply.
541      ***/

542     public int sendCommand(String JavaDoc command) throws IOException JavaDoc
543     {
544         return sendCommand(command, null);
545     }
546
547
548     /***
549      * Sends an FTP command with no arguments to the server, waits for a
550      * reply and returns the numerical response code. After invocation, for
551      * more detailed information, the actual reply text can be accessed by
552      * calling {@link #getReplyString getReplyString } or
553      * {@link #getReplyStrings getReplyStrings }.
554      * <p>
555      * @param command The FTPCommand constant corresponding to the FTP command
556      * to send.
557      * @return The integer value of the FTP reply code returned by the server
558      * in response to the command.
559      * @exception FTPConnectionClosedException
560      * If the FTP server prematurely closes the connection as a result
561      * of the client being idle or some other reason causing the server
562      * to send FTP reply code 421. This exception may be caught either
563      * as an IOException or independently as itself.
564      * @exception IOException If an I/O error occurs while either sending the
565      * command or receiving the server reply.
566      ***/

567     public int sendCommand(int command) throws IOException JavaDoc
568     {
569         return sendCommand(command, null);
570     }
571
572
573     /***
574      * Returns the integer value of the reply code of the last FTP reply.
575      * You will usually only use this method after you connect to the
576      * FTP server to check that the connection was successful since
577      * <code> connect </code> is of type void.
578      * <p>
579      * @return The integer value of the reply code of the last FTP reply.
580      ***/

581     public int getReplyCode()
582     {
583         return _replyCode;
584     }
585
586     /***
587      * Fetches a reply from the FTP server and returns the integer reply
588      * code. After calling this method, the actual reply text can be accessed
589      * from either calling {@link #getReplyString getReplyString } or
590      * {@link #getReplyStrings getReplyStrings }. Only use this
591      * method if you are implementing your own FTP client or if you need to
592      * fetch a secondary response from the FTP server.
593      * <p>
594      * @return The integer value of the reply code of the fetched FTP reply.
595      * @exception FTPConnectionClosedException
596      * If the FTP server prematurely closes the connection as a result
597      * of the client being idle or some other reason causing the server
598      * to send FTP reply code 421. This exception may be caught either
599      * as an IOException or independently as itself.
600      * @exception IOException If an I/O error occurs while receiving the
601      * server reply.
602      ***/

603     public int getReply() throws IOException JavaDoc
604     {
605         __getReply();
606         return _replyCode;
607     }
608
609
610     /***
611      * Returns the lines of text from the last FTP server response as an array
612      * of strings, one entry per line. The end of line markers of each are
613      * stripped from each line.
614      * <p>
615      * @return The lines of text from the last FTP response as an array.
616      ***/

617     public String JavaDoc[] getReplyStrings()
618     {
619         String JavaDoc[] lines;
620         lines = new String JavaDoc[_replyLines.size()];
621         _replyLines.copyInto(lines);
622         return lines;
623     }
624
625     /***
626      * Returns the entire text of the last FTP server response exactly
627      * as it was received, including all end of line markers in NETASCII
628      * format.
629      * <p>
630      * @return The entire text from the last FTP response as a String.
631      ***/

632     public String JavaDoc getReplyString()
633     {
634         Enumeration JavaDoc en;
635         StringBuffer JavaDoc buffer;
636
637         if (!_newReplyString)
638             return _replyString;
639
640         buffer = new StringBuffer JavaDoc(256);
641         en = _replyLines.elements();
642         while (en.hasMoreElements())
643         {
644             buffer.append((String JavaDoc)en.nextElement());
645             buffer.append(SocketClient.NETASCII_EOL);
646         }
647
648         _newReplyString = false;
649
650         return (_replyString = buffer.toString());
651     }
652
653
654     /***
655      * A convenience method to send the FTP USER command to the server,
656      * receive the reply, and return the reply code.
657      * <p>
658      * @param username The username to login under.
659      * @return The reply code received from the server.
660      * @exception FTPConnectionClosedException
661      * If the FTP server prematurely closes the connection as a result
662      * of the client being idle or some other reason causing the server
663      * to send FTP reply code 421. This exception may be caught either
664      * as an IOException or independently as itself.
665      * @exception IOException If an I/O error occurs while either sending the
666      * command or receiving the server reply.
667      ***/

668     public int user(String JavaDoc username) throws IOException JavaDoc
669     {
670         return sendCommand(FTPCommand.USER, username);
671     }
672
673     /**
674      * A convenience method to send the FTP PASS command to the server,
675      * receive the reply, and return the reply code.
676      * @param password The plain text password of the username being logged into.
677      * @return The reply code received from the server.
678      * @exception FTPConnectionClosedException
679      * If the FTP server prematurely closes the connection as a result
680      * of the client being idle or some other reason causing the server
681      * to send FTP reply code 421. This exception may be caught either
682      * as an IOException or independently as itself.
683      * @exception IOException If an I/O error occurs while either sending the
684      * command or receiving the server reply.
685      */

686     public int pass(String JavaDoc password) throws IOException JavaDoc
687     {
688         return sendCommand(FTPCommand.PASS, password);
689     }
690
691     /***
692      * A convenience method to send the FTP ACCT command to the server,
693      * receive the reply, and return the reply code.
694      * <p>
695      * @param account The account name to access.
696      * @return The reply code received from the server.
697      * @exception FTPConnectionClosedException
698      * If the FTP server prematurely closes the connection as a result
699      * of the client being idle or some other reason causing the server
700      * to send FTP reply code 421. This exception may be caught either
701      * as an IOException or independently as itself.
702      * @exception IOException If an I/O error occurs while either sending the
703      * command or receiving the server reply.
704      ***/

705     public int acct(String JavaDoc account) throws IOException JavaDoc
706     {
707         return sendCommand(FTPCommand.ACCT, account);
708     }
709
710
711     /***
712      * A convenience method to send the FTP ABOR command to the server,
713      * receive the reply, and return the reply code.
714      * <p>
715      * @return The reply code received from the server.
716      * @exception FTPConnectionClosedException
717      * If the FTP server prematurely closes the connection as a result
718      * of the client being idle or some other reason causing the server
719      * to send FTP reply code 421. This exception may be caught either
720      * as an IOException or independently as itself.
721      * @exception IOException If an I/O error occurs while either sending the
722      * command or receiving the server reply.
723      ***/

724     public int abor() throws IOException JavaDoc
725     {
726         return sendCommand(FTPCommand.ABOR);
727     }
728
729     /***
730      * A convenience method to send the FTP CWD command to the server,
731      * receive the reply, and return the reply code.
732      * <p>
733      * @param directory The new working directory.
734      * @return The reply code received from the server.
735      * @exception FTPConnectionClosedException
736      * If the FTP server prematurely closes the connection as a result
737      * of the client being idle or some other reason causing the server
738      * to send FTP reply code 421. This exception may be caught either
739      * as an IOException or independently as itself.
740      * @exception IOException If an I/O error occurs while either sending the
741      * command or receiving the server reply.
742      ***/

743     public int cwd(String JavaDoc directory) throws IOException JavaDoc
744     {
745         return sendCommand(FTPCommand.CWD, directory);
746     }
747
748     /***
749      * A convenience method to send the FTP CDUP command to the server,
750      * receive the reply, and return the reply code.
751      * <p>
752      * @return The reply code received from the server.
753      * @exception FTPConnectionClosedException
754      * If the FTP server prematurely closes the connection as a result
755      * of the client being idle or some other reason causing the server
756      * to send FTP reply code 421. This exception may be caught either
757      * as an IOException or independently as itself.
758      * @exception IOException If an I/O error occurs while either sending the
759      * command or receiving the server reply.
760      ***/

761     public int cdup() throws IOException JavaDoc
762     {
763         return sendCommand(FTPCommand.CDUP);
764     }
765
766     /***
767      * A convenience method to send the FTP QUIT command to the server,
768      * receive the reply, and return the reply code.
769      * <p>
770      * @return The reply code received from the server.
771      * @exception FTPConnectionClosedException
772      * If the FTP server prematurely closes the connection as a result
773      * of the client being idle or some other reason causing the server
774      * to send FTP reply code 421. This exception may be caught either
775      * as an IOException or independently as itself.
776      * @exception IOException If an I/O error occurs while either sending the
777      * command or receiving the server reply.
778      ***/

779     public int quit() throws IOException JavaDoc
780     {
781         return sendCommand(FTPCommand.QUIT);
782     }
783
784     /***
785      * A convenience method to send the FTP REIN command to the server,
786      * receive the reply, and return the reply code.
787      * <p>
788      * @return The reply code received from the server.
789      * @exception FTPConnectionClosedException
790      * If the FTP server prematurely closes the connection as a result
791      * of the client being idle or some other reason causing the server
792      * to send FTP reply code 421. This exception may be caught either
793      * as an IOException or independently as itself.
794      * @exception IOException If an I/O error occurs while either sending the
795      * command or receiving the server reply.
796      ***/

797     public int rein() throws IOException JavaDoc
798     {
799         return sendCommand(FTPCommand.REIN);
800     }
801
802     /***
803      * A convenience method to send the FTP SMNT command to the server,
804      * receive the reply, and return the reply code.
805      * <p>
806      * @param dir The directory name.
807      * @return The reply code received from the server.
808      * @exception FTPConnectionClosedException
809      * If the FTP server prematurely closes the connection as a result
810      * of the client being idle or some other reason causing the server
811      * to send FTP reply code 421. This exception may be caught either
812      * as an IOException or independently as itself.
813      * @exception IOException If an I/O error occurs while either sending the
814      * command or receiving the server reply.
815      ***/

816     public int smnt(String JavaDoc dir) throws IOException JavaDoc
817     {
818         return sendCommand(FTPCommand.SMNT, dir);
819     }
820
821     /***
822      * A convenience method to send the FTP PORT command to the server,
823      * receive the reply, and return the reply code.
824      * <p>
825      * @param host The host owning the port.
826      * @param port The new port.
827      * @return The reply code received from the server.
828      * @exception FTPConnectionClosedException
829      * If the FTP server prematurely closes the connection as a result
830      * of the client being idle or some other reason causing the server
831      * to send FTP reply code 421. This exception may be caught either
832      * as an IOException or independently as itself.
833      * @exception IOException If an I/O error occurs while either sending the
834      * command or receiving the server reply.
835      ***/

836     public int port(InetAddress JavaDoc host, int port) throws IOException JavaDoc
837     {
838         int num;
839         StringBuffer JavaDoc info = new StringBuffer JavaDoc(24);
840
841         info.append(host.getHostAddress().replace('.', ','));
842         num = port >>> 8;
843         info.append(',');
844         info.append(num);
845         info.append(',');
846         num = port & 0xff;
847         info.append(num);
848
849         return sendCommand(FTPCommand.PORT, info.toString());
850     }
851
852     /***
853      * A convenience method to send the FTP PASV command to the server,
854      * receive the reply, and return the reply code. Remember, it's up
855      * to you to interpret the reply string containing the host/port
856      * information.
857      * <p>
858      * @return The reply code received from the server.
859      * @exception FTPConnectionClosedException
860      * If the FTP server prematurely closes the connection as a result
861      * of the client being idle or some other reason causing the server
862      * to send FTP reply code 421. This exception may be caught either
863      * as an IOException or independently as itself.
864      * @exception IOException If an I/O error occurs while either sending the
865      * command or receiving the server reply.
866      ***/

867     public int pasv() throws IOException JavaDoc
868     {
869         return sendCommand(FTPCommand.PASV);
870     }
871
872     /**
873      * A convenience method to send the FTP TYPE command for text files
874      * to the server, receive the reply, and return the reply code.
875      * @param fileType The type of the file (one of the <code>FILE_TYPE</code>
876      * constants).
877      * @param formatOrByteSize The format of the file (one of the
878      * <code>_FORMAT</code> constants. In the case of
879      * <code>LOCAL_FILE_TYPE</code>, the byte size.
880      * @return The reply code received from the server.
881      * @exception FTPConnectionClosedException
882      * If the FTP server prematurely closes the connection as a result
883      * of the client being idle or some other reason causing the server
884      * to send FTP reply code 421. This exception may be caught either
885      * as an IOException or independently as itself.
886      * @exception IOException If an I/O error occurs while either sending the
887      * command or receiving the server reply.
888      */

889     public int type(int fileType, int formatOrByteSize) throws IOException JavaDoc
890     {
891         StringBuffer JavaDoc arg = new StringBuffer JavaDoc();
892
893         arg.append(__modes.charAt(fileType));
894         arg.append(' ');
895         if (fileType == LOCAL_FILE_TYPE)
896             arg.append(formatOrByteSize);
897         else
898             arg.append(__modes.charAt(formatOrByteSize));
899
900         return sendCommand(FTPCommand.TYPE, arg.toString());
901     }
902
903
904     /**
905      * A convenience method to send the FTP TYPE command to the server,
906      * receive the reply, and return the reply code.
907      * <p>
908      * @param fileType The type of the file (one of the <code>FILE_TYPE</code>
909      * constants).
910      * @return The reply code received from the server.
911      * @exception FTPConnectionClosedException
912      * If the FTP server prematurely closes the connection as a result
913      * of the client being idle or some other reason causing the server
914      * to send FTP reply code 421. This exception may be caught either
915      * as an IOException or independently as itself.
916      * @exception IOException If an I/O error occurs while either sending the
917      * command or receiving the server reply.
918      */

919     public int type(int fileType) throws IOException JavaDoc
920     {
921         return sendCommand(FTPCommand.TYPE,
922                            __modes.substring(fileType, fileType +