How to pause and resume transfers

FTP is generally quite a reliable way to transfer files. However at times network connections fail or processes are restarted, interrupting transfers. Also, it is sometimes necessary to terminate transfers, particularly for larger files that take a long time to transfer. 

In these cases, it is often desirable to resume transfers rather than starting them from scratch - especially for large files. This means only the remaining part of the file is subsequently uploaded or downloaded. 

In edtFTPj/PRO, two methods are available to achieve transfer termination and resumption. These methods are supported in FTPClient, SSLFTPClient, ProFTPClient and SSHFTPClient

Invoking cancelTransfer will cancel the currently executing transfer. To be able to call this method during a transfer will require a separate thread. Once this method is called, the transfer will cease once the current transfer buffer is emptied:

ftp.cancelTransfer(); // called from a different thread 

Cancelling a transfer may leave the connection to the server in an inconsistent state. After cancelling a transfer, it may be necessary to quit and reconnect to the server.

Once a transfer has been cancelled or has been interrupted, resume can be used to complete the transfer. Please note that resume is only supported for binary mode transfers. For resuming both uploads and downloads, it relies on examining the partially downloaded or uploaded file to see how many bytes remain to be transferred. The remaining bytes are then appended to the partial file. To perform a resume, call resume, and then upload or download the file again, as shown below:

ftp.resume();
ftp.get(localFilename, remoteFilename); // gets the rest of the file

Note that resume only applies to the next transfer (upload or download). The internal resume flag is reset once a transfer has been made. If two transfers in a row need to be resumed, resumeTransfer must be called prior to each transfer.  

Because resumeTransfer relies only on the size of the partially downloaded or uploaded file, it does not matter how long ago the transfer failed or was terminated. As long as the partially transferred file is still available (and of course the original file to be transferred has not changed) , resuming the transfer will work correctly.

If resume is called erroneously, it can be cancelled by calling cancelResume. This means the next transfer will not be resumed, but will be transferred completely.