public static async System.Threading.Tasks.Task downloadResource( string prefix, ResourceType resourceType, string resourceName, string version, System.IO.Stream resultStream) { long totalFileSize; try { totalFileSize = getResourceVersions(prefix, resourceType, resourceName).versions[version].byteCount; } catch (System.Exception) { throw new NetworkUtilsException( $"Could not find {resourceType.ToString().ToLower()} resource {resourceName} at version {version} on server" ); } int bufferSize = System.Math.Max(1, System.Math.Min((int)(totalFileSize / 100.0), 10000000)); string url = resourceFileUrl(prefix, resourceType, resourceName, version); try { using (var contentStream = await client.GetStreamAsync(url)) { byte[] buffer = new byte[bufferSize]; bool doneReadingContent = false; long totalBytesRead = 0; // @TODO add cancellation? do { int bytesRead = contentStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { doneReadingContent = true; } totalBytesRead += bytesRead; await resultStream.WriteAsync(buffer, 0, bytesRead); int percentDone = totalFileSize == 0 ? 100 : (int)(100 * (double)totalBytesRead / (double)totalFileSize); CLIInterface.writeBottomLineOverwriteExisting($"Download {resourceName}: {percentDone}%"); }while(!doneReadingContent); CLIInterface.logLine(""); } } catch (System.Exception) { throw new NetworkUtilsException( $"Unable to get file for {resourceType} resource {resourceName}" ); } }