public override async Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            int attempts = 0;

            while (attempts++ < 5)
            {
                var uriString = _uri.ToString();
                if (_stream == null)
                {
                    _stream = await _client.StartOrResumeFtpDownload(_uri, _totalDone);

                    _client.EventSource.FinishedDownload(uriString);
                }
                try
                {
                    // This will throw a timeout exception if the connection is interrupted.
                    // Will throw null exception if failed to open (start); this will also retry.
                    int done = await _stream.ReadAsync(buffer, offset, count, cancellationToken);

                    _totalDone += done;
                    return(done);
                }
                catch (Exception ex)
                {
                    CaughtException = ex;

                    _client.EventSource.FailedToDownloadFile(uriString, ex.ToString());

                    // Close ftp resources if possible. Set instances to null to force restart.
                    Close();
                }
            }
            return(0);
        }
        public override async Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            var attempts = 0;

            while (attempts++ < 5)
            {
                if (_stream == null)
                {
                    _stream = await _client.StartOrResumeFtpDownload(_uri, _totalDone);

                    _client.Logger.LogInformation("Finishing download from '{FtpBlobUri}'", _uri);
                }
                try
                {
                    // This will throw a timeout exception if the connection is interrupted.
                    // Will throw null exception if failed to open (start); this will also retry.
                    int done = await _stream.ReadAsync(buffer, offset, count, cancellationToken);

                    _totalDone += done;
                    return(done);
                }
                catch (Exception ex)
                {
                    CaughtException = ex;

                    _client.Logger.LogError(0, ex, "Failed to download file after {Attempts} attempts", attempts);

                    // Close ftp resources and set instance to null to restart the download.
                    _stream?.Dispose();
                    _stream = null;
                }
            }
            return(0);
        }