Index: FTP.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/net/src/java/org/apache/commons/net/ftp/FTP.java,v retrieving revision 1.16 diff -u -r1.16 FTP.java --- FTP.java 8 Aug 2004 20:38:35 -0000 1.16 +++ FTP.java 31 Aug 2004 18:11:51 -0000 @@ -26,16 +26,12 @@ import org.apache.commons.net.ProtocolCommandListener; import org.apache.commons.net.ProtocolCommandSupport; import org.apache.commons.net.SocketClient; -import org.apache.commons.net.telnet.TelnetClient; /*** * FTP provides the basic the functionality necessary to implement your - * own FTP client. It extends org.apache.commons.net.TelnetClient - * simply because it saves the writing of extra code to handle the FTP - * control connection which always remains open during an FTP session and - * uses the Telnet protocol. Aggregation would require writing new - * wrapper methods and wouldn't leverage the functionality already - * present in org.apache.commons.net.SocketClient. + * own FTP client. It extends org.apache.commons.net.SocketClient since + * extending TelnetClient was causing unwanted behavior (like connections + * that did not time out properly). *

* To derive the full benefits of the FTP class requires some knowledge * of the FTP protocol defined in RFC 959. However, there is no reason @@ -85,12 +81,13 @@ *

*

* @author Daniel F. Savarese + * @author Joseph Hindsley * @see FTPClient * @see FTPConnectionClosedException * @see org.apache.commons.net.MalformedServerReplyException ***/ -public class FTP extends TelnetClient +public class FTP extends SocketClient { /*** The default FTP data port (20). ***/ public static final int DEFAULT_DATA_PORT = 20; @@ -217,19 +214,17 @@ public static final String DEFAULT_CONTROL_ENCODING = "ISO-8859-1"; private static final String __modes = "ABILNTCFRPSBC"; - private StringBuffer __commandBuffer; - - BufferedReader _controlInput; - BufferedWriter _controlOutput; - int _replyCode; - Vector _replyLines; - boolean _newReplyString; - String _replyString; - String _controlEncoding; + protected BufferedReader _controlInput; + protected BufferedWriter _controlOutput; + protected int _replyCode; + protected Vector _replyLines; + protected boolean _newReplyString; + protected String _replyString; + protected String _controlEncoding; /*** * A ProtocolCommandSupport object used to manage the registering of - * ProtocolCommandListeners and te firing of ProtocolCommandEvents. + * ProtocolCommandListeners and the firing of ProtocolCommandEvents. ***/ protected ProtocolCommandSupport _commandSupport_; @@ -240,8 +235,8 @@ ***/ public FTP() { + super(); setDefaultPort(DEFAULT_PORT); - __commandBuffer = new StringBuffer(); _replyLines = new Vector(); _newReplyString = false; _replyString = null; @@ -251,33 +246,26 @@ private void __getReply() throws IOException { - int length; - _newReplyString = true; _replyLines.setSize(0); String line = _controlInput.readLine(); - if (line == null) - throw new FTPConnectionClosedException( - "Connection closed without indication."); + if (line == null) { + throw new FTPConnectionClosedException("Connection closed without indication."); + } // In case we run into an anomaly we don't want fatal index exceptions // to be thrown. - length = line.length(); - if (length < 3) - throw new MalformedServerReplyException( - "Truncated server reply: " + line); - - try - { - String code = line.substring(0, 3); - _replyCode = Integer.parseInt(code); + final int length = line.length(); + if (length < 3) { + throw new MalformedServerReplyException("Truncated server reply: " + line); } - catch (NumberFormatException e) - { - throw new MalformedServerReplyException( - "Could not parse response code.\nServer Reply: " + line); + + try { + _replyCode = Integer.parseInt( line.substring(0, 3) ); + } catch (NumberFormatException e) { + throw new MalformedServerReplyException("Could not parse response code.\nServer Reply: " + line); } _replyLines.addElement(line); @@ -309,12 +297,13 @@ // line.startsWith(code))); } - if (_commandSupport_.getListenerCount() > 0) + if (_commandSupport_.getListenerCount() > 0) { _commandSupport_.fireReplyReceived(_replyCode, getReplyString()); + } - if (_replyCode == FTPReply.SERVICE_NOT_AVAILABLE) - throw new FTPConnectionClosedException( - "FTP response 421 received. Server closed connection."); + if (_replyCode == FTPReply.SERVICE_NOT_AVAILABLE) { + throw new FTPConnectionClosedException("FTP response 421 received. Server closed connection."); + } } // initiates control connections and gets initial reply @@ -322,10 +311,10 @@ { super._connectAction_(); _controlInput = - new BufferedReader(new InputStreamReader(getInputStream(), + new BufferedReader(new InputStreamReader(_socket_.getInputStream(), getControlEncoding())); _controlOutput = - new BufferedWriter(new OutputStreamWriter(getOutputStream(), + new BufferedWriter(new OutputStreamWriter(_socket_.getOutputStream(), getControlEncoding())); __getReply(); // If we received code 120, we have to fetch completion reply. @@ -420,23 +409,23 @@ ***/ public int sendCommand(String command, String args) throws IOException { - String message; - - __commandBuffer.setLength(0); - __commandBuffer.append(command); + final StringBuffer cmdBuffer = new StringBuffer(0); + cmdBuffer.append(command); if (args != null) { - __commandBuffer.append(' '); - __commandBuffer.append(args); + cmdBuffer.append(' '); + cmdBuffer.append(args); } - __commandBuffer.append(SocketClient.NETASCII_EOL); + cmdBuffer.append(SocketClient.NETASCII_EOL); - _controlOutput.write(message = __commandBuffer.toString()); + _controlOutput.write(cmdBuffer.toString()); _controlOutput.flush(); + - if (_commandSupport_.getListenerCount() > 0) - _commandSupport_.fireCommandSent(command, message); + if (_commandSupport_.getListenerCount() > 0) { + _commandSupport_.fireCommandSent(command, cmdBuffer.toString()); + } __getReply(); return _replyCode; @@ -583,8 +572,9 @@ Enumeration enum; StringBuffer buffer; - if (!_newReplyString) + if (!_newReplyString) { return _replyString; + } buffer = new StringBuffer(256); enum = _replyLines.elements();