com.myjavaworld.ftp
Class DataConnection

java.lang.Object
  extended by com.myjavaworld.ftp.DataConnection
All Implemented Interfaces:
FTPConstants
Direct Known Subclasses:
SSLDataConnection

public class DataConnection
extends Object
implements FTPConstants

A DataConnection object is used to transfer data over the data connection in an FTP process. FTPClient object will initiate a DataConnection based on the commands the send to the remote host. For more details about data connection, refer to the FTP protocol specification (RFC 959).


Field Summary
protected  boolean abort
          A flag for aborting the data transfer.
protected  FTPClient client
          A reference to the FTPClient that created this DataConnection.
protected  InputStream in
          An InputStream object for reading binary data either from the remote host or from the local file system.
protected  EventListenerList listenerList
          A List of registered listeners that are interested in receiving notifications about the activities of this DataConnection.
protected  OutputStream out
          An OutputStream object used to write binary data to the remote host or to the local file system.
protected  ServerSocket server
          A ServerSocket object used in active mode of data transfers.
protected  Socket socket
          A Socket that represents a data connection.
 
Fields inherited from interface com.myjavaworld.ftp.FTPConstants
DEFAULT_BUFFER_SIZE, DEFAULT_IMPLICIT_SSL_PORT, DEFAULT_MODE, DEFAULT_PORT, DEFAULT_STRUCTURE, DEFAULT_TIMEOUT, DEFAULT_TYPE, EOL, MODE_BLOCK, MODE_COMPRESSED, MODE_STREAM, STRUCTURE_FILE, STRUCTURE_PAGE, STRUCTURE_RECORD, TYPE_ASCII, TYPE_BINARY, TYPE_EBCDIC, TYPE_IMAGE, TYPE_LOCAL, USE_EXPLICIT_SSL, USE_IMPLICIT_SSL, USE_NO_SSL, USE_SSL_IF_AVAILABLE
 
Constructor Summary
DataConnection(FTPClient client)
          Constructs a DataConnection object.
 
Method Summary
 void abort()
          Sets the abort flag to true.
 void accept()
          Listens for connections.
 int bind()
          Binds a server socket on the local host on a free port.
 void close()
          Closes this data connection and open streams, if any.
 void connect(InetAddress address, int port)
          Connects to the specified InetAddress to the specified port number.
 void connect(String ipAddress, int port)
          Connects to the specified IP address to the specified port number.
 void download(File destination, boolean append)
          Reads the stream over this data connection and saves it in the specified local file destination.
protected  void fireDataTransferAborted(DataConnectionEvent evt)
          Notifies the registered listeners that the data transfer has aborted.
protected  void fireDataTransferFinished(DataConnectionEvent evt)
          Notifies the registered listeners that the data transfer has finished.
protected  void fireDataTransferProgress(DataConnectionEvent evt)
          Notifies the registered listeners that the data transfer is in progress.
protected  void fireDataTransferStarted(DataConnectionEvent evt)
          Notifies the registered listeners that the data transfer has started.
 boolean isAborted()
          Checks to see if the abort flag was set.
 RemoteFile[] list(RemoteFile dir)
          Parses the data received over this data connection to an array of RemoteFile objects.
protected  void stdout(String message)
          Writes the given message to the standard output.
 void upload(File source, long skip)
          Sends the contents of the specified local file src to the remote host.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

client

protected FTPClient client
A reference to the FTPClient that created this DataConnection.


server

protected ServerSocket server
A ServerSocket object used in active mode of data transfers.


socket

protected Socket socket
A Socket that represents a data connection.


in

protected InputStream in
An InputStream object for reading binary data either from the remote host or from the local file system.


out

protected OutputStream out
An OutputStream object used to write binary data to the remote host or to the local file system.


listenerList

protected EventListenerList listenerList
A List of registered listeners that are interested in receiving notifications about the activities of this DataConnection.


abort

protected boolean abort
A flag for aborting the data transfer.

Constructor Detail

DataConnection

public DataConnection(FTPClient client)
Constructs a DataConnection object.

Parameters:
client - FTPClient that created this data connection.
Method Detail

bind

public int bind()
         throws ConnectionException
Binds a server socket on the local host on a free port. This server socket is used for transmitting data in active mode.

Returns:
the port number to which this server is bound to.
Throws:
ConnectionException - If could not bind a server.

accept

public void accept()
            throws ConnectionException
Listens for connections. This method blocks until a connection is made or a timeout occurs.

Throws:
ConnectionException - If a network or IO error occurs.

connect

public void connect(String ipAddress,
                    int port)
             throws ConnectionException
Connects to the specified IP address to the specified port number. This method is called by the FTPClients if they prefer to transfer data in passive mode.

Parameters:
ipAddress - IP address of the remote host in xxx.xxx.xxx.xxx format.
port - Port number to connect to.
Throws:
ConnectionException - if a network or IO error occurs.

connect

public void connect(InetAddress address,
                    int port)
             throws ConnectionException
Connects to the specified InetAddress to the specified port number. This method is called by the FTPClients if they prefer to transfer data in passive mode.

Parameters:
address - Internet address of the remote host.
port - Port number to connect to.
Throws:
ConnectionException - if a network or IO error occurs.

close

public void close()
Closes this data connection and open streams, if any.


abort

public void abort()
Sets the abort flag to true.


isAborted

public boolean isAborted()
Checks to see if the abort flag was set.

Returns:
true, if the abort request was made. false, otherwise.

list

public RemoteFile[] list(RemoteFile dir)
                  throws ConnectionException,
                         ParseException
Parses the data received over this data connection to an array of RemoteFile objects.

Parameters:
dir - The remote directory for which the listing is being done.
Returns:
An array of RemoteFile objects which are supposedly the children of the specified directory dir.
Throws:
ConnectionException - If a network or IO error occurs.
ParseException - if The data can not be parsed to a RemoteFile object.

download

public void download(File destination,
                     boolean append)
              throws IOException
Reads the stream over this data connection and saves it in the specified local file destination.

Parameters:
destination - The local file to which the contents of the stream are to be stored.
append - Whether or not to append the contents of the stream to the local file.
Throws:
IOException - if an IO error occurs.

upload

public void upload(File source,
                   long skip)
            throws IOException
Sends the contents of the specified local file src to the remote host.

Parameters:
source - The source for whose contents are to be sent to the remote host.
skip - If the value of this parameter is greater than zero, these many bytes will be skipped before sending the data.
Throws:
IOException - if an IO error occurs.

fireDataTransferStarted

protected void fireDataTransferStarted(DataConnectionEvent evt)
Notifies the registered listeners that the data transfer has started.

Parameters:
evt - DataConnectionEvent.

fireDataTransferFinished

protected void fireDataTransferFinished(DataConnectionEvent evt)
Notifies the registered listeners that the data transfer has finished.

Parameters:
evt - DataConnectionEvent.

fireDataTransferAborted

protected void fireDataTransferAborted(DataConnectionEvent evt)
Notifies the registered listeners that the data transfer has aborted.

Parameters:
evt - DataConnectionEvent.

fireDataTransferProgress

protected void fireDataTransferProgress(DataConnectionEvent evt)
Notifies the registered listeners that the data transfer is in progress.

Parameters:
evt - DataConnectionEvent.

stdout

protected void stdout(String message)
Writes the given message to the standard output.

Parameters:
message - The message to be written.


Copyright © 2000-2012 jMethods, Inc.. All Rights Reserved.