FTP Client

com.amoebacode.ftp
Class FTPSocket

java.lang.Object
  extended bycom.amoebacode.ftp.FTPSocket

class FTPSocket
extends java.lang.Object

FTPSocket encapsulates all communication between the FTP client and the FTP server.

This class incorporates elements from Bret Taylor's FTPConection.


Field Summary
private  java.net.Socket connectionSocket
          The communication socket connecting the FTP client and server
private  boolean debugMode
          Indicates whether server responses should be logged
private  java.io.BufferedReader inputStream
          The input stream for the primary communication socket
private  java.lang.String lineSeparator
          The line separator used for sending commands (default value is the "line.seperator" System property)
private  java.io.PrintStream outputStream
          The output stream for the primary communication socket
private  int socketTimeout
          The socket timeout delay
 
Constructor Summary
protected FTPSocket(int socketTimeout, boolean debugMode)
          Default protected constructor.
 
Method Summary
protected  void closeConnection()
          Closes this connection from the FTP server.
private  void debugPrint(java.lang.String message)
          When debug mode is on, prints the specified message to the System output stream.
private  FTPServerResponse executeCommand(java.lang.String command)
          Sends the specified command to the FTP server.
protected  FTPServerResponse executeCompleteCommand(java.lang.String command)
          Sends the specified command to the FTP server (expecting a positive complete response).
protected  void executeDataCommand(java.lang.String command, java.io.InputStream in)
          Sends the specified data command to the FTP server, then transfers the contents of the specified input stream.
protected  void executeDataCommand(java.lang.String command, java.io.OutputStream out)
          Sends the specified data command to the FTP server, transferring the results to the specified output stream.
protected  FTPServerResponse executeIntermediateCommand(java.lang.String command)
          Sends the specified command to the FTP server (expecting a positive intermediate response).
protected  FTPServerResponse executePreliminaryCommand(java.lang.String command)
          Sends the specified command to the FTP server (expecting a positive preliminary response).
private  java.net.Socket getDataSocket()
          Creates and returns a data socket between the client and the FTP server.
private  FTPServerResponse getServerResponse()
          Returns the FTP server response to the last client command.
protected  void openConnection(java.lang.String host, int port)
          Opens a connection to the specified host through the specified port.
protected  void readDataToFile(java.lang.String command, java.lang.String fileName)
          Sends the specified data command to the FTP server, transferring the results to the file at the specified path.
protected  java.lang.String[] readDataToString(java.lang.String command)
          Sends the specified data command to the FTP server, returning the results as a String array.
 void setLineSeparator(java.lang.String lineSeparator)
          Sets the line separator used for sending commands.
protected  void setTransferType(boolean ascii)
          Sets the transfer type (ASCII or binary) for this connection.
private  void transferData(java.io.InputStream in, java.io.OutputStream out)
          Transfers data in one kilobyte blocks from the specified input stream to the specified output stream.
protected  void writeDataFromFile(java.lang.String command, java.lang.String fileName)
          Sends the specified data command to the FTP server, then transfers the contents of the file at the specified path.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debugMode

private boolean debugMode
Indicates whether server responses should be logged


connectionSocket

private java.net.Socket connectionSocket
The communication socket connecting the FTP client and server


outputStream

private java.io.PrintStream outputStream
The output stream for the primary communication socket


inputStream

private java.io.BufferedReader inputStream
The input stream for the primary communication socket


socketTimeout

private int socketTimeout
The socket timeout delay


lineSeparator

private java.lang.String lineSeparator
The line separator used for sending commands (default value is the "line.seperator" System property)

Constructor Detail

FTPSocket

protected FTPSocket(int socketTimeout,
                    boolean debugMode)
Default protected constructor.

Parameters:
socketTimeout - specifies the socket timeout delay
debugMode - indicates whether server responses should be logged
Method Detail

openConnection

protected void openConnection(java.lang.String host,
                              int port)
                       throws java.net.UnknownHostException,
                              java.io.IOException,
                              IllegalFTPResponseException
Opens a connection to the specified host through the specified port.

Parameters:
host - the host address of the FTP server
port - the FTP server connection port
Throws:
IllegalFTPResponseException - if a positive complete response is not received
java.net.UnknownHostException
java.io.IOException

closeConnection

protected void closeConnection()
                        throws java.io.IOException
Closes this connection from the FTP server.

Throws:
java.io.IOException

setTransferType

protected void setTransferType(boolean ascii)
                        throws java.io.IOException,
                               IllegalFTPResponseException
Sets the transfer type (ASCII or binary) for this connection.

Parameters:
ascii - the new transfer type
Throws:
java.io.IOException
IllegalFTPResponseException

getServerResponse

private FTPServerResponse getServerResponse()
                                     throws java.io.IOException
Returns the FTP server response to the last client command.

Returns:
the FTPServerResponse object
Throws:
java.io.IOException

executeCommand

private FTPServerResponse executeCommand(java.lang.String command)
                                  throws java.io.IOException
Sends the specified command to the FTP server.

Parameters:
command - the command to send to the FTP server
Returns:
the response from the FTP server
Throws:
java.io.IOException

executeCompleteCommand

protected FTPServerResponse executeCompleteCommand(java.lang.String command)
                                            throws java.io.IOException,
                                                   IllegalFTPResponseException
Sends the specified command to the FTP server (expecting a positive complete response).

Parameters:
command - the command to send to the FTP server
Returns:
the response from the FTP server
Throws:
IllegalFTPResponseException - if a positive complete response is not received
java.io.IOException

executeIntermediateCommand

protected FTPServerResponse executeIntermediateCommand(java.lang.String command)
                                                throws java.io.IOException,
                                                       IllegalFTPResponseException
Sends the specified command to the FTP server (expecting a positive intermediate response).

Parameters:
command - the command to send to the FTP server
Returns:
the response from the FTP server
Throws:
IllegalFTPResponseException - if a positive intermediate response is not received
java.io.IOException

executePreliminaryCommand

protected FTPServerResponse executePreliminaryCommand(java.lang.String command)
                                               throws java.io.IOException,
                                                      IllegalFTPResponseException
Sends the specified command to the FTP server (expecting a positive preliminary response).

Parameters:
command - the command to send to the FTP server
Returns:
the response from the FTP server
Throws:
IllegalFTPResponseException - if a positive preliminary response is not received
java.io.IOException

readDataToFile

protected void readDataToFile(java.lang.String command,
                              java.lang.String fileName)
                       throws java.io.IOException,
                              IllegalFTPResponseException
Sends the specified data command to the FTP server, transferring the results to the file at the specified path.

Parameters:
command - the command to send to the FTP server
fileName - the path to the destination file
Throws:
java.io.IOException
IllegalFTPResponseException

readDataToString

protected java.lang.String[] readDataToString(java.lang.String command)
                                       throws java.io.IOException,
                                              IllegalFTPResponseException
Sends the specified data command to the FTP server, returning the results as a String array.

Parameters:
command - the command to send to the FTP server
Returns:
a String array containing the results of the data command
Throws:
java.io.IOException
IllegalFTPResponseException

writeDataFromFile

protected void writeDataFromFile(java.lang.String command,
                                 java.lang.String fileName)
                          throws java.io.IOException,
                                 IllegalFTPResponseException
Sends the specified data command to the FTP server, then transfers the contents of the file at the specified path.

Parameters:
command - the command to send to the FTP server
fileName - the path to the source file
Throws:
java.io.IOException
IllegalFTPResponseException

executeDataCommand

protected void executeDataCommand(java.lang.String command,
                                  java.io.OutputStream out)
                           throws java.io.IOException,
                                  IllegalFTPResponseException
Sends the specified data command to the FTP server, transferring the results to the specified output stream.

Parameters:
command - the command to send to the FTP server
out - the destination output stream
Throws:
java.io.IOException
IllegalFTPResponseException

executeDataCommand

protected void executeDataCommand(java.lang.String command,
                                  java.io.InputStream in)
                           throws java.io.IOException,
                                  IllegalFTPResponseException
Sends the specified data command to the FTP server, then transfers the contents of the specified input stream.

Parameters:
command - the command to send to the FTP server
in - the source input stream
Throws:
java.io.IOException
IllegalFTPResponseException

setLineSeparator

public void setLineSeparator(java.lang.String lineSeparator)
Sets the line separator used for sending commands. Default value is the "line.separator" System property.

Parameters:
lineSeparator - the new line separator String

getDataSocket

private java.net.Socket getDataSocket()
                               throws java.io.IOException
Creates and returns a data socket between the client and the FTP server.

Returns:
a new data socket
Throws:
java.io.IOException

transferData

private void transferData(java.io.InputStream in,
                          java.io.OutputStream out)
                   throws java.io.IOException
Transfers data in one kilobyte blocks from the specified input stream to the specified output stream.

Parameters:
in - the source stream
out - the destination stream
Throws:
java.io.IOException

debugPrint

private void debugPrint(java.lang.String message)
When debug mode is on, prints the specified message to the System output stream.

Parameters:
message - the String to print

FTP Client