private byte[] downloadFileIncrementally(string claimToken) { int chunkSize = 16000; int totalBytes = wsClient.DownloadFileStart(accessToken(), claimToken, chunkSize); onDownloadStarted(totalBytes); if (totalBytes == 0) { return(null); } int numChunks = totalBytes / chunkSize; int fragmentChunkSize = totalBytes % chunkSize; if (fragmentChunkSize > 0) { numChunks += 1; } // create the buffer byte[] buffer = new byte[totalBytes]; int numDownloaded = 0; // move the full-sized chunks for (int i = 0; i < (numChunks - 1); i++) { byte[] chunk = wsClient.DownloadFilePart(accessToken(), claimToken, i); if (chunk == null) { return(null); } Array.Copy(chunk, 0, buffer, (chunkSize * i), chunkSize); numDownloaded += chunkSize; onPartDownloaded(numDownloaded, totalBytes); } // move the fragment chunk (if any) if (fragmentChunkSize > 0) { byte[] chunk = wsClient.DownloadFilePart(accessToken(), claimToken, numChunks - 1); if (chunk == null) { return(null); } Array.Copy(chunk, 0, buffer, (numChunks - 1) * chunkSize, fragmentChunkSize); numDownloaded += fragmentChunkSize; onPartDownloaded(numDownloaded, totalBytes); } // end the download if (wsClient.DownloadFileEnd(accessToken(), claimToken) == false) { return(null); } onDownloadEnded(totalBytes); return(buffer); }