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, three methods are available to achieve transfer termination and resumption. These methods are supported in FTPClient, SSLFTPClient, ProFTPClient and SSHFTPClient.

Cancelling transfers

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.

Resuming transfers

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. This means methods that use a stream instead of a local filename cannot be used in resuming - there is no way to find out how large the local file is. 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, resume must be called prior to each transfer.

Because resume 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.

Arbitrary resumption

Often, it is desirable to download from an arbitrary point in a file without actually resuming a previous download. This is done via the following method:

ftp.resumeNextDownload(offset);

The download begins not from the beginning of the remote file but from the offset. This is useful if the latter portion of a file is to be downloaded. This method can be used with any of the download methods, as a local file is not required to calculate the offset.

This method can't be used with uploads, and isn't necessary because the user has control over local data. To resume an upload arbitrarily, simply supply the correct byte array as input, or the stream set to the correct offset.