## net.sf.jftp.net
Class FtpConnection

    java.lang.Object
      net.sf.jftp.net.FtpConnection

All Implemented Interfaces:
  BasicConnection, FtpConstants

    public class FtpConnection

extends java.lang.Object
implements BasicConnection, FtpConstants

All control transactions are made here. See doc/FtpDownload.java for examples and note that this class is event-driven and not Exception-based! You might want to take a look at ConnectionListener, too.

Author:
  David Hansmann, hansmann.d@debitel.net

Field Summary
`static java.lang.String` `ASCII`
`static java.lang.String` `BINARY`
`static java.lang.String` `BLOCKED`
`static java.lang.String` `COMPRESSED`
`java.util.Vector<java.lang.String>` `currentFiles`
`java.util.Vector<java.lang.String>` `currentListing`
`java.util.Vector<java.lang.String>` `currentPerms`
`java.util.Vector<java.lang.String>` `currentSizes`
`java.util.Vector<java.util.Date>` `dateVector` May contain date listing
`static java.lang.String` `EBCDIC`
`boolean` `hasUploaded` Used to determine the type of transfer.
`static java.lang.String` `L8`
`static java.lang.String` `LIST`
`static java.lang.String` `LIST_DEFAULT`
`static java.lang.String` `STREAM`
`boolean` `work`

Fields inherited from interface net.sf.jftp.net.FtpConstants

`ABOR, ACCT, ADAT, ALLO, APPE, AUTH, CCC, CDUP, CHDIR_FAILED, CONF, CWD, DATACONNECTION_FAILED, DELE, DENIED, ENC, FILE_NOT_FOUND, FTP200_OK, FTP215_SYSTEM_TYPE, FTP220_SERVICE_READY, FTP221_SERVICE_CLOSING, FTP226_CLOSING_DATA_REQUEST_SUCCESSFUL, FTP227_ENTERING_PASSIVE_MODE, FTP230_LOGGED_IN, FTP250_COMPLETED, FTP257_PATH_CREATED, FTP331_USER_OK_NEED_PASSWORD, GENERIC_FAILED, HELP, LOGIN_OK, MIC, MKD, MKDIR_FAILED, MODE, NEW_TRANSFER_SPAWNED, NLST, NOOP, OFFLINE, PASS, PASV, PBSZ, PERMISSION_DENIED, PORT, PROT, PWD, QUIT, R, RC110, RC120, RC125, RC150, RC202, RC211, RC212, RC213, RC214, RC225, RC332, RC350, RC421, RC425, RC426, RC450, RC451, RC452, RC500, RC501, RC502, RC503, RC504, RC530, RC532, RC550, RC551, RC552, RC553, REIN, REMOVE_FAILED, REMOVE_SUCCESSFUL, RENAME_FAILED, REST, RETR, RMD, RNFR, RNTO, SITE, SMNT, STAT, STOR, STOU, STRU, SYST, TRANSFER_FAILED, TRANSFER_STOPPED, TRANSFER_SUCCESSFUL, TYPE, USER, W, WRONG_LOGIN_DATA`

Constructor Summary
`FtpConnection(java.lang.String host)` Create an instance with a given host
`FtpConnection(java.lang.String host, int port, java.lang.String initCWD)` Create an instance with a given host, port and initial remote working directory
`FtpConnection(java.lang.String host, int port, java.lang.String initCWD, java.lang.String crlfx)` Connect to the server an log in.

Method Summary
`void` `abort()` Try to abort the transfer.
`void` `abortTransfer()` Abort the last spawned transfer.
`void` `addConnectionListener(ConnectionListener l)` Add a ConnectionListener.
`void` `ascii()` Tell server we want ascii data connections.
`void` `binary()` Tell server we want binary data connections.
`boolean` `cdup()` Change to the parent of the current working directory.
`boolean` `chdir(java.lang.String p)` Parses directory and does a chdir().
`boolean` `chdirNoRefresh(java.lang.String p)` Parses directory and does a chdir(), but does not send an update signal
`boolean` `chdirRaw(java.lang.String dirName)` Change directory unparsed.
`void` `disconnect()` Disconnect from the server.
`int` `download(java.lang.String file)` Download a file or directory, block until finished.
`int` `exists(java.lang.String file)` Checks wheter a file exists.
`void` `fireActionFinished(FtpConnection con)` Transfer is done
`void` `fireConnectionFailed(FtpConnection con, java.lang.String why)` We are not logged in for some reason
`void` `fireConnectionInitialized(FtpConnection con)` Connection is there and user logged in
`void` `fireDirectoryUpdate(FtpConnection con)` Remote directory has changed.
`void` `fireProgressUpdate(java.lang.String file, java.lang.String type, long bytes)` Progress update.
`java.lang.String` `getCachedPWD()` Returns current remote directory (cached)
`java.io.BufferedReader` `getCommandInputReader()`
`java.io.OutputStream` `getCommandOutputStream()`
`ConnectionHandler` `getConnectionHandler()` Return the ConnectionHandler.
`java.lang.String` `getCRLF()`
`DataConnection` `getDataConnection()` Return the DataConnection.
`java.io.InputStream` `getDownloadInputStream(java.lang.String file)` Get download InputStream.
`java.lang.String` `getHost()` Return the host used.
`java.io.BufferedReader` `getIn()`
`FtpKeepAliveThread` `getKeepAliveThread()`
`FtpTransfer` `getLastInitiatedTransfer()` Get the last FtpTransfer object spawned.
`java.lang.String` `getLine(java.lang.String until)` Reads the response until line found or error.
`java.lang.String` `getLine(java.lang.String[] until)` Reads the response until line found or error.
`java.lang.String` `getLocalPath()` Return the local working directory
`java.lang.String` `getOsType()` Try to determine the os-type.
`java.lang.String` `getPassword()` Return the password.
`int[]` `getPermissions()` Sort the permissions an return an array.
`int` `getPort()` Get the port.
`java.lang.String` `getPWD()` Get the current remote directory.
`java.lang.String` `getTypeNow()` Returns the type used.
`java.lang.String` `getUsername()` Return the username.
`int` `handleDownload(java.lang.String file)` Download a file or directory.
`int` `handleUpload(java.lang.String file)` Upload a file or directory.
`int` `handleUpload(java.lang.String file, java.lang.String realName)` Upload a file or directory.
`boolean` `isConnected()` Check if login() was successful.
`void` `list()` List remote directory.
`int` `login(java.lang.String username, java.lang.String password)` Connect to the server an log in.
`boolean` `mkdir(java.lang.String dirName)` Create a directory with the given name.
`java.lang.String` `mode(java.lang.String code)` Set mode manually.
`void` `modeBlocked()` Unsupported at this time.
`void` `modeCompressed()`
`void` `modeStream()` Set mode to Stream.
`void` `noop()` Do nothing, but flush buffers
`void` `pause(int time)` Waits a specified amount of time
`int` `removeFileOrDir(java.lang.String file)` Remove a remote file or directory.
`boolean` `rename(java.lang.String from, java.lang.String to)` Moves or renames remote file.
`void` `sendRawCommand(java.lang.String cmd)` Execute a remote command.
`void` `setConnectionHandler(ConnectionHandler h)` Set the ConnectionHandler.
`void` `setConnectionListeners(java.util.Vector<ConnectionListener> l)` Add a Vector of ConnectionListeners.
`void` `setIn(java.io.BufferedReader in)`
`boolean` `setLocalPath(java.lang.String newPath)` Set the local working directory
`java.util.Date[]` `sortDates()`
`java.lang.String[]` `sortLs()` Sort the filenames of the current working dir an return an array.
`java.lang.String[]` `sortSize()` Sort the filesizes an return an array.
`java.lang.String` `system()` This command is used to find out the type of operating system at the server.
`boolean` `type(java.lang.String code)` Set type (L8,I,A for example).
`int` `upload(java.lang.String file)` Upload a file or directory, block until finished.
`int` `upload(java.lang.String file, java.io.InputStream in)` Upload from an InputStream, block until finished.
`int` `upload(java.lang.String file, java.lang.String realName)` Upload and a file or directory under a given name, block until finished.
`int` `upload(java.lang.String file, java.lang.String realName, java.io.InputStream in)` Upload and a file or directory under a given name, block until finished.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### ASCII

    public static final java.lang.String ASCII

See Also:
  Constant Field Values

### BINARY

    public static final java.lang.String BINARY

See Also:
  Constant Field Values

### EBCDIC

    public static final java.lang.String EBCDIC

See Also:
  Constant Field Values

### L8

    public static final java.lang.String L8

See Also:
  Constant Field Values

### STREAM

    public static final java.lang.String STREAM

See Also:
  Constant Field Values

### BLOCKED

    public static final java.lang.String BLOCKED

See Also:
  Constant Field Values

### COMPRESSED

    public static final java.lang.String COMPRESSED

See Also:
  Constant Field Values

### LIST_DEFAULT

    public static java.lang.String LIST_DEFAULT

### LIST

    public static java.lang.String LIST

### hasUploaded

    public boolean hasUploaded

  Used to determine the type of transfer.

### work

    public boolean work

### dateVector

    public java.util.Vector<java.util.Date> dateVector

  May contain date listing

### currentListing

    public java.util.Vector<java.lang.String> currentListing

### currentFiles

    public java.util.Vector<java.lang.String> currentFiles

### currentSizes

    public java.util.Vector<java.lang.String> currentSizes

### currentPerms

    public java.util.Vector<java.lang.String> currentPerms

Constructor Detail

### FtpConnection

    public FtpConnection(java.lang.String host)

  Create an instance with a given host

  Parameters:
    `host` - The host to connect to

### FtpConnection

    public FtpConnection(java.lang.String host,
                         int port,
                         java.lang.String initCWD)

  Create an instance with a given host, port and initial remote working directory

  Parameters:
    `host` - The host to connect to
    `port` - The port used for the control connection, usually 21
    `initCWD` - The initial remote working directory

### FtpConnection

    public FtpConnection(java.lang.String host,
                         int port,
                         java.lang.String initCWD,
                         java.lang.String crlfx)

  Connect to the server an log in. Does also fire directory update and connection initialized event if successful or connection failed event if failed.

  Parameters:
    `username` - The username
    `password` - The password
    `initCWD` - The initial remote working directory
    `crlfx` - \n, \r, \r\n, CR, LF or CRLF - line break style of the server

Method Detail

### login

    public int login(java.lang.String username,
                     java.lang.String password)

  Connect to the server an log in. Does also fire directory update and connection initialized event if successful or connection failed event if failed.

  Parameters:
    `username` - The username
    `password` - The password
  Returns:
    WRONG_LOGIN_DATA, OFFLINE, GENERIC_FAILED or LOGIN_OK status code

### getKeepAliveThread

    public FtpKeepAliveThread getKeepAliveThread()

### sortSize

    public java.lang.String[] sortSize()

  Sort the filesizes an return an array. The Array should be in sync with the other sort*-Methods

  Specified by:
    `sortSize` in interface `BasicConnection`
  Returns:
    An String-array of sizes containing the size or -1 if failed or 0 for directories

### getPermissions

    public int[] getPermissions()

  Sort the permissions an return an array. The Array should be in sync with the other sort*-Methods

  Specified by:
    `getPermissions` in interface `BasicConnection`
  Parameters:
    `file` - The file containing the raw server listing, usually Settings.ls_out
  Returns:
    An int-array of sizes containing W, R or DENIED for each file

### sortLs

    public java.lang.String[] sortLs()

  Sort the filenames of the current working dir an return an array. The Array should be in sync with the other sort*-Methods

  Specified by:
    `sortLs` in interface `BasicConnection`
  Returns:
    An String-array of sizes containing the name of the file (symlinks are resolved)

### getOsType

    public java.lang.String getOsType()

  Try to determine the os-type. (Used internally to check how to parse the ls-output)

  Returns:
    A Part of the SYST comman server response

### handleDownload

    public int handleDownload(java.lang.String file)

  Download a file or directory. Uses multithreading if enabled and does not block.

  Specified by:
    `handleDownload` in interface `BasicConnection`
  Parameters:
    `file` - The file to download
  Returns:
    An int-statuscode, see constants defined in this class for details

### download

    public int download(java.lang.String file)

  Download a file or directory, block until finished.

  Specified by:
    `download` in interface `BasicConnection`
  Parameters:
    `file` - The file to download
  Returns:
    An int returncode

### getDownloadInputStream

    public java.io.InputStream getDownloadInputStream(java.lang.String file)

  Get download InputStream.

  Specified by:
    `getDownloadInputStream` in interface `BasicConnection`
  Parameters:
    `file` - The file to download
  Returns:
    An InputStream

### handleUpload

    public int handleUpload(java.lang.String file)

  Upload a file or directory. Uses multithreading if enabled and does not block.

  Specified by:
    `handleUpload` in interface `BasicConnection`
  Parameters:
    `file` - The file to upload
  Returns:
    An int-statuscode, NEW_TRANSFER_SPAWNED,TRANSFER_FAILED or TRANSFER_SUCCESSFUL

### handleUpload

    public int handleUpload(java.lang.String file,
                            java.lang.String realName)

  Upload a file or directory. Uses multithreading if enabled and does not block.

  Parameters:
    `file` - The file to upload
    `realName` - The file to rename the uploaded file to
  Returns:
    An int-statuscode, NEW_TRANSFER_SPAWNED,TRANSFER_FAILED or TRANSFER_SUCCESSFUL

### upload

    public int upload(java.lang.String file)

  Upload a file or directory, block until finished.

  Specified by:
    `upload` in interface `BasicConnection`
  Parameters:
    `file` - The file to upload
  Returns:
    An int returncode

### upload

    public int upload(java.lang.String file,
                      java.lang.String realName)

  Upload and a file or directory under a given name, block until finished. Note that setting realName does not affect directory transfers

  Parameters:
    `file` - The file to upload
    `realName` - The file to rename the uploaded file to
  Returns:
    An int responsecode

### upload

    public int upload(java.lang.String file,
                      java.io.InputStream in)

  Upload from an InputStream, block until finished.

  Specified by:
    `upload` in interface `BasicConnection`
  Parameters:
    `file` - The file to upload
    `in` - InputStream to read from
  Returns:
    An int responsecode

### upload

    public int upload(java.lang.String file,
                      java.lang.String realName,
                      java.io.InputStream in)

  Upload and a file or directory under a given name, block until finished. Note that setting realName does not affect directory transfers

  Parameters:
    `file` - The file to upload
    `realName` - The file to rename the uploaded file to
    `in` - InputStream to read from
  Returns:
    An int responsecode

### removeFileOrDir

    public int removeFileOrDir(java.lang.String file)

  Remove a remote file or directory.

  Specified by:
    `removeFileOrDir` in interface `BasicConnection`
  Parameters:
    `file` - The file to remove
  Returns:
    REMOVE_SUCCESSFUL, REMOVE_FAILED, PERMISSION_DENIED or GENERIC_FAILED

### disconnect

    public void disconnect()

  Disconnect from the server. The connection is marked ad closed even if the server does not respond correctly - if it fails for any reason the connection should just time out

  Specified by:
    `disconnect` in interface `BasicConnection`

### sendRawCommand

    public void sendRawCommand(java.lang.String cmd)

  Execute a remote command. Sends noops before and after the command

  Specified by:
    `sendRawCommand` in interface `BasicConnection`
  Parameters:
    `cmd` - The raw command that is to be sent to the server

### getLine

    public java.lang.String getLine(java.lang.String until)

  Reads the response until line found or error. (Used internally)

  Parameters:
    `until` - The String the response line hast to start with, 2 for succesful return codes for example

### getLine

    public java.lang.String getLine(java.lang.String[] until)

  Reads the response until line found or error. (Used internally)

### isConnected

    public boolean isConnected()

  Check if login() was successful.

  Specified by:
    `isConnected` in interface `BasicConnection`
  Returns:
    True if connected, false otherwise

### getPWD

    public java.lang.String getPWD()

  Get the current remote directory.

  Specified by:
    `getPWD` in interface `BasicConnection`
  Returns:
    The server's CWD

### getCachedPWD

    public java.lang.String getCachedPWD()

  Returns current remote directory (cached)

  Returns:
    The cached CWD

### chdirRaw

    public boolean chdirRaw(java.lang.String dirName)

  Change directory unparsed. Use chdir instead if you do not parse the dir correctly...

  Parameters:
    `dirName` - The raw directory name
  Returns:
    True if successful, false otherwise

### cdup

    public boolean cdup()

  Change to the parent of the current working directory. The CDUP command is a special case of CWD, and is included to simplify the implementation of programs for transferring directory trees between operating systems having different syntaxes for naming the parent directory.

  Specified by:
    `cdup` in interface `BasicConnection`
  Returns:
    True if successful, false otherwise

### mkdir

    public boolean mkdir(java.lang.String dirName)

  Create a directory with the given name.

  Specified by:
    `mkdir` in interface `BasicConnection`
  Parameters:
    `dirName` - The name of the directory to create
  Returns:
    True if successful, false otherwise

### list

    public void list()
              throws java.io.IOException

  List remote directory. Note that you have to get the output using the sort*-methods.

  Specified by:
    `list` in interface `BasicConnection`
  Parameters:
    `outfile` - The file to save the output to, usually Settings.ls_out
  Throws:
    `java.io.IOException`

### chdir

    public boolean chdir(java.lang.String p)

  Parses directory and does a chdir(). Does also fire a directory update event.

  Specified by:
    `chdir` in interface `BasicConnection`
  Returns:
    True is successful, false otherwise

### chdirNoRefresh

    public boolean chdirNoRefresh(java.lang.String p)

  Parses directory and does a chdir(), but does not send an update signal

  Specified by:
    `chdirNoRefresh` in interface `BasicConnection`
  Returns:
    True is successful, false otherwise

### pause

    public void pause(int time)

  Waits a specified amount of time

  Parameters:
    `time` - The time to sleep in milliseconds

### getLocalPath

    public java.lang.String getLocalPath()

  Return the local working directory

  Specified by:
    `getLocalPath` in interface `BasicConnection`
  Returns:
    The local CWD

### setLocalPath

    public boolean setLocalPath(java.lang.String newPath)

  Set the local working directory

  Specified by:
    `setLocalPath` in interface `BasicConnection`
  Parameters:
    `newPath` - The new local CWD
  Returns:
    Always true in the current implementation

### exists

    public int exists(java.lang.String file)

  Checks wheter a file exists.

  Parameters:
    `file` - the name of the file
  Returns:
    An int-code: CHDIR_FAILED, PERMISSION_DENIED (no listing allowed), R, W or DENIED

### rename

    public boolean rename(java.lang.String from,
                          java.lang.String to)

  Moves or renames remote file.

  Specified by:
    `rename` in interface `BasicConnection`
  Parameters:
    `from` - remote file to move or rename.
    `to` - new file name.
  Returns:
    `true` if completed successfully; `false` otherwise.

### getPort

    public int getPort()

  Get the port.

  Returns:
    The port used by the FTP control connection

### binary

    public void binary()

  Tell server we want binary data connections.

### ascii

    public void ascii()

  Tell server we want ascii data connections.

### type

    public boolean type(java.lang.String code)

  Set type (L8,I,A for example).

  Returns:
    True if the type was changed successfully, false otherwise

### getTypeNow

    public java.lang.String getTypeNow()

  Returns the type used.

  Returns:
    The type String, I, A, L8 for example

### noop

    public void noop()

  Do nothing, but flush buffers

### abort

    public void abort()

  Try to abort the transfer.

### system

    public java.lang.String system()

  This command is used to find out the type of operating system at the server. The reply shall have as its first word one of the system names listed in the current version of the Assigned Numbers document (RFC 943).

  Returns:
    The server response of the SYST command

### modeStream

    public void modeStream()

  Set mode to Stream. Used internally.

### modeBlocked

    public void modeBlocked()

  Unsupported at this time.

### modeCompressed

    public void modeCompressed()

### mode

    public java.lang.String mode(java.lang.String code)

  Set mode manually.

  Parameters:
    `code` - The mode code wanted, I, A, L8 for example
  Returns:
    The server's response to the request

### getHost

    public java.lang.String getHost()

  Return the host used.

  Returns:
    The remote host of this connection

### getUsername

    public java.lang.String getUsername()

  Return the username.

  Returns:
    The username used by this connection

### getPassword

    public java.lang.String getPassword()

  Return the password.

  Returns:
    The password used by this connection

### getDataConnection

    public DataConnection getDataConnection()

  Return the DataConnection. Should be necessary only for complex actions.

  Returns:
    The DataConnection object currently used by this conenction

### addConnectionListener

    public void addConnectionListener(ConnectionListener l)

  Add a ConnectionListener. The Listener is notified about transfers and the connection state.

  Specified by:
    `addConnectionListener` in interface `BasicConnection`
  Parameters:
    `l` - A ConnectionListener object

### setConnectionListeners

    public void setConnectionListeners(java.util.Vector<ConnectionListener> l)

  Add a Vector of ConnectionListeners. Each Listener is notified about transfers and the connection state.

  Specified by:
    `setConnectionListeners` in interface `BasicConnection`
  Parameters:
    `l` - A Vector containig ConnectionListener objects

### fireDirectoryUpdate

    public void fireDirectoryUpdate(FtpConnection con)

  Remote directory has changed.

  Parameters:
    `con` - The FtpConnection calling the event

### fireProgressUpdate

    public void fireProgressUpdate(java.lang.String file,
                                   java.lang.String type,
                                   long bytes)

  Progress update. The transfer is active and makes progress, so a signal is send each few kb.

  Parameters:
    `file` - The file transferred
    `type` - the type of event, DataConnection.GETDIR for example
    `bytes` - The number of bytes transferred so far

### fireConnectionInitialized

    public void fireConnectionInitialized(FtpConnection con)

  Connection is there and user logged in

  Parameters:
    `con` - The FtpConnection calling the event

### fireConnectionFailed

    public void fireConnectionFailed(FtpConnection con,
                                     java.lang.String why)

  We are not logged in for some reason

  Parameters:
    `con` - The FtpConnection calling the event
    `why` - The login() response code

### fireActionFinished

    public void fireActionFinished(FtpConnection con)

  Transfer is done

  Parameters:
    `con` - The FtpConnection calling the event

### getConnectionHandler

    public ConnectionHandler getConnectionHandler()

  Return the ConnectionHandler.

  Returns:
    The connection handler of this instance

### setConnectionHandler

    public void setConnectionHandler(ConnectionHandler h)

  Set the ConnectionHandler.

  Parameters:
    `h` - The connection handler that is to be used by this connection

### getLastInitiatedTransfer

    public FtpTransfer getLastInitiatedTransfer()

  Get the last FtpTransfer object spawned.

### abortTransfer

    public void abortTransfer()

  Abort the last spawned transfer.

### sortDates

    public java.util.Date[] sortDates()

  Specified by:
    `sortDates` in interface `BasicConnection`

### getCRLF

    public java.lang.String getCRLF()

### getIn

    public java.io.BufferedReader getIn()

### setIn

    public void setIn(java.io.BufferedReader in)

### getCommandInputReader

    public java.io.BufferedReader getCommandInputReader()

### getCommandOutputStream

    public java.io.OutputStream getCommandOutputStream()