Java Globus API: Class FTPClient

org.globus.io.ftp
Class FTPClient

java.lang.Object
  |
  +--org.globus.io.ftp.FTPCommon
        |
        +--org.globus.io.ftp.FTPClient
Direct Known Subclasses:
GSIFTPClient

public class FTPClient
extends FTPCommon

This class provides an implementation of common ftp functions such as put(), get(), mget(), mput(), list(). It also provides more sophisticated functions such as third-party transfers. FTP NOTES:

  • list() is server depended and might not work on some ftp implementations.
  • recursive mget() or copy() functions rely on the list() function to obtain the list of directories. Since, the list() function is not server independed, these functions might not work correctly. Non-recursive mget() or copy() should work on any server.
  • Transfer restarts are not implemented because we only support stream mode so far.
  • Third-party transfers need to be more tested for better error recovery.


Field Summary
protected  java.text.SimpleDateFormat _dateFormat
           
static int BUFF_SIZE
           
protected  int bufferSize
           
protected  boolean compatibilityMode
           
protected  int dataChannelPort
           
protected  int localBufferSize
           
protected  boolean passiveMode
           
protected  int type
           
 
Fields inherited from class org.globus.io.ftp.FTPCommon
ASCII, BINARY, ftpin, ftpout, host, NLST, PERMANENT_NEGATIVE_REPLY, port, POSITIVE_COMPLETION_REPLY, POSITIVE_INTERMEDIATE_REPLY, POSITIVE_PRELIMINARY_REPLY, requestThread, RETR, socket, TIMEOUT, TRANSIENT_NEGATIVE_REPLY
 
Constructor Summary
FTPClient(java.lang.String host, int port)
           
 
Method Summary
 void changeDir(java.lang.String dir)
           
protected static void closeSockets(java.net.Socket data, java.net.ServerSocket server)
           
protected static void closeStreams(java.io.InputStream in, java.io.OutputStream out)
           
 void copy(java.lang.String remoteFilter, FTPClient dstServer, boolean recursive, TransferProgressListener listener)
          Performs a multiple third-party transfers between two servers.
 void copy(java.lang.String remoteSrcFile, FTPClient dstServer, java.lang.String remoteDstFile, boolean append, TransferProgressListener listener)
          Performs a third-party transfer between two servers.
 void deleteDir(java.lang.String dir)
           
 void deleteFile(java.lang.String filename)
           
 java.net.Socket executeStreamCommand(java.lang.String command, java.lang.String args)
           
 boolean exists(java.lang.String filename)
          Checks if given file/directory exists on the server.
 java.lang.String[] features()
           
 void get(java.lang.String remoteFile, java.io.File localFile)
          Retreives a file from ftp server.
 void get(java.lang.String remoteFile, java.io.File localFile, TransferProgressListener listener)
          Retreives a file from ftp server and saves it in a local file.
 int getBufferSize()
          Returns current buffer size.
 java.lang.String getCurrentDir()
           
 int getLocalBufferSize()
          Returns local buffer size.
 void goUpDir()
           
 boolean isFeatureSupported(java.lang.String feature)
           
 boolean isInCompatibilityMode()
          Returns if the old GSI FTPD compatibility is enabled.
 boolean isPassiveMode()
           
 java.util.Date lastModified(java.lang.String filename)
          Returns last modification time of the specifed file.
 java.util.Vector list()
          Performs list function.
 java.util.Vector list(java.lang.String filter)
          Performs list function with specified filter.
 void makeDir(java.lang.String dir)
           
 void mGet(java.lang.String remoteFilter, java.io.File localDir, boolean recursive, TransferProgressListener listener)
          Retreives multiple files from ftp server.
 void mPut(java.io.File localDir, java.lang.String filter, boolean recursive, TransferProgressListener listener)
          Copies multiple files from specified directory to ftp server.
 java.util.Vector nlist(java.lang.String filter)
          Performs nlist function with specified filter.
 void put(java.io.File localFile, java.lang.String remoteFile, boolean append)
          Copies a local file to ftp server.
 void put(java.io.File localFile, java.lang.String remoteFile, boolean append, TransferProgressListener listener)
          Copies a local file to ftp server.
 void rename(java.lang.String oldName, java.lang.String newName)
           
 void setBufferSize(int size)
          Sets buffer size used for transfering data.
 void setCompatibilityMode(boolean compatibility)
          Sets the compatibility mode to work with the old GSI FTPD implementations (non-wuftp).
 void setDataChannelPort(int port)
          Sets the default port used for data connection.
 void setLocalBufferSize(int size)
          Sets the local buffer size.
 void setPassiveMode(boolean passiveMode)
          Enables/disables passive data connections.
 void setRemoteBufferSize(int size)
          Sets the remote buffer size.
protected  void setRestartMarker(int offset)
          Supplies a restart marker for stream mode to the server
protected  void setSocketBufferSize(java.net.Socket socket)
          Sets the socket TCP buffer sizes.
 void setType(int type)
           
protected  HostPort setupServers(FTPClient src, FTPClient dst)
           
 long size(java.lang.String filename)
          Returns size of the specifed file.
protected  void transfer(int totalBytes, java.lang.String from, java.io.InputStream in, java.lang.String to, java.io.OutputStream out, TransferProgressListener listener)
           
protected  void transferASCII(int totalBytes, java.lang.String from, java.io.InputStream inn, java.lang.String to, java.io.OutputStream outt, byte[] lineSep, TransferProgressListener listener)
           
protected  void transferData(boolean get, int totalBytes, java.lang.String from, java.io.InputStream in, java.lang.String to, java.io.OutputStream out, TransferProgressListener listener)
           
 
Methods inherited from class org.globus.io.ftp.FTPCommon
abort, authenticate, authenticate, checkResponse, close, closeSocket, connect, disconnect, doCmd, doCmd, executeCommand, getControlSocket, getReply, getRequestThread, getResponse, isPositiveComplete, isPositivePreliminary, isReplyType, readLine, sendFileCommand, setPassiveMode, setPort, startDataChannel, startDataChannel, startPassiveDataChannel, write, writeln
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUFF_SIZE

public static int BUFF_SIZE

passiveMode

protected boolean passiveMode

dataChannelPort

protected int dataChannelPort

bufferSize

protected int bufferSize

compatibilityMode

protected boolean compatibilityMode

localBufferSize

protected int localBufferSize

_dateFormat

protected java.text.SimpleDateFormat _dateFormat

type

protected int type
Constructor Detail

FTPClient

public FTPClient(java.lang.String host,
                 int port)
          throws java.io.IOException,
                 FTPException
Method Detail

setBufferSize

public void setBufferSize(int size)
Sets buffer size used for transfering data. Note: This does not set the TCP buffer sizes!
Parameters:
size - buffer size

getBufferSize

public int getBufferSize()
Returns current buffer size.
Returns:
buffer size

setDataChannelPort

public void setDataChannelPort(int port)
Sets the default port used for data connection. It is only used in non-passive mode.
Parameters:
port - port number. If set to 0, the port number will be automatically asigned.

setPassiveMode

public void setPassiveMode(boolean passiveMode)
Enables/disables passive data connections.
Parameters:
passiveMode, - if true passive connections will be established. If false, they will not.

isPassiveMode

public boolean isPassiveMode()

closeSockets

protected static void closeSockets(java.net.Socket data,
                                   java.net.ServerSocket server)

closeStreams

protected static void closeStreams(java.io.InputStream in,
                                   java.io.OutputStream out)

setCompatibilityMode

public void setCompatibilityMode(boolean compatibility)
Sets the compatibility mode to work with the old GSI FTPD implementations (non-wuftp). Note: Only applies to the setRemoteBufferSize() method.
Parameters:
compatibility - set to true if working with the old GSI FTPD server.

isInCompatibilityMode

public boolean isInCompatibilityMode()
Returns if the old GSI FTPD compatibility is enabled.
Returns:
true if compatibility mode is on, otherwise false.

setRemoteBufferSize

public void setRemoteBufferSize(int size)
                         throws java.io.IOException,
                                FTPException
Sets the remote buffer size. Note: This function is server depended. Enable the comatibility mode is dealing with the old GSI FTPD implementation.
Parameters:
size - buffer size in bytes.

setLocalBufferSize

public void setLocalBufferSize(int size)
Sets the local buffer size. Note: The value is only a hint to the network I/O the actual TCP buffer size might be different.
Parameters:
size - suggested buffer size in bytes.

getLocalBufferSize

public int getLocalBufferSize()
Returns local buffer size. Note: The local buffer size might be different from the buffer size set through setLocalBufferSize() since the value if only a hint to the underlying network I/O.
Returns:
local buffer size. 0 if not set.

setSocketBufferSize

protected void setSocketBufferSize(java.net.Socket socket)
                            throws java.net.SocketException
Sets the socket TCP buffer sizes.
Parameters:
socket - Socket to set buffer sizes of

size

public long size(java.lang.String filename)
          throws java.io.IOException,
                 FTPException
Returns size of the specifed file.
Parameters:
filename - filename get the size for.
Returns:
size of the file.
Throws:
FTPException - if the file does not exist or an error occured.

lastModified

public java.util.Date lastModified(java.lang.String filename)
                            throws java.io.IOException,
                                   FTPException
Returns last modification time of the specifed file.
Parameters:
filename - filename get the last modification time for.
Returns:
the time and date of the last modification.
Throws:
FTPException - if the file does not exist or an error occured.

exists

public boolean exists(java.lang.String filename)
               throws java.io.IOException,
                      FTPException
Checks if given file/directory exists on the server.
Parameters:
filename - file or directory name
Returns:
true if the file exists, false otherwise.

list

public java.util.Vector list()
                      throws java.io.IOException,
                             FTPException
Performs list function. It sends 'LIST -d' command. Note: This function can only parse Unix ls -d output. If 'ls' is executed without -d option this function might fail. Also 'ls -d' output might be different on each architecture.
Returns:
Vector list of FileInfo objects

list

public java.util.Vector list(java.lang.String filter)
                      throws java.io.IOException,
                             FTPException
Performs list function with specified filter. It sends 'LIST -d' command. Performs list function. It sends 'LIST -d' command. Note: This function can only parse Unix ls -d output. If 'ls' is executed without -d option this function might fail. Also 'ls -d' output might be different on each architecture.
Returns:
Vector list of FileInfo objects that satisfy given filter.

nlist

public java.util.Vector nlist(java.lang.String filter)
                       throws java.io.IOException,
                              FTPException
Performs nlist function with specified filter. It sends 'NLST' command that should be ftpd implementation independed. It is used for non-recursive mget() and copy() functions.
Returns:
Vector list of FileInfo objects that satisfy given filter. The FileInfo objects will have no detailed info such as file size, owner, etc.

put

public void put(java.io.File localFile,
                java.lang.String remoteFile,
                boolean append)
         throws FTPException,
                java.io.IOException
Copies a local file to ftp server.
Parameters:
localFile - local file
remoteFile - remote file name
append - append mode
Throws:
java.io.IOException - if local file does not exist or specified file is a directory. Also, when IO problems on the ftp server.

put

public void put(java.io.File localFile,
                java.lang.String remoteFile,
                boolean append,
                TransferProgressListener listener)
         throws FTPException,
                java.io.IOException
Copies a local file to ftp server.
Parameters:
localFile - local file
remoteFile - remote file name
append - append mode
listener - transfer progress listener. Can be set to null.
Throws:
java.io.IOException - if local file does not exist or specified file is a directory. Also, when IO problems on the ftp server.

get

public void get(java.lang.String remoteFile,
                java.io.File localFile)
         throws FTPException,
                java.io.IOException
Retreives a file from ftp server.
Parameters:
remoteFile - remote file name
localFile - local file name. If a file already exists it will be overwritten.
Throws:
java.io.IOException - if the specified file is a directory or any other I/O problems occur during the transfer.

get

public void get(java.lang.String remoteFile,
                java.io.File localFile,
                TransferProgressListener listener)
         throws FTPException,
                java.io.IOException
Retreives a file from ftp server and saves it in a local file.
Parameters:
remoteFile - remote file name
localFile - local file name. If a file already exists it will be overwritten.
listener - transfer progress listener. Can be set to null.
Throws:
java.io.IOException - if the specified file is a directory or any other I/O problems occur during the transfer.

executeStreamCommand

public java.net.Socket executeStreamCommand(java.lang.String command,
                                            java.lang.String args)
                                     throws java.io.IOException,
                                            FTPException

mGet

public void mGet(java.lang.String remoteFilter,
                 java.io.File localDir,
                 boolean recursive,
                 TransferProgressListener listener)
          throws java.io.IOException,
                 FTPException
Retreives multiple files from ftp server. Retreives files based on specified filter from current directory.
Parameters:
remoteFilter - file filter e.g. *.txt
localDir - local directory to store the files in.
recursive - specifies option to traverse all directories.
listener - transfer progress listener.
Throws:
java.io.IOException - if local directory does not exist or specified directory is really a file.

mPut

public void mPut(java.io.File localDir,
                 java.lang.String filter,
                 boolean recursive,
                 TransferProgressListener listener)
          throws java.io.IOException,
                 FTPException
Copies multiple files from specified directory to ftp server. Files are copied to the current directory on the server.
Parameters:
localDir - local directory of source files.
filter - file filter. IGNORED.
recursive - specifies option to traverse all directories in the source directory.
listener - transfer progress listener.
Throws:
java.io.IOException - if local directory does not exist or specified directory is really a file.

setupServers

protected HostPort setupServers(FTPClient src,
                                FTPClient dst)
                         throws java.io.IOException,
                                FTPException

copy

public void copy(java.lang.String remoteSrcFile,
                 FTPClient dstServer,
                 java.lang.String remoteDstFile,
                 boolean append,
                 TransferProgressListener listener)
          throws java.io.IOException,
                 FTPException
Performs a third-party transfer between two servers. Note: This needs to be tested a lot for recovery from errors.
Parameters:
remoteSrcFile - source filename
dstServer - destination server
remoteDstFile - destination filename
append - enables append mode, if true file will be appened.
listener - transer progress listener. Can be set to null.

copy

public void copy(java.lang.String remoteFilter,
                 FTPClient dstServer,
                 boolean recursive,
                 TransferProgressListener listener)
          throws java.io.IOException,
                 FTPException
Performs a multiple third-party transfers between two servers. Files are copied from the current directory of the source server to the current directory of the destination server. Note: This needs to be tested a lot for recovery from errors.
Parameters:
remoteFilter - file filter to apply, e.g. *.txt
dstServer - destination server
recursive - specifies option to traverse all directories in the source directory.
listener - transer progress listener. Can be set to null.

setRestartMarker

protected void setRestartMarker(int offset)
                         throws java.io.IOException,
                                FTPException
Supplies a restart marker for stream mode to the server
Parameters:
next - byte in the file stream to start transfering
Throws:
FTPException - if the file does not exist or an error occured.

transferData

protected void transferData(boolean get,
                            int totalBytes,
                            java.lang.String from,
                            java.io.InputStream in,
                            java.lang.String to,
                            java.io.OutputStream out,
                            TransferProgressListener listener)
                     throws java.io.IOException

transferASCII

protected void transferASCII(int totalBytes,
                             java.lang.String from,
                             java.io.InputStream inn,
                             java.lang.String to,
                             java.io.OutputStream outt,
                             byte[] lineSep,
                             TransferProgressListener listener)
                      throws java.io.IOException

transfer

protected void transfer(int totalBytes,
                        java.lang.String from,
                        java.io.InputStream in,
                        java.lang.String to,
                        java.io.OutputStream out,
                        TransferProgressListener listener)
                 throws java.io.IOException

isFeatureSupported

public boolean isFeatureSupported(java.lang.String feature)

features

public java.lang.String[] features()
                            throws java.io.IOException,
                                   FTPException

changeDir

public void changeDir(java.lang.String dir)
               throws java.io.IOException,
                      FTPException

deleteDir

public void deleteDir(java.lang.String dir)
               throws java.io.IOException,
                      FTPException

deleteFile

public void deleteFile(java.lang.String filename)
                throws java.io.IOException,
                       FTPException

makeDir

public void makeDir(java.lang.String dir)
             throws java.io.IOException,
                    FTPException

rename

public void rename(java.lang.String oldName,
                   java.lang.String newName)
            throws java.io.IOException,
                   FTPException

getCurrentDir

public java.lang.String getCurrentDir()
                               throws java.io.IOException,
                                      FTPException

goUpDir

public void goUpDir()
             throws java.io.IOException,
                    FTPException

setType

public void setType(int type)
             throws java.io.IOException,
                    FTPException